ruby-progressbar 0.11.0 → 1.0.0rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +1 -0
  4. data/.travis.yml +8 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +42 -0
  7. data/Guardfile +6 -0
  8. data/LICENSE +22 -0
  9. data/README.md +229 -63
  10. data/lib/progress_bar/base.rb +166 -0
  11. data/lib/progress_bar/components.rb +5 -0
  12. data/lib/progress_bar/components/bar.rb +44 -0
  13. data/lib/progress_bar/components/elapsed_timer.rb +20 -0
  14. data/lib/progress_bar/components/estimated_timer.rb +88 -0
  15. data/lib/progress_bar/components/progressable.rb +92 -0
  16. data/lib/progress_bar/components/timer.rb +64 -0
  17. data/lib/progress_bar/depreciable.rb +121 -0
  18. data/lib/progress_bar/format.rb +2 -0
  19. data/lib/progress_bar/format/base.rb +30 -0
  20. data/lib/progress_bar/format/molecule.rb +37 -0
  21. data/lib/progress_bar/formatter.rb +109 -0
  22. data/lib/progress_bar/length_calculator.rb +53 -0
  23. data/lib/progress_bar/running_average_calculator.rb +7 -0
  24. data/lib/progress_bar/time.rb +22 -0
  25. data/lib/progress_bar/version.rb +3 -0
  26. data/lib/progressbar.rb +8 -295
  27. data/lib/ruby-progressbar.rb +19 -0
  28. data/ruby-progressbar.gemspec +44 -0
  29. data/spec/progress_bar/base_spec.rb +434 -0
  30. data/spec/progress_bar/components/bar_spec.rb +198 -0
  31. data/spec/progress_bar/components/elapsed_timer_spec.rb +79 -0
  32. data/spec/progress_bar/components/estimated_timer_spec.rb +173 -0
  33. data/spec/progress_bar/components/progressable_spec.rb +30 -0
  34. data/spec/progress_bar/format/molecule_spec.rb +22 -0
  35. data/spec/progress_bar/running_average_calculator_spec.rb +11 -0
  36. data/spec/progress_bar/time_spec.rb +51 -0
  37. data/spec/spec_helper.rb +13 -0
  38. data/spec/support/focused.rb +7 -0
  39. data/spec/support/timecop.rb +19 -0
  40. metadata +170 -19
  41. data/GPL_LICENSE +0 -340
  42. data/RUBY_LICENSE +0 -53
  43. data/test.rb +0 -247
