spec/lib/ruby-progressbar/base_spec.rb in ruby-progressbar-1.5.1 vs spec/lib/ruby-progressbar/base_spec.rb in ruby-progressbar-1.6.0

- old
+ new

@@ -292,10 +292,35 @@ progressbar.stop non_tty_output.rewind expect(non_tty_output.read).to eql "\n\nProgress: |====\n" end + + it 'ignores changes to the title due to the fact that the bar length cannot change' do + progressbar = ProgressBar::Base.new(:output => non_tty_output, :length => 20, :starting_at => 0, :total => 6, :throttle_rate => 0.0) + + 3.times { progressbar.increment } + + progressbar.title = "Testing" + progressbar.stop + + non_tty_output.rewind + + expect(non_tty_output.read).to eql "\n\nProgress: |====\n" + end + + it 'allows the title to be customized when the bar is created' do + progressbar = ProgressBar::Base.new(:output => non_tty_output, :title => 'Custom', :length => 20, :starting_at => 0, :total => 6, :throttle_rate => 0.0) + + 3.times { progressbar.increment } + + progressbar.stop + + non_tty_output.rewind + + expect(non_tty_output.read).to eql "\n\nCustom: |=====\n" + end end end context 'when a bar is about to be completed' do let(:progressbar) { ProgressBar::Base.new(:starting_at => 5, :total => 6, :output => output, :length => 20) } @@ -319,10 +344,54 @@ expect(output.read).to end_with " \rProgress: |====== |\rProgress: |========|\n" end end end + context 'when a bar with autofinish=false is about to be completed' do + let(:progressbar) { ProgressBar::Base.new(:autofinish => false, :starting_at => 5, :total => 6, :output => output, :length => 20) } + + context 'and it is incremented' do + before { progressbar.increment } + + it 'does not automatically finish' do + expect(progressbar).not_to be_finished + end + + it 'does not prints a new line' do + output.rewind + + expect(output.read.end_with?("\n")).to eql false + end + + it 'allows reset' do + progressbar.finish + expect(progressbar).to be_finished + + progressbar.reset + + expect(progressbar).not_to be_finished + end + + it 'does prints a new line when manually finished' do + progressbar.finish + expect(progressbar).to be_finished + + output.rewind + + expect(output.read.end_with?("\n")).to eql true + end + + it 'does not continue to print bars if finish is subsequently called' do + progressbar.finish + + output.rewind + + expect(output.read).to end_with " \rProgress: |====== |\rProgress: |========|\n" + end + end + end + context 'when a bar has an unknown amount to completion' do let(:progressbar) { ProgressBar::Base.new(:total => nil, :output => output, :length => 80, :unknown_progress_animation_steps => ['=--', '-=-', '--=']) } it 'is represented correctly' do expect(progressbar.to_s('%i')).to eql '=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=-' @@ -698,10 +767,24 @@ expect(progressbar.to_s('%w%i')).to match /^====\s{96}\z/ progressbar.increment expect(progressbar.to_s('%w%i')).to match /^= 5 =\s{95}\z/ end + it 'can display a percentage, even if the total is unknown' do + progressbar = ProgressBar::Base.new(:output => output, :length => 100, :total => nil, :starting_at => 0) + + expect(progressbar.to_s('%p')).to match /\A0\z/ + expect(progressbar.to_s('%P')).to match /\A0\.0\z/ + end + + it 'can display a percentage, even if the total is zero' do + progressbar = ProgressBar::Base.new(:output => output, :length => 100, :total => 0, :starting_at => 0) + + expect(progressbar.to_s('%p')).to match /\A100\z/ + expect(progressbar.to_s('%P')).to match /\A100\.0\z/ + end + it 'displays the current capacity when passed the "%c" format flag' do progressbar = ProgressBar::Base.new(:output => output, :starting_at => 0) expect(progressbar.to_s('%c')).to match /^0\z/ progressbar.increment @@ -720,13 +803,25 @@ progressbar = ProgressBar::Base.new(:starting_at => 33, :total => 200) expect(progressbar.to_s('%p')).to match /^16\z/ end + it 'displays the justified percentage complete when passed the "%j" format flag' do + progressbar = ProgressBar::Base.new(:starting_at => 33, :total => 200) + + expect(progressbar.to_s('%j')).to match /^ 16\z/ + end + it 'displays the percentage complete when passed the "%P" format flag' do progressbar = ProgressBar::Base.new(:starting_at => 33, :total => 200) expect(progressbar.to_s('%P')).to match /^16.50\z/ + end + + it 'displays the justified percentage complete when passed the "%J" format flag' do + progressbar = ProgressBar::Base.new(:starting_at => 33, :total => 200) + + expect(progressbar.to_s('%J')).to match /^ 16.50\z/ end it 'displays only up to 2 decimal places when using the "%P" flag' do progressbar = ProgressBar::Base.new(:starting_at => 66, :total => 99)