forked from jfelchner/ruby-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
221 lines (191 loc) · 4.4 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require 'test/unit'
require File.expand_path('../lib/progressbar', __FILE__)
class ProgressBarTest < Test::Unit::TestCase
SleepUnit = 0.01
def teardown
Time.clear_stubs
end
def do_make_progress_bar (title, total)
ProgressBar.new(title, total)
end
def test_bytes
total = 1024 * 1024
pbar = do_make_progress_bar("test(bytes)", total)
pbar.file_transfer_mode
0.step(total, 2**14) {|x|
pbar.set(x)
sleep(SleepUnit)
}
pbar.finish
end
def test_clear
total = 100
pbar = do_make_progress_bar("test(clear)", total)
total.times {
sleep(SleepUnit)
pbar.inc
}
pbar.clear
puts
end
def test_halt
total = 100
pbar = do_make_progress_bar("test(halt)", total)
(total / 2).times {
sleep(SleepUnit)
pbar.inc
}
pbar.halt
end
def test_inc
total = 100
pbar = do_make_progress_bar("test(inc)", total)
total.times {
sleep(SleepUnit)
pbar.inc
}
pbar.finish
end
def test_inc_x
total = File.size("lib/progressbar.rb")
pbar = do_make_progress_bar("test(inc(x))", total)
File.new("lib/progressbar.rb").each {|line|
sleep(SleepUnit)
pbar.inc(line.length)
}
pbar.finish
end
def test_invalid_set
total = 100
pbar = do_make_progress_bar("test(invalid set)", total)
begin
pbar.set(200)
rescue RuntimeError => e
puts e.message
end
end
def test_set
total = 1000
pbar = do_make_progress_bar("test(set)", total)
(1..total).find_all {|x| x % 10 == 0}.each {|x|
sleep(SleepUnit)
pbar.set(x)
}
pbar.finish
end
def test_slow
total = 100000
pbar = do_make_progress_bar("test(slow)", total)
0.step(500, 1) {|x|
pbar.set(x)
sleep(SleepUnit)
}
pbar.halt
end
def test_total_zero
total = 0
pbar = do_make_progress_bar("test(total=0)", total)
pbar.finish
end
def test_alternate_bar
total = 100
pbar = do_make_progress_bar("test(alternate)", total)
pbar.bar_mark = "="
total.times {
sleep(SleepUnit)
pbar.inc
}
pbar.finish
end
def test_custom_bar
custom_bar_class = Class.new(ProgressBar) do
def title_width
20
end
end
pbar = custom_bar_class.new('test(custom)', 100)
total = 100
total.times {
sleep(SleepUnit)
pbar.inc
}
pbar.finish
end
def test_timecop
offset = 3905
total = 10000
pbar = do_make_progress_bar("test(timecop)", total)
Time.stub(:now_without_mock_time, lambda { Time.now_without_stubbing })
Time.stub(:now, lambda { Time.now_without_stubbing - offset })
0.step(500, 1) {|x|
Time.stub(:now, lambda { Time.now_without_stubbing + offset }) if x == 250
sleep(SleepUnit)
pbar.set(x)
}
pbar.halt
end
def test_delorean
offset = 3905
total = 10000
pbar = do_make_progress_bar("test(delorean)", total)
Time.stub(:now_without_delorean, lambda { Time.now_without_stubbing })
Time.stub(:now, lambda { Time.now_without_stubbing - offset })
0.step(500, 1) {|x|
Time.stub(:now, lambda { Time.now_without_stubbing + offset }) if x == 250
sleep(SleepUnit)
pbar.set(x)
}
pbar.halt
end
class StubbedTtyProgressBar < ProgressBar
def tty?; false; end
end
def test_non_tty
total = 1024 * 1024
pbar = StubbedTtyProgressBar.new("non-tty compatible", total)
0.step(total, 2**14) {|x|
pbar.set(x)
sleep(SleepUnit)
}
pbar.finish
end
end
class ReversedProgressBarTest < ProgressBarTest
def do_make_progress_bar (title, total)
ReversedProgressBar.new(title, total)
end
end
module Stubbing
def stub(method_name, value=nil)
stubs[method_name.to_sym] = value
end
def clear_stubs
stubs.clear
end
def respond_to?(method_name, include_private=false)
has_stub?(method_name) || super
end
def method_missing(method_name, *args, &blk)
has_stub?(method_name) ? invoke_stub(method_name) : super
end
private
def stubs
@stubs ||= {}
end
def has_stub?(method_name)
stubs.keys.include? method_name.to_sym
end
def invoke_stub(method_name)
stub = stubs[method_name]
stub.respond_to?(:call) ? stub.call : stub
end
end
class Time
extend Stubbing
class << self
alias_method :now_without_stubbing, :now
def now
has_stub?(:now) ? invoke_stub(:now) : now_without_stubbing
end
end
end