@@ -0,0 +1,198 @@
1
+ require 'spec_helper'
2
+
3
+ describe ProgressBar::Components::Bar do
4
+ context 'when a new bar is created' do
5
+ context 'and no parameters are passed' do
6
+ before { @progressbar = ProgressBar::Components::Bar.new }
7
+
8
+ describe '#total' do
9
+ it 'returns the default total' do
10
+ @progressbar.total.should eql ProgressBar::Components::Bar::DEFAULT_TOTAL
11
+ end
12
+ end
13
+
14
+ describe '#progress_mark' do
15
+ it 'returns the default mark' do
16
+ @progressbar.progress_mark.should eql ProgressBar::Components::Bar::DEFAULT_PROGRESS_MARK
17
+ end
18
+ end
19
+
20
+ context 'and the bar has not been started' do
21
+ describe '#progress' do
22
+ it 'returns the default beginning position' do
23
+ @progressbar.progress.should be_zero
24
+ end
25
+ end
26
+ end
27
+
28
+ context 'and the bar has been started with no starting value given' do
29
+ before { @progressbar.start }
30
+
31
+ describe '#progress' do
32
+ it 'returns the default starting value' do
33
+ @progressbar.progress.should eql ProgressBar::Components::Progressable::DEFAULT_BEGINNING_POSITION
34
+ end
35
+ end
36
+ end
37
+
38
+ context 'and the bar has been started with a starting value' do
39
+ before { @progressbar.start :at => 10 }
40
+
41
+ describe '#progress' do
42
+ it 'returns the given starting value' do
43
+ @progressbar.progress.should eql 10
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ context 'and options are passed' do
50
+ before { @progressbar = ProgressBar::Components::Bar.new(:total => 12, :progress_mark => 'x') }
51
+
52
+ describe '#total' do
53
+ it 'returns the overridden total' do
54
+ @progressbar.total.should eql 12
55
+ end
56
+ end
57
+
58
+ describe '#progress_mark' do
59
+ it 'returns the overridden mark' do
60
+ @progressbar.progress_mark.should eql 'x'
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ context 'when just begun' do
67
+ before do
68
+ @progressbar = ProgressBar::Components::Bar.new(:total => 50)
69
+ @progressbar.length = 100
70
+ @progressbar.start
71
+ end
72
+
73
+ describe '#percentage_completed' do
74
+ it 'calculates the amount' do
75
+ @progressbar.percentage_completed.should eql 0
76
+ end
77
+ end
78
+
79
+ describe '#to_s' do
80
+ it 'displays the bar with no indication of progress' do
81
+ @progressbar.to_s.should eql ' '
82
+ end
83
+ end
84
+ end
85
+
86
+ context 'when nothing has been completed' do
87
+ before do
88
+ @progressbar = ProgressBar::Components::Bar.new(:total => 50)
89
+ @progressbar.length = 100
90
+ @progressbar.start
91
+ end
92
+
93
+ context 'and the bar is incremented' do
94
+ before { @progressbar.increment }
95
+
96
+ it 'adds to the progress amount' do
97
+ @progressbar.progress.should eql 1
98
+ end
99
+
100
+ describe '#percentage_completed' do
101
+ it 'calculates the amount completed' do
102
+ @progressbar.percentage_completed.should eql 2
103
+ end
104
+ end
105
+
106
+ describe '#to_s' do
107
+ it 'displays the bar with an indication of progress' do
108
+ @progressbar.to_s.should eql '== '
109
+ end
110
+ end
111
+ end
112
+
113
+ describe '#percentage_completed' do
114
+ it 'is zero' do
115
+ @progressbar.percentage_completed.should eql 0
116
+ end
117
+ end
118
+
119
+ describe '#to_s' do
120
+ it 'displays the bar with no indication of progress' do
121
+ @progressbar.to_s.should eql ' '
122
+ end
123
+ end
124
+ end
125
+
126
+ context 'when a fraction of a percentage has been completed' do
127
+ before do
128
+ @progressbar = ProgressBar::Components::Bar.new(:total => 200)
129
+ @progressbar.length = 100
130
+ @progressbar.start :at => 1
131
+ end
132
+
133
+ describe '#percentage_completed' do
134
+ it 'always rounds down' do
135
+ @progressbar.percentage_completed.should eql 0
136
+ end
137
+ end
138
+
139
+ describe '#to_s' do
140
+ it 'displays the bar with no indication of progress' do
141
+ @progressbar.to_s.should eql ' '
142
+ end
143
+ end
144
+ end
145
+
146
+ context 'when completed' do
147
+ before do
148
+ @progressbar = ProgressBar::Components::Bar.new(:total => 50)
149
+ @progressbar.length = 100
150
+ @progressbar.start :at => 50
151
+ end
152
+
153
+ context 'and the bar is incremented' do
154
+ before { @progressbar.increment }
155
+
156
+ it 'does not increment past the total' do
157
+ @progressbar.progress.should eql 50
158
+ @progressbar.percentage_completed.should eql 100
159
+ end
160
+
161
+ describe '#to_s' do
162
+ it 'displays the bar as 100% complete' do
163
+ @progressbar.to_s.should eql '=' * 100
164
+ end
165
+ end
166
+ end
167
+
168
+ context 'and the bar is decremented' do
169
+ before { @progressbar.decrement }
170
+
171
+ it 'removes some progress from the bar' do
172
+ @progressbar.progress.should eql 49
173
+ @progressbar.percentage_completed.should eql 98
174
+ end
175
+
176
+ describe '#to_s' do
177
+ it 'displays the bar as 98% complete' do
178
+ @progressbar.to_s.should eql "#{'=' * 98} "
179
+ end
180
+ end
181
+ end
182
+
183
+ describe '#to_s' do
184
+ it 'displays the bar as 100% complete' do
185
+ @progressbar.to_s.should eql ('=' * 100)
186
+ end
187
+ end
188
+ end
189
+
190
+ context "when attempting to set the bar's current value to be greater than the total" do
191
+ describe '#new' do
192
+ it 'raises an error' do
193
+ @progressbar = ProgressBar::Components::Bar.new(:total => 10)
194
+ lambda { @progressbar.start :at => 11 }.should raise_error "You can't set the item's current value to be greater than the total."
195
+ end
196
+ end
197
+ end
198
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+ require 'timecop'
3
+
4
+ describe ProgressBar::Components::ElapsedTimer do
5
+ before { @timer = ProgressBar::Components::ElapsedTimer.new }
6
+
7
+ describe '#to_s' do
8
+ context 'when the timer has not been started' do
9
+ it 'displays "Time: --:--:--"' do
10
+ @timer.to_s.should eql 'Time: --:--:--'
11
+ end
12
+ end
13
+
14
+ context 'when it has just been started' do
15
+ it 'displays "Time: 00:00:00"' do
16
+ @timer.start
17
+ @timer.to_s.should eql 'Time: 00:00:00'
18
+ end
19
+ end
20
+
21
+ context 'when it was started 4 hours, 28 minutes and 13 seconds ago' do
22
+ before do
23
+ Timecop.travel(-16093) do
24
+ @timer.start
25
+ end
26
+ end
27
+
28
+ context 'and it was stopped 32 seconds ago' do
29
+ before do
30
+ Timecop.travel(-32) do
31
+ @timer.stop
32
+ end
33
+ end
34
+
35
+ context 'and #reset is called' do
36
+ before { @timer.reset }
37
+
38
+ it 'displays "Time: --:--:--"' do
39
+ @timer.to_s.should eql 'Time: --:--:--'
40
+ end
41
+ end
42
+
43
+ it 'displays "Time: 04:27:41"' do
44
+ @timer.to_s.should eql 'Time: 04:27:41'
45
+ end
46
+ end
47
+
48
+ context 'and #reset is called' do
49
+ before { @timer.reset }
50
+
51
+ it 'displays "Time: --:--:--"' do
52
+ @timer.to_s.should eql 'Time: --:--:--'
53
+ end
54
+ end
55
+
56
+ it 'displays "Time: 04:28:13"' do
57
+ @timer.to_s.should eql 'Time: 04:28:13'
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#stopped?' do
63
+ context 'when the timer is started' do
64
+ before { @timer.start }
65
+
66
+ context 'and then it is stopped' do
67
+ before { @timer.stop }
68
+
69
+ context 'and then it is restarted' do
70
+ before { @timer.start }
71
+
72
+ it 'is false' do
73
+ @timer.should_not be_stopped
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+
3
+ describe ProgressBar::Components::EstimatedTimer do
4
+ describe '#progress=' do
5
+ it 'raises an error when passed a number larger than the total' do
6
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:total => 100)
7
+ lambda{ @estimated_time.progress = 101 }.should raise_error "You can't set the item's current value to be greater than the total."
8
+ end
9
+ end
10
+
11
+ describe '#to_s' do
12
+ context 'when the timer has been started but no progress has been made' do
13
+ before do
14
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:total => 100)
15
+ @estimated_time.start
16
+ end
17
+
18
+ it 'displays an unknown time remaining' do
19
+ @estimated_time.to_s.should eql ' ETA: ??:??:??'
20
+ end
21
+
22
+ context 'and it is incremented' do
23
+ it 'should not display unknown time remaining' do
24
+ @estimated_time.increment
25
+ @estimated_time.to_s.should_not eql ' ETA: ??:??:??'
26
+ end
27
+ end
28
+ end
29
+
30
+ context 'when half the progress has been made' do
31
+ context 'and smoothing is turned off' do
32
+ let(:smoothing) { 0.0 }
33
+
34
+ context 'and it took 3:42:12 to do it' do
35
+ before do
36
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:starting_at => 0, :total => 100, :smoothing => smoothing)
37
+
38
+ Timecop.travel(-13332) do
39
+ @estimated_time.start
40
+ 50.times { @estimated_time.increment }
41
+ end
42
+ end
43
+
44
+ context 'when #decrement is called' do
45
+ before { 20.times { @estimated_time.decrement } }
46
+
47
+ it 'displays the correct time remaining' do
48
+ @estimated_time.to_s.should eql ' ETA: 08:38:28'
49
+ end
50
+ end
51
+
52
+ context 'when #reset is called' do
53
+ before { @estimated_time.reset }
54
+
55
+ it 'displays unknown time remaining' do
56
+ @estimated_time.to_s.should eql ' ETA: ??:??:??'
57
+ end
58
+ end
59
+
60
+ it 'displays the correct time remaining' do
61
+ @estimated_time.to_s.should eql ' ETA: 03:42:12'
62
+ end
63
+ end
64
+
65
+ context 'when it is estimated to take longer than 99:59:59' do
66
+ before do
67
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:starting_at => 0, :total => 100, :smoothing => smoothing)
68
+
69
+ Timecop.travel(-120000) do
70
+ @estimated_time.start
71
+ 25.times { @estimated_time.increment }
72
+ end
73
+ end
74
+
75
+ context 'and the out of bounds time format has been set to "friendly"' do
76
+ before { @estimated_time.out_of_bounds_time_format = :friendly }
77
+
78
+ it 'displays "> 4 Days" remaining' do
79
+ @estimated_time.to_s.should eql ' ETA: > 4 Days'
80
+ end
81
+ end
82
+
83
+ context 'and the out of bounds time format has been set to "unknown"' do
84
+ before { @estimated_time.out_of_bounds_time_format = :unknown }
85
+
86
+ it 'displays "??:??:??" remaining' do
87
+ @estimated_time.to_s.should eql ' ETA: ??:??:??'
88
+ end
89
+ end
90
+
91
+ it 'displays the correct time remaining' do
92
+ @estimated_time.to_s.should eql ' ETA: 100:00:00'
93
+ end
94
+ end
95
+ end
96
+
97
+ context 'and smoothing is turned on' do
98
+ let(:smoothing) { 0.5 }
99
+
100
+ context 'and it took 3:42:12 to do it' do
101
+ before do
102
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:starting_at => 0, :total => 100, :smoothing => smoothing)
103
+
104
+ Timecop.travel(-13332) do
105
+ @estimated_time.start
106
+ 50.times { @estimated_time.increment }
107
+ end
108
+ end
109
+
110
+ context 'when #decrement is called' do
111
+ before { 20.times { @estimated_time.decrement } }
112
+
113
+ it 'displays the correct time remaining' do
114
+ @estimated_time.to_s.should eql ' ETA: 08:14:34'
115
+ end
116
+ end
117
+
118
+ context 'when #reset is called' do
119
+ before { @estimated_time.reset }
120
+
121
+ it 'displays unknown time remaining' do
122
+ @estimated_time.to_s.should eql ' ETA: ??:??:??'
123
+ end
124
+ end
125
+
126
+ it 'displays the correct time remaining' do
127
+ @estimated_time.to_s.should eql ' ETA: 03:51:16'
128
+ end
129
+ end
130
+
131
+ context 'when it is estimated to take longer than 99:59:59' do
132
+ before do
133
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:starting_at => 0, :total => 100, :smoothing => smoothing)
134
+
135
+ Timecop.travel(-120000) do
136
+ @estimated_time.start
137
+ 25.times { @estimated_time.increment }
138
+ end
139
+ end
140
+
141
+ context 'and the out of bounds time format has been set to "friendly"' do
142
+ before { @estimated_time.out_of_bounds_time_format = :friendly }
143
+
144
+ it 'displays "> 4 Days" remaining' do
145
+ @estimated_time.to_s.should eql ' ETA: > 4 Days'
146
+ end
147
+ end
148
+
149
+ context 'and the out of bounds time format has been set to "unknown"' do
150
+ before { @estimated_time.out_of_bounds_time_format = :unknown }
151
+
152
+ it 'displays "??:??:??" remaining' do
153
+ @estimated_time.to_s.should eql ' ETA: ??:??:??'
154
+ end
155
+ end
156
+
157
+ it 'displays the correct time remaining' do
158
+ @estimated_time.to_s.should eql ' ETA: 105:33:19'
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ describe '#out_of_bounds_time_format=' do
166
+ context 'when set to an invalid format' do
167
+ it 'raises an exception' do
168
+ @estimated_time = ProgressBar::Components::EstimatedTimer.new(:total => 100)
169
+ lambda{ @estimated_time.out_of_bounds_time_format = :foo }.should raise_error('Invalid Out Of Bounds time format. Valid formats are [:unknown, :friendly, nil]')
170
+ end
171
+ end
172
+ end
173
+ end