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

- old
+ new

@@ -1,6 +1,6 @@ -require 'rspectacular' +require 'spec_helper' require 'support/time' require 'stringio' describe ProgressBar::Base do let(:output) do @@ -30,11 +30,11 @@ and_return 20 progressbar.increment output.rewind - expect(output.read).to match /\raaaaaaaaaaaaaaaaaaaaaaaaa \r\s+\raaaaaaaaaaaaaaaaaaaaaaaaa\r\z/ + expect(output.read).to match(/\raaaaaaaaaaaaaaaaaaaaaaaaa \r\s+\raaaaaaaaaaaaaaaaaaaaaaaaa\r\z/) end context 'and the bar length is calculated' do it 'returns the proper string' do progressbar = ProgressBar::Base.new(:output => output, :title => ('*' * 21), :starting_at => 5, :total => 10, :autostart => false) @@ -184,11 +184,11 @@ end end it 'completes the bar' do output.rewind - expect(output.read).to match /Progress: \|#{'=' * 68}\|\n/ + expect(output.read).to match(/Progress: \|#{'=' * 68}\|\n/) end it 'shows the elapsed time instead of the estimated time since the bar is completed' do expect(progressbar.to_s('%e')).to eql 'Time: 00:02:00' end @@ -425,47 +425,47 @@ describe '#progress_mark=' do it 'changes the mark used to represent progress and updates the output' do progressbar.progress_mark = 'x' output.rewind - expect(output.read).to match /\rProgress: \|xxxxxx#{' ' * 62}\|\r\z/ + expect(output.read).to match(/\rProgress: \|xxxxxx#{' ' * 62}\|\r\z/) end end describe '#remainder_mark=' do it 'changes the mark used to represent the remaining part of the bar and updates the output' do progressbar.remainder_mark = 'x' output.rewind - expect(output.read).to match /\rProgress: \|======#{'x' * 62}\|\r\z/ + expect(output.read).to match(/\rProgress: \|======#{'x' * 62}\|\r\z/) end end describe '#title=' do it 'changes the title used to represent the items being progressed and updates the output' do progressbar.title = 'Items' output.rewind - expect(output.read).to match /\rItems: \|=======#{' ' * 64}\|\r\z/ + expect(output.read).to match(/\rItems: \|=======#{' ' * 64}\|\r\z/) end end describe '#reset' do before { progressbar.reset } it 'resets the bar back to the starting value' do output.rewind - expect(output.read).to match /\rProgress: \|#{' ' * 68}\|\r\z/ + expect(output.read).to match(/\rProgress: \|#{' ' * 68}\|\r\z/) end end describe '#stop' do before { progressbar.stop } it 'forcibly halts the bar wherever it is and cancels it' do output.rewind - expect(output.read).to match /\rProgress: \|======#{' ' * 62}\|\n\z/ + expect(output.read).to match(/\rProgress: \|======#{' ' * 62}\|\n\z/) end it 'does not output the bar multiple times if the bar is already stopped' do output.rewind progressbar.stop @@ -496,45 +496,45 @@ describe '#reset' do before { progressbar.reset } it 'resets the bar back to the starting value' do output.rewind - expect(output.read).to match /\rProgress: \|==========#{' ' * 90}\|\r\z/ + expect(output.read).to match(/\rProgress: \|==========#{' ' * 90}\|\r\z/) end end end end describe '#clear' do it 'clears the current terminal line and/or bar text' do progressbar.clear output.rewind - expect(output.read).to match /^#{progressbar.send(:clear_string)}/ + expect(output.read).to match(/^#{progressbar.send(:clear_string)}/) end end describe '#start' do it 'clears the current terminal line' do progressbar.start output.rewind - expect(output.read).to match /^#{progressbar.send(:clear_string)}/ + expect(output.read).to match(/^#{progressbar.send(:clear_string)}/) end it 'prints the bar for the first time' do progressbar.start output.rewind - expect(output.read).to match /Progress: \| \|\r\z/ + expect(output.read).to match(/Progress: \| \|\r\z/) end it 'prints correctly if passed a position to start at' do progressbar.start(:at => 20) output.rewind - expect(output.read).to match /Progress: \|============= \|\r\z/ + expect(output.read).to match(/Progress: \|============= \|\r\z/) end end context 'when the bar has not been completed' do let(:progressbar) { ProgressBar::Base.new(:length => 112, :starting_at => 0, :total => 50, :output => output, :throttle_rate => 0.0) } @@ -542,11 +542,11 @@ describe '#increment' do before { progressbar.increment } it 'displays the bar with the correct formatting' do output.rewind - expect(output.read).to match /Progress: \|== \|\r\z/ + expect(output.read).to match(/Progress: \|== \|\r\z/) end end end context 'when a new bar is created with a specific format' do @@ -555,60 +555,60 @@ context 'if called with no arguments' do before { progressbar.format } it 'resets the format back to the default' do - expect(progressbar.to_s).to match /^Progress: \|\s+\|\z/ + expect(progressbar.to_s).to match(/^Progress: \|\s+\|\z/) end end context 'if called with a specific format string' do before { progressbar.format '%t' } it 'sets it as the new format for the bar' do - expect(progressbar.to_s).to match /^Progress\z/ + expect(progressbar.to_s).to match(/^Progress\z/) end end end context '#to_s' do context 'when no time has elapsed' do it 'displays zero for the rate' do Timecop.freeze do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 0) - expect(progressbar.to_s('%r')).to match /^0\z/ + expect(progressbar.to_s('%r')).to match(/^0\z/) end end end context 'when any time has elasped' do context 'and the standard rate is applied' do it 'displays zero for %r if no progress has been made' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^0\z/ + expect(progressbar.to_s('%r')).to match(/^0\z/) end end it 'displays zero for %R if no progress has been made' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) Timecop.travel(2) do - expect(progressbar.to_s('%R')).to match /^0.00\z/ + expect(progressbar.to_s('%R')).to match(/^0.00\z/) end end it 'takes into account the starting position when calculating %r' do Timecop.freeze do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) progressbar.start progressbar.progress += 20 Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^10\z/ + expect(progressbar.to_s('%r')).to match(/^10\z/) end end end it 'takes into account the starting position when calculating %R' do @@ -616,11 +616,11 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) progressbar.start progressbar.progress += 13 Timecop.travel(2) do - expect(progressbar.to_s('%R')).to match /^6.50\z/ + expect(progressbar.to_s('%R')).to match(/^6.50\z/) end end end it 'displays the rate when passed the "%r" format flag' do @@ -628,11 +628,11 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 0) progressbar.start progressbar.progress += 20 Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^10\z/ + expect(progressbar.to_s('%r')).to match(/^10\z/) end end end it 'displays the rate when passed the "%R" format flag' do @@ -640,41 +640,41 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 0) progressbar.start progressbar.progress += 10 Timecop.travel(6) do - expect(progressbar.to_s('%R')).to match /^1.67\z/ + expect(progressbar.to_s('%R')).to match(/^1.67\z/) end end end end context 'and the a custom rate is applied' do it 'displays zero for %r if no progress has been made' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20, :rate_scale => lambda { |rate| rate / 2 }) Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^0\z/ + expect(progressbar.to_s('%r')).to match(/^0\z/) end end it 'displays zero for %R if no progress has been made' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20, :rate_scale => lambda { |rate| rate / 2 }) Timecop.travel(2) do - expect(progressbar.to_s('%R')).to match /^0.00\z/ + expect(progressbar.to_s('%R')).to match(/^0.00\z/) end end it 'takes into account the starting position when calculating %r' do Timecop.freeze do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20, :rate_scale => lambda { |rate| rate / 2 }) progressbar.start progressbar.progress += 20 Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^5\z/ + expect(progressbar.to_s('%r')).to match(/^5\z/) end end end it 'takes into account the starting position when calculating %R' do @@ -682,11 +682,11 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20, :rate_scale => lambda { |rate| rate / 2 }) progressbar.start progressbar.progress += 13 Timecop.travel(2) do - expect(progressbar.to_s('%R')).to match /^3.25\z/ + expect(progressbar.to_s('%R')).to match(/^3.25\z/) end end end it 'displays the rate when passed the "%r" format flag' do @@ -694,11 +694,11 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 0, :rate_scale => lambda { |rate| rate / 2 }) progressbar.start progressbar.progress += 20 Timecop.travel(2) do - expect(progressbar.to_s('%r')).to match /^5\z/ + expect(progressbar.to_s('%r')).to match(/^5\z/) end end end it 'displays the rate when passed the "%R" format flag' do @@ -706,147 +706,147 @@ progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 0, :rate_scale => lambda { |rate| rate / 2 }) progressbar.start progressbar.progress += 10 Timecop.travel(6) do - expect(progressbar.to_s('%R')).to match /^0.83\z/ + expect(progressbar.to_s('%R')).to match(/^0.83\z/) end end end end end it 'displays the title when passed the "%t" format flag' do - expect(progressbar.to_s('%t')).to match /^Progress\z/ + expect(progressbar.to_s('%t')).to match(/^Progress\z/) end it 'displays the title when passed the "%T" format flag' do - expect(progressbar.to_s('%T')).to match /^Progress\z/ + expect(progressbar.to_s('%T')).to match(/^Progress\z/) end it 'displays the bar when passed the "%B" format flag (including empty space)' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) - expect(progressbar.to_s('%B')).to match /^#{'=' * 20}#{' ' * 80}\z/ + expect(progressbar.to_s('%B')).to match(/^#{'=' * 20}#{' ' * 80}\z/) end it 'displays the bar when passed the combined "%b%i" format flags' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) - expect(progressbar.to_s('%b%i')).to match /^#{'=' * 20}#{' ' * 80}\z/ + expect(progressbar.to_s('%b%i')).to match(/^#{'=' * 20}#{' ' * 80}\z/) end it 'displays the bar when passed the "%b" format flag (excluding empty space)' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) - expect(progressbar.to_s('%b')).to match /^#{'=' * 20}\z/ + expect(progressbar.to_s('%b')).to match(/^#{'=' * 20}\z/) end it 'displays the incomplete space when passed the "%i" format flag' do progressbar = ProgressBar::Base.new(:length => 100, :starting_at => 20) - expect(progressbar.to_s('%i')).to match /^#{' ' * 80}\z/ + expect(progressbar.to_s('%i')).to match(/^#{' ' * 80}\z/) end it 'displays the bar when passed the "%w" format flag' do progressbar = ProgressBar::Base.new(:output => output, :length => 100, :starting_at => 0) - expect(progressbar.to_s('%w')).to match /^\z/ + expect(progressbar.to_s('%w')).to match(/^\z/) 4.times { progressbar.increment } - expect(progressbar.to_s('%w')).to match /^====\z/ + expect(progressbar.to_s('%w')).to match(/^====\z/) progressbar.increment - expect(progressbar.to_s('%w')).to match /^= 5 =\z/ + expect(progressbar.to_s('%w')).to match(/^= 5 =\z/) 5.times { progressbar.increment } - expect(progressbar.to_s('%w')).to match /^=== 10 ===\z/ + expect(progressbar.to_s('%w')).to match(/^=== 10 ===\z/) progressbar.decrement - expect(progressbar.to_s('%w')).to match /^=== 9 ===\z/ + expect(progressbar.to_s('%w')).to match(/^=== 9 ===\z/) 91.times { progressbar.increment } - expect(progressbar.to_s('%w')).to match /^#{'=' * 47} 100 #{'=' * 48}\z/ + expect(progressbar.to_s('%w')).to match(/^#{'=' * 47} 100 #{'=' * 48}\z/) end it 'calculates the remaining negative space properly with an integrated percentage bar of 0 percent' do progressbar = ProgressBar::Base.new(:output => output, :length => 100, :total => 200, :starting_at => 0) - expect(progressbar.to_s('%w%i')).to match /^\s{100}\z/ + expect(progressbar.to_s('%w%i')).to match(/^\s{100}\z/) 9.times { progressbar.increment } - expect(progressbar.to_s('%w%i')).to match /^====\s{96}\z/ + 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/ + 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/ + 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/ + 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/ + expect(progressbar.to_s('%c')).to match(/^0\z/) progressbar.increment - expect(progressbar.to_s('%c')).to match /^1\z/ + expect(progressbar.to_s('%c')).to match(/^1\z/) progressbar.decrement - expect(progressbar.to_s('%c')).to match /^0\z/ + expect(progressbar.to_s('%c')).to match(/^0\z/) end it 'displays the total capacity when passed the "%C" format flag' do progressbar = ProgressBar::Base.new(:total => 100) - expect(progressbar.to_s('%C')).to match /^100\z/ + expect(progressbar.to_s('%C')).to match(/^100\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\z/ + 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/ + 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/ + 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/ + 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) - expect(progressbar.to_s('%P')).to match /^66.66\z/ + expect(progressbar.to_s('%P')).to match(/^66.66\z/) end it 'displays a literal percent sign when using the "%%" flag' do progressbar = ProgressBar::Base.new(:starting_at => 66, :total => 99) - expect(progressbar.to_s('%%')).to match /^%\z/ + expect(progressbar.to_s('%%')).to match(/^%\z/) end it 'displays a literal percent sign when using the "%%" flag' do progressbar = ProgressBar::Base.new(:starting_at => 66, :total => 99) - expect(progressbar.to_s('%%')).to match /^%\z/ + expect(progressbar.to_s('%%')).to match(/^%\z/) end # Autostarting for now. This will be applicable later. # context "when called before #start" do # it "displays unknown time elapsed when using the %a flag" do - # expect(progressbar.to_s('%a')).to match /^Time: --:--:--\z/ + # expect(progressbar.to_s('%a')).to match(/^Time: --:--:--\z/) # end # end context 'when called after #start' do before do @@ -857,23 +857,23 @@ context 'and the bar is reset' do before { progressbar.reset } it 'displays "??:??:??" until finished when passed the %e flag' do - expect(progressbar.to_s('%a')).to match /^Time: --:--:--\z/ + expect(progressbar.to_s('%a')).to match(/^Time: --:--:--\z/) end end it 'displays the time elapsed when using the "%a" flag' do - expect(progressbar.to_s('%a')).to match /^Time: 01:02:03\z/ + expect(progressbar.to_s('%a')).to match(/^Time: 01:02:03\z/) end end context 'when called before #start' do it 'displays unknown time until finished when passed the "%e" flag' do progressbar = ProgressBar::Base.new - expect(progressbar.to_s('%e')).to match /^ ETA: \?\?:\?\?:\?\?\z/ + expect(progressbar.to_s('%e')).to match(/^ ETA: \?\?:\?\?:\?\?\z/) end end context 'when called after #start' do let(:progressbar) do @@ -887,16 +887,16 @@ context 'and the bar is reset' do before { progressbar.reset } it 'displays "??:??:??" until finished when passed the "%e" flag' do - expect(progressbar.to_s('%e')).to match /^ ETA: \?\?:\?\?:\?\?\z/ + expect(progressbar.to_s('%e')).to match(/^ ETA: \?\?:\?\?:\?\?\z/) end end it 'displays the estimated time remaining when using the "%e" flag' do - expect(progressbar.to_s('%e')).to match /^ ETA: 01:02:03\z/ + expect(progressbar.to_s('%e')).to match(/^ ETA: 01:02:03\z/) end end context 'when it could take 100 hours or longer to finish' do let(:progressbar) do @@ -907,18 +907,18 @@ progressbar end end it 'displays "> 4 Days" until finished when passed the "%E" flag' do - expect(progressbar.to_s('%E')).to match /^ ETA: > 4 Days\z/ + expect(progressbar.to_s('%E')).to match(/^ ETA: > 4 Days\z/) end it 'displays "??:??:??" until finished when passed the "%e" flag' do - expect(progressbar.to_s('%e')).to match /^ ETA: \?\?:\?\?:\?\?\z/ + expect(progressbar.to_s('%e')).to match(/^ ETA: \?\?:\?\?:\?\?\z/) end it 'displays the exact estimated time until finished when passed the "%f" flag' do - expect(progressbar.to_s('%f')).to match /^ ETA: 100:00:00\z/ + expect(progressbar.to_s('%f')).to match(/^ ETA: 100:00:00\z/) end end end end