diff --git a/lib/miga/common.rb b/lib/miga/common.rb index 280ebcd..1e139a3 100644 --- a/lib/miga/common.rb +++ b/lib/miga/common.rb @@ -64,11 +64,12 @@ def say(*par) # The report goes to $stderr iff --verbose def advance(step, n = 0, total = nil, bin = true) # Initialize advance timing - @_advance_time ||= { last: nil, n: 0, avg: nil } - if @_advance_time[:n] > n + @_advance_time ||= { last: nil, n: 0, avg: nil, total: total } + if @_advance_time[:n] > n || total != @_advance_time[:total] @_advance_time[:last] = nil @_advance_time[:n] = 0 @_advance_time[:avg] = nil + @_advance_time[:total] = total end # Estimate timing diff --git a/test/common_test.rb b/test/common_test.rb index c81f4e5..1543f40 100644 --- a/test/common_test.rb +++ b/test/common_test.rb @@ -76,23 +76,26 @@ def test_miga_name def test_advance m = MiGA::MiGA.new - # Check advance when missing total + # Check advance when missing total and n o = capture_stderr { m.advance('x', 0) }.string assert_match(%r{\] x *\r}, o) + # Check advance when missing total + o = capture_stderr { m.advance('x', 10) }.string + assert_match(%r{\] x 10 *\r}, o) + # Initialize advance o = capture_stderr { m.advance('x', 0, 1001) }.string assert_match(%r{\] x 0\.0% \(0/1001\) *\r}, o) # Insufficient data for prediction - sleep(1) o = capture_stderr { m.advance('x', 1, 1000) }.string assert_match(%r{\] x 0\.1% \(1/1000\) *\r}, o) # Predict time - sleep(1) - o = capture_stderr { m.advance('x', 2, 1000) }.string - assert_match(%r{\] x 0\.2% \(2/1000\) 1\d\.\dm left *\r}, o) + sleep(1.01) + o = capture_stderr { m.advance('x', 4, 1000) }.string + assert_match(%r{\] x 0\.4% \(4/1000\) \d\.\dm left *\r}, o) end def test_num_suffix