@@ -74,3 +74,63 @@ def test_formats_line_with_zero_expected_total
7474 assert_equal "0/0 completed (0.0%) | ETA calculating..." , line
7575 end
7676end
77+
78+ require "stringio"
79+
80+ class FakeTTY
81+ attr_reader :writes
82+
83+ def initialize
84+ @writes = [ ]
85+ end
86+
87+ def tty? = true
88+ def print ( str ) = @writes << str
89+ end
90+
91+ class ProgressReporterInstanceTest < Minitest ::Test
92+ def test_update_is_noop_with_empty_samples
93+ io = StringIO . new
94+ Bench ::ProgressReporter . new ( expected_total : 500 , io : io ) . update ( [ ] )
95+ assert_equal "" , io . string
96+ end
97+
98+ def test_non_tty_prints_immediately_then_throttles_by_sample_time
99+ io = StringIO . new
100+ reporter = Bench ::ProgressReporter . new ( expected_total : 500 , io : io , plain_interval : 10 )
101+
102+ reporter . update ( [ { "t" => 0.0 , "completed" => 10 } ] )
103+ reporter . update ( [ { "t" => 0.0 , "completed" => 10 } , { "t" => 5.0 , "completed" => 60 } ] )
104+ reporter . update ( [
105+ { "t" => 0.0 , "completed" => 10 } , { "t" => 5.0 , "completed" => 60 } ,
106+ { "t" => 11.0 , "completed" => 120 }
107+ ] )
108+
109+ lines = io . string . lines
110+ assert_equal 2 , lines . length
111+ assert_includes lines [ 0 ] , "10/500 completed"
112+ assert_includes lines [ 1 ] , "120/500 completed"
113+ end
114+
115+ def test_tty_redraws_in_place_on_every_update
116+ io = FakeTTY . new
117+ reporter = Bench ::ProgressReporter . new ( expected_total : 500 , io : io )
118+
119+ reporter . update ( [ { "t" => 0.0 , "completed" => 10 } ] )
120+ reporter . update ( [ { "t" => 0.1 , "completed" => 12 } ] )
121+
122+ assert_equal 2 , io . writes . length
123+ assert_match ( /\A \r 10\/ 500 completed.*\e \[ K\z / , io . writes [ 0 ] )
124+ assert_match ( /\A \r 12\/ 500 completed.*\e \[ K\z / , io . writes [ 1 ] )
125+ end
126+
127+ def test_finish_prints_newline_on_tty_only
128+ tty_io = FakeTTY . new
129+ Bench ::ProgressReporter . new ( expected_total : 500 , io : tty_io ) . finish
130+ assert_equal [ "\n " ] , tty_io . writes
131+
132+ plain_io = StringIO . new
133+ Bench ::ProgressReporter . new ( expected_total : 500 , io : plain_io ) . finish
134+ assert_equal "" , plain_io . string
135+ end
136+ end
0 commit comments