diff --git a/00_hello/hello_spec.rb b/00_hello/hello_spec.rb index 3ffd5cce4..9c9acca4a 100644 --- a/00_hello/hello_spec.rb +++ b/00_hello/hello_spec.rb @@ -113,20 +113,24 @@ # "Hello, #{who}!" # end # + + require "hello" + + describe "the hello function" do it "says hello" do - hello.should == "Hello!" + expect( hello).to eq("Hello!") end end describe "the greet function" do - it "says hello to someone" do - greet("Alice").should == "Hello, Alice!" + it "says hello to Alice:" do + expect(greet("Alice")).to eq("Hello, Alice!") end - it "says hello to someone else" do - greet("Bob").should == "Hello, Bob!" + it "says hello to Bob" do + expect(greet("Bob")).to eq( "Hello, Bob!") end end diff --git a/01_temperature/temperature_spec.rb b/01_temperature/temperature_spec.rb index 6cad3701c..6064b6e70 100644 --- a/01_temperature/temperature_spec.rb +++ b/01_temperature/temperature_spec.rb @@ -22,19 +22,19 @@ describe "#ftoc" do it "converts freezing temperature" do - ftoc(32).should == 0 + expect(ftoc(32)).to eq( 0 ) end it "converts boiling temperature" do - ftoc(212).should == 100 + expect(ftoc(212)).to eq( 100 ) end it "converts body temperature" do - ftoc(98.6).should == 37 + expect( ftoc(98.6)).to eq( 37 ) end it "converts arbitrary temperature" do - ftoc(68).should == 20 + expect( ftoc(68)).to eq( 20 ) end end @@ -42,19 +42,19 @@ describe "#ctof" do it "converts freezing temperature" do - ctof(0).should == 32 + expect( ctof(0)).to eq( 32 ) end it "converts boiling temperature" do - ctof(100).should == 212 + expect( ctof(100)).to eq( 212 ) end it "converts arbitrary temperature" do - ctof(20).should == 68 + expect( ctof(20)).to eq( 68 ) end it "converts body temperature" do - ctof(37).should be_within(0.1).of(98.6) + expect( ctof(37)).to be_within(0.1).of(98.6) # Why do we need to use be_within? # See http://www.ruby-forum.com/topic/169330 # and http://en.wikipedia.org/wiki/IEEE_754-2008 diff --git a/02_calculator/calculator_spec.rb b/02_calculator/calculator_spec.rb index 29c91f64e..29cca8c0a 100644 --- a/02_calculator/calculator_spec.rb +++ b/02_calculator/calculator_spec.rb @@ -37,39 +37,40 @@ describe "add" do it "adds 0 and 0" do - add(0,0).should == 0 + expect(add(0,0)).to eq( 0 ) end it "adds 2 and 2" do - add(2,2).should == 4 + expect(add(2,2)).to eq( 4 ) + end it "adds positive numbers" do - add(2,6).should == 8 + expect(add(2,6)).to eq( 8 ) end end describe "subtract" do it "subtracts numbers" do - subtract(10,4).should == 6 + expect( subtract(10,4)).to eq( 6 ) end end describe "sum" do it "computes the sum of an empty array" do - sum([]).should == 0 + expect(sum([])).to eq( 0 ) end it "computes the sum of an array of one number" do - sum([7]).should == 7 + expect(sum([7])).to eq( 7 ) end it "computes the sum of an array of two numbers" do - sum([7,11]).should == 18 + expect(sum([7,11])).to eq( 18 ) end it "computes the sum of an array of many numbers" do - sum([1,3,5,7,9]).should == 25 + expect(sum([1,3,5,7,9])).to eq( 25 ) end end diff --git a/03_simon_says/simon_says_spec.rb b/03_simon_says/simon_says_spec.rb index 8dac0b9f0..d324d25b9 100644 --- a/03_simon_says/simon_says_spec.rb +++ b/03_simon_says/simon_says_spec.rb @@ -16,27 +16,27 @@ describe "Simon says" do describe "echo" do it "should echo hello" do - echo("hello").should == "hello" + expect(echo("hello")).to eq( "hello") end it "should echo bye" do - echo("bye").should == "bye" + expect( echo("bye")).to eq( "bye") end end describe "shout" do it "should shout hello" do - shout("hello").should == "HELLO" + expect(shout("hello")).to eq("HELLO") end it "should shout multiple words" do - shout("hello world").should == "HELLO WORLD" + expect(shout("hello world")).to eq( "HELLO WORLD") end end describe "repeat" do it "should repeat" do - repeat("hello").should == "hello hello" + expect( repeat("hello")).to eq( "hello hello") end # Wait a second! How can you make the "repeat" method diff --git a/04_pig_latin/pig_latin_spec.rb b/04_pig_latin/pig_latin_spec.rb index 86328f2a4..6981173ed 100644 --- a/04_pig_latin/pig_latin_spec.rb +++ b/04_pig_latin/pig_latin_spec.rb @@ -23,46 +23,46 @@ it "translates a word beginning with a vowel" do s = translate("apple") - s.should == "appleay" + expect(s).to eq( "appleay") end it "translates a word beginning with a consonant" do s = translate("banana") - s.should == "ananabay" + expect(s).to eq( "ananabay") end it "translates a word beginning with two consonants" do s = translate("cherry") - s.should == "errychay" + expect(s).to eq( "errychay") end it "translates two words" do s = translate("eat pie") - s.should == "eatay iepay" + expect(s).to eq( "eatay iepay") end it "translates a word beginning with three consonants" do - translate("three").should == "eethray" + expect(translate("three")).to eq( "eethray") end it "counts 'sch' as a single phoneme" do s = translate("school") - s.should == "oolschay" + expect(s).to eq( "oolschay") end it "counts 'qu' as a single phoneme" do s = translate("quiet") - s.should == "ietquay" + expect(s).to eq( "ietquay") end it "counts 'qu' as a consonant even when it's preceded by a consonant" do s = translate("square") - s.should == "aresquay" + expect(s).to eq( "aresquay") end it "translates many words" do s = translate("the quick brown fox") - s.should == "ethay ickquay ownbray oxfay" + expect(s).to eq( "ethay ickquay ownbray oxfay") end # Test-driving bonus: diff --git a/05_silly_blocks/silly_blocks_spec.rb b/05_silly_blocks/silly_blocks_spec.rb index 6a34b2cff..fec25a2d1 100644 --- a/05_silly_blocks/silly_blocks_spec.rb +++ b/05_silly_blocks/silly_blocks_spec.rb @@ -15,28 +15,27 @@ result = reverser do "hello" end - result.should == "olleh" + expect(result).to eq( "olleh") end it "reverses each word in the string returned by the default block" do result = reverser do "hello dolly" end - result.should == "olleh yllod" + expect(result).to eq( "olleh yllod") end end describe "adder" do it "adds one to the value returned by the default block" do - adder do - 5 - end.should == 6 + expect(adder { 5 } + ).to eq( 6) end it "adds 3 to the value returned by the default block" do - adder(3) do + expect(adder(3) do 5 - end.should == 8 + end).to eq( 8) end end @@ -46,7 +45,7 @@ repeater do block_was_executed = true end - block_was_executed.should == true + expect(block_was_executed).to eq( true) end it "executes the default block 3 times" do @@ -54,7 +53,7 @@ repeater(3) do n += 1 end - n.should == 3 + expect(n).to eq(3) end it "executes the default block 10 times" do @@ -62,7 +61,7 @@ repeater(10) do n += 1 end - n.should == 10 + expect(n).to eq(10) end end diff --git a/06_performance_monitor/performance_monitor_spec.rb b/06_performance_monitor/performance_monitor_spec.rb index 04820d4f7..6373a4e40 100644 --- a/06_performance_monitor/performance_monitor_spec.rb +++ b/06_performance_monitor/performance_monitor_spec.rb @@ -1,16 +1,24 @@ # # Topics # # * stubs +# -what are stubs? [Rspec Documentation]( https://github.com/rspec/rspec-mocks#method-stubs ) +# -Further Explanation: [Requisite StackOverflow]( http://stackoverflow.com/questions/14326000/what-exactly-does-time-stubnow-fake-time-do#14326239 ) +# # * blocks +# # * yield # +# # # Performance Monitor # # This is (a stripped down version of) an actual useful concept: a function that runs a block of code and then tells you how long it took to run. require "performance_monitor" -require "time" # loads up the Time.parse method -- do NOT create time.rb! +# loads up the Time.parse method -- do NOT create time.rb! +# http://ruby-doc.org/core-2.3.1/Time.html +require "time" + describe "Performance Monitor" do before do @@ -20,59 +28,67 @@ it "takes about 0 seconds to run an empty block" do elapsed_time = measure do end - elapsed_time.should be_within(0.1).of(0) + expect(elapsed_time).to be_within(0.1).of(0) end it "takes exactly 0 seconds to run an empty block (with stubs)" do - Time.stub(:now) { @eleven_am } + ## has been updated with Rspec 3. The now form syntax is: + # old: + # Time.stub(:now).and_return( @eleven_am ) + # new: + allow(Time).to receive(:now).and_return( @eleven_am) + + elapsed_time = measure do end - elapsed_time.should == 0 + expect(elapsed_time).to eql(0.0) end - it "takes about 1 second to run a block that sleeps for 1 second" do + it "takes about 0.1 second to run a block that sleeps for 0.1 second" do elapsed_time = measure do - sleep 1 + sleep 0.1 end - elapsed_time.should be_within(0.1).of(1) + expect(elapsed_time).to be_within(0.01).of(0.1) end it "takes exactly 1 second to run a block that sleeps for 1 second (with stubs)" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } + elapsed_time = measure do - fake_time += 60 # adds one minute to fake_time + fake_time += 60.0 # adds one minute to fake_time end - elapsed_time.should == 60 + expect(elapsed_time).to be_within(0.01).of( 60.0) end it "runs a block N times" do - n = 0 + @n = 0 measure(4) do - n += 1 + @n += 1 end - n.should == 4 + expect(@n).to eql(4) end it "returns the average time, not the total time, when running multiple times" do run_times = [8,6,5,7] - fake_time = @eleven_am - Time.stub(:now) { fake_time } + @fake_time = @eleven_am + allow(Time).to receive(:now) { @fake_time } + average_time = measure(4) do - fake_time += run_times.pop + @fake_time += run_times.pop end - average_time.should == 6.5 + expect(average_time).to eql( 6.5) end it "returns the average time when running a random number of times for random lengths of time" do fake_time = @eleven_am - Time.stub(:now) { fake_time } + allow(Time).to receive(:now) { fake_time } number_of_times = rand(10) + 2 average_time = measure(number_of_times) do delay = rand(10) fake_time += delay end - average_time.should == (fake_time - @eleven_am).to_f/number_of_times + expect(average_time).to eql((fake_time - @eleven_am).to_f/number_of_times) end end diff --git a/07_hello_friend/hello_friend_spec.rb b/07_hello_friend/hello_friend_spec.rb index 29b25730f..45f0a471e 100644 --- a/07_hello_friend/hello_friend_spec.rb +++ b/07_hello_friend/hello_friend_spec.rb @@ -92,10 +92,10 @@ describe Friend do it "says hello" do - Friend.new.greeting.should == "Hello!" + expect(Friend.new.greeting).to eql( "Hello!") end it "says hello to someone" do - Friend.new.greeting("Bob").should == "Hello, Bob!" + expect(Friend.new.greeting("Bob")).to eql("Hello, Bob!") end end diff --git a/08_book_titles/book_titles_spec.rb b/08_book_titles/book_titles_spec.rb index d7bc9a359..c7785fb81 100644 --- a/08_book_titles/book_titles_spec.rb +++ b/08_book_titles/book_titles_spec.rb @@ -23,52 +23,52 @@ describe 'title' do it 'should capitalize the first letter' do @book.title = "inferno" - @book.title.should == "Inferno" + expect(@book.title).to eql("Inferno") end it 'should capitalize every word' do @book.title = "stuart little" - @book.title.should == "Stuart Little" + expect(@book.title).to eql( "Stuart Little") end describe 'should capitalize every word except...' do describe 'articles' do specify 'the' do @book.title = "alexander the great" - @book.title.should == "Alexander the Great" + expect(@book.title).to eql("Alexander the Great") end specify 'a' do @book.title = "to kill a mockingbird" - @book.title.should == "To Kill a Mockingbird" + expect(@book.title).to eql( "To Kill a Mockingbird") end specify 'an' do @book.title = "to eat an apple a day" - @book.title.should == "To Eat an Apple a Day" + expect(@book.title).to eql( "To Eat an Apple a Day") end end specify 'conjunctions' do @book.title = "war and peace" - @book.title.should == "War and Peace" + expect(@book.title).to eql( "War and Peace") end specify 'prepositions' do @book.title = "love in the time of cholera" - @book.title.should == "Love in the Time of Cholera" + expect(@book.title).to eql( "Love in the Time of Cholera") end end describe 'should always capitalize...' do specify 'I' do @book.title = "what i wish i knew when i was 20" - @book.title.should == "What I Wish I Knew When I Was 20" + expect(@book.title).to eql( "What I Wish I Knew When I Was 20") end specify 'the first word' do @book.title = "the man in the iron mask" - @book.title.should == "The Man in the Iron Mask" + expect( @book.title).to eql( "The Man in the Iron Mask") end end end diff --git a/09_timer/timer_spec.rb b/09_timer/timer_spec.rb index d1baf90b3..5017ae2cb 100644 --- a/09_timer/timer_spec.rb +++ b/09_timer/timer_spec.rb @@ -15,28 +15,28 @@ end it "should initialize to 0 seconds" do - @timer.seconds.should == 0 + expect(@timer.seconds).to eql( 0.0) end describe 'time_string' do it "should display 0 seconds as 00:00:00" do @timer.seconds = 0 - @timer.time_string.should == "00:00:00" + expect(@timer.time_string).to eql("00:00:00") end it "should display 12 seconds as 00:00:12" do @timer.seconds = 12 - @timer.time_string.should == "00:00:12" + expect(@timer.time_string).to eql( "00:00:12") end it "should display 66 seconds as 00:01:06" do @timer.seconds = 66 - @timer.time_string.should == "00:01:06" + expect(@timer.time_string).to eql( "00:01:06") end it "should display 4000 seconds as 01:06:40" do @timer.seconds = 4000 - @timer.time_string.should == "01:06:40" + expect( @timer.time_string).to eql( "01:06:40") end end diff --git a/10_temperature_object/temperature_object_spec.rb b/10_temperature_object/temperature_object_spec.rb index fe21c1135..f2da8c756 100644 --- a/10_temperature_object/temperature_object_spec.rb +++ b/10_temperature_object/temperature_object_spec.rb @@ -4,11 +4,14 @@ # * constructors # * class methods # * factory methods -# * options hashes +# +# * [Options Hashes] ( http://stackoverflow.com/questions/1515577/factory-methods-in-ruby#1516824 ) +# # # # Hints # # Remember that one degree fahrenheit is 5/9 of one degree celsius, and that the freezing point of water is 0 degrees celsius but 32 degrees fahrenheit. +# (and that you probably wrote those methods in assignment 1) # # Remember to define the `from_celsius` factory method as a *class* method, not an *instance* method. # @@ -24,44 +27,44 @@ describe "can be constructed with an options hash" do describe "in degrees fahrenheit" do it "at 50 degrees" do - Temperature.new(:f => 50).in_fahrenheit.should == 50 + expect(Temperature.new(:f => 50).in_fahrenheit).to be_within(0.01).of( 50.0) end describe "and correctly convert to celsius" do it "at freezing" do - Temperature.new(:f => 32).in_celsius.should == 0 + expect(Temperature.new(:f => 32).in_celsius).to be_within(0.01).of( 0.0) end it "at boiling" do - Temperature.new(:f => 212).in_celsius.should == 100 + expect(Temperature.new(:f => 212).in_celsius).to be_within(0.01).of( 100.0) end it "at body temperature" do - Temperature.new(:f => 98.6).in_celsius.should == 37 + expect(Temperature.new(:f => 98.6).in_celsius).to be_within(0.01).of( 37.0) end it "at an arbitrary temperature" do - Temperature.new(:f => 68).in_celsius.should == 20 + expect(Temperature.new(:f => 68).in_celsius).to be_within(0.01).of( 20); end end end describe "in degrees celsius" do it "at 50 degrees" do - Temperature.new(:c => 50).in_celsius.should == 50 + expect(Temperature.new(:c => 50).in_celsius).to be_within(0.01).of( 50); end describe "and correctly convert to fahrenheit" do it "at freezing" do - Temperature.new(:c => 0).in_fahrenheit.should == 32 + expect(Temperature.new(:c => 0).in_fahrenheit).to be_within(0.01).of( 32); end it "at boiling" do - Temperature.new(:c => 100).in_fahrenheit.should == 212 + expect(Temperature.new(:c => 100).in_fahrenheit).to be_within(0.01).of( 212 ); end it "at body temperature" do - Temperature.new(:c => 37).in_fahrenheit.should be_within(0.1).of(98.6) + expect( Temperature.new(:c => 37).in_fahrenheit).to be_within(0.01).of(98.6) # Why do we need to use be_within here? # See http://www.ruby-forum.com/topic/169330 # and http://groups.google.com/group/rspec/browse_thread/thread/f3ebbe3c313202bb @@ -78,13 +81,13 @@ describe "can be constructed via factory methods" do it "in degrees celsius" do - Temperature.from_celsius(50).in_celsius.should == 50 - Temperature.from_celsius(50).in_fahrenheit.should == 122 + expect(Temperature.from_celsius(50).in_celsius).to be_within(0.01).of( 50) + expect(Temperature.from_celsius(50).in_fahrenheit).to be_within(0.01).of( 122); end it "in degrees fahrenheit" do - Temperature.from_fahrenheit(50).in_fahrenheit.should == 50 - Temperature.from_fahrenheit(50).in_celsius.should == 10 + expect( Temperature.from_fahrenheit(50).in_fahrenheit).to be_within(0.01).of( 50); + expect( Temperature.from_fahrenheit(50).in_celsius).to be_within(0.01).of( 10); end end @@ -104,23 +107,23 @@ describe "Temperature subclasses" do describe "Celsius subclass" do it "is constructed in degrees celsius" do - Celsius.new(50).in_celsius.should == 50 - Celsius.new(50).in_fahrenheit.should == 122 + expect(Celsius.new(50).in_celsius).to be_within(0.01).of( 50) + expect(Celsius.new(50).in_fahrenheit).to be_within(0.01).of( 122); end it "is a Temperature subclass" do - Celsius.new(0).should be_a(Temperature) + expect(Celsius.new(0)).to be_a(Temperature) end end describe "Fahrenheit subclass" do it "is constructed in degrees fahrenheit" do - Fahrenheit.new(50).in_fahrenheit.should == 50 - Fahrenheit.new(50).in_celsius.should == 10 + expect(Fahrenheit.new(50).in_fahrenheit).to be_within(0.01).of( 50) + expect(Fahrenheit.new(50).in_celsius).to be_within(0.01).of( 10) end it "is a Temperature subclass" do - Fahrenheit.new(0).should be_a(Temperature) + expect(Fahrenheit.new(0)).to be_a(Temperature) end end end diff --git a/11_dictionary/dictionary_spec.rb b/11_dictionary/dictionary_spec.rb index 85f147ccf..a03ce6bc7 100644 --- a/11_dictionary/dictionary_spec.rb +++ b/11_dictionary/dictionary_spec.rb @@ -5,6 +5,8 @@ # * instance variables # * regular expressions # +# N.B.: Rspec has changed how it expresses matching: +# [Matchers] ( https://relishapp.com/rspec/rspec-expectations/v/3-5/docs/built-in-matchers/be-matchers ) require 'dictionary' @@ -14,70 +16,72 @@ end it 'is empty when created' do - @d.entries.should == {} + expect(@d.entries).to eql( {} ) end it 'can add whole entries with keyword and definition' do @d.add('fish' => 'aquatic animal') - @d.entries.should == {'fish' => 'aquatic animal'} - @d.keywords.should == ['fish'] + expect(@d.keywords).to eql(['fish']) + expect(@d.entries).to eql({'fish' => 'aquatic animal'}) end it 'add keywords (without definition)' do @d.add('fish') - @d.entries.should == {'fish' => nil} - @d.keywords.should == ['fish'] + expect(@d.keywords).to eql(['fish']) + expect(@d.entries).to eql({'fish' => nil}) + end it 'can check whether a given keyword exists' do - @d.include?('fish').should be_false + expect(@d.include?('fish')).to be_falsey end it "doesn't cheat when checking whether a given keyword exists" do - @d.include?('fish').should be_false # if the method is empty, this test passes with nil returned + expect(@d.include?('fish')).to be_falsey # if the method is empty, this test passes with nil returned @d.add('fish') - @d.include?('fish').should be_true # confirms that it actually checks - @d.include?('bird').should be_false # confirms not always returning true after add + expect(@d.include?('fish')).to be # confirms that it actually checks + expect(@d.include?('bird')).to be_falsey # confirms not always returning true after add end it "doesn't include a prefix that wasn't added as a word in and of itself" do @d.add('fish') - @d.include?('fi').should be_false + expect(@d.include?('fi')).to be_falsey end it "doesn't find a word in empty dictionary" do - @d.find('fi').should be_empty # {} + expect(@d.find('fi')).to contain_exactly + #'contain_excactly' implicitly matches against an empty set ... {} end it 'finds nothing if the prefix matches nothing' do @d.add('fiend') @d.add('great') - @d.find('nothing').should be_empty + expect(@d.find('nothing')).to contain_exactly #see above end it "finds an entry" do @d.add('fish' => 'aquatic animal') - @d.find('fish').should == {'fish' => 'aquatic animal'} + expect(@d.find('fish')).to eql({'fish' => 'aquatic animal'}) end it 'finds multiple matches from a prefix and returns the entire entry (keyword + definition)' do @d.add('fish' => 'aquatic animal') @d.add('fiend' => 'wicked person') @d.add('great' => 'remarkable') - @d.find('fi').should == {'fish' => 'aquatic animal', 'fiend' => 'wicked person'} + expect(@d.find('fi')).to eql({'fish' => 'aquatic animal', 'fiend' => 'wicked person'}) end it 'lists keywords alphabetically' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.keywords.should == %w(apple fish zebra) + expect(@d.keywords).to eql( %w(apple fish zebra)) end it 'can produce printable output like so: [keyword] "definition"' do @d.add('zebra' => 'African land animal with stripes') @d.add('fish' => 'aquatic animal') @d.add('apple' => 'fruit') - @d.printable.should == %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"} + expect(@d.printable).to eql( %Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"} ) end end diff --git a/12_rpn_calculator/rpn_calculator_spec.rb b/12_rpn_calculator/rpn_calculator_spec.rb index 0b57f136b..b6e25be8a 100644 --- a/12_rpn_calculator/rpn_calculator_spec.rb +++ b/12_rpn_calculator/rpn_calculator_spec.rb @@ -2,6 +2,7 @@ # * arrays # * arithmetic # * strings +# * [ Exceptions ] ( http://rubylearning.com/satishtalim/ruby_exceptions.html ) # # # RPN Calculator # @@ -47,7 +48,7 @@ calculator.push(2) calculator.push(3) calculator.plus - calculator.value.should == 5 + expect(calculator.value).to eq(5) end it "adds three numbers" do @@ -55,16 +56,16 @@ calculator.push(3) calculator.push(4) calculator.plus - calculator.value.should == 7 + expect(calculator.value).to eq( 7) calculator.plus - calculator.value.should == 9 + expect(calculator.value).to eq( 9) end it "subtracts the second number from the first number" do calculator.push(2) calculator.push(3) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq( -1) end it "adds and subtracts" do @@ -72,9 +73,9 @@ calculator.push(3) calculator.push(4) calculator.minus - calculator.value.should == -1 + expect(calculator.value).to eq( -1) calculator.plus - calculator.value.should == 1 + expect(calculator.value).to eq( 1) end it "multiplies and divides" do @@ -82,27 +83,31 @@ calculator.push(3) calculator.push(4) calculator.divide - calculator.value.should == (3.0 / 4.0) + expect(calculator.value).to eq(3.0 / 4.0) calculator.times - calculator.value.should == 2.0 * (3.0 / 4.0) + expect(calculator.value).to eq(2.0 * (3.0 / 4.0)) end - it "resolves operator precedence unambiguously" do - # 1 2 + 3 * => (1 + 2) * 3 - calculator.push(1) - calculator.push(2) - calculator.plus - calculator.push(3) - calculator.times - calculator.value.should == (1+2)*3 - - # 1 2 3 * + => 1 + (2 * 3) - calculator.push(1) - calculator.push(2) - calculator.push(3) - calculator.times - calculator.plus - calculator.value.should == 1+(2*3) + describe "resolves operator precedence unambiguously" do + it "calculates first ordering" do + # 1 2 + 3 * => (1 + 2) * 3 + calculator.push(1) + calculator.push(2) + calculator.plus + calculator.push(3) + calculator.times + expect(calculator.value).to eq( (1+2)*3) + end + + it "calculates second ordering" do + # 1 2 3 * + => 1 + (2 * 3) + calculator.push(1) + calculator.push(2) + calculator.push(3) + calculator.times + calculator.plus + expect(calculator.value).to eq( 1+(2*3)) + end end it "fails informatively when there's not enough values stacked away" do @@ -125,23 +130,18 @@ # extra credit it "tokenizes a string" do - calculator.tokens("1 2 3 * + 4 5 - /").should == - [1, 2, 3, :*, :+, 4, 5, :-, :/] + expect(calculator.tokens("1 2 3 * + 4 5 - /")).to eq( [1, 2, 3, :*, :+, 4, 5, :-, :/]) end # extra credit it "evaluates a string" do - calculator.evaluate("1 2 3 * +").should == - ((2 * 3) + 1) + expect(calculator.evaluate("1 2 3 * +")).to eq((2 * 3) + 1) - calculator.evaluate("4 5 -").should == - (4 - 5) + expect(calculator.evaluate("4 5 -")).to eq( 4 - 5) - calculator.evaluate("2 3 /").should == - (2.0 / 3.0) + expect(calculator.evaluate("2 3 /")).to eq( 2.0 / 3.0) - calculator.evaluate("1 2 3 * + 4 5 - /").should == - (1.0 + (2 * 3)) / (4 - 5) + expect(calculator.evaluate("1 2 3 * + 4 5 - /")).to eq( (1.0 + (2 * 3)) / (4 - 5) ) end end diff --git a/13_xml_document/xml_document_spec.rb b/13_xml_document/xml_document_spec.rb index 88ce3ae69..2f45f7ee9 100644 --- a/13_xml_document/xml_document_spec.rb +++ b/13_xml_document/xml_document_spec.rb @@ -22,56 +22,57 @@ end it "renders an empty tag" do - @xml.hello.should == "" + expect(@xml.hello).to eql("") end it "renders a tag with attributes" do - @xml.hello(:name => 'dolly').should == "" + expect(@xml.hello(:name => 'dolly')).to eql("") end it "renders a randomly named tag" do tag_name = (1..8).map{|i| ('a'..'z').to_a[rand(26)]}.join - @xml.send(tag_name).should == "<#{tag_name}/>" + expect( @xml.send(tag_name)).to eql("<#{tag_name}/>") end it "renders block with text inside" do - @xml.hello do + expect(@xml.hello do "dolly" - end.should == "dolly" + end).to eql( "dolly" ) end it "nests one level" do - @xml.hello do + expect( @xml.hello do @xml.goodbye - end.should == "" + end ).to eql("") end it "nests several levels" do xml = XmlDocument.new - xml.hello do + expect(xml.hello do xml.goodbye do xml.come_back do xml.ok_fine(:be => "that_way") end end - end.should == "" + end).to eql( "") end it "indents" do @xml = XmlDocument.new(true) - @xml.hello do - @xml.goodbye do - @xml.come_back do - @xml.ok_fine(:be => "that_way") + expect( + @xml.hello do + @xml.goodbye do + @xml.come_back do + @xml.ok_fine(:be => "that_way") + end end - end - end.should == - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "\n" + end).to eql( + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n") end end diff --git a/14_array_extensions/array_extensions_spec.rb b/14_array_extensions/array_extensions_spec.rb index a755ac899..d5fedd3d0 100755 --- a/14_array_extensions/array_extensions_spec.rb +++ b/14_array_extensions/array_extensions_spec.rb @@ -3,7 +3,7 @@ # # Topics # # * objects, methods, classes -# * reopening classes +# * [ reopening classes ] ( http://juixe.com/techknow/index.php/2007/01/17/reopening-ruby-classes-2/ ) # require "array_extensions" # we don't call it "array.rb" since that would be confusing @@ -12,25 +12,25 @@ describe "#sum" do it "has a #sum method" do - [].should respond_to(:sum) + expect([]).to respond_to(:sum) end it "should be 0 for an empty array" do - [].sum.should == 0 + expect([].sum).to eq(0) end it "should add all of the elements" do - [1,2,4].sum.should == 7 + expect([1,2,4].sum).to eq(7) end end describe '#square' do it "does nothing to an empty array" do - [].square.should == [] + expect([].square).to eq([]) end it "returns a new array containing the squares of each element" do - [1,2,3].square.should == [1,4,9] + expect([1,2,3].square).to eq([1,4,9]) end end @@ -38,7 +38,7 @@ it "squares each element of the original array" do array = [1,2,3] array.square! - array.should == [1,4,9] + expect(array).to eq([1,4,9]) end end diff --git a/15_in_words/in_words_spec.rb b/15_in_words/in_words_spec.rb index d9872e767..2360434d1 100644 --- a/15_in_words/in_words_spec.rb +++ b/15_in_words/in_words_spec.rb @@ -23,80 +23,80 @@ describe Fixnum do it "reads 0 to 9" do - 0.in_words.should == 'zero' - 1.in_words.should == 'one' - 2.in_words.should == 'two' - 3.in_words.should == 'three' - 4.in_words.should == 'four' - 5.in_words.should == 'five' - 6.in_words.should == 'six' - 7.in_words.should == 'seven' - 8.in_words.should == 'eight' - 9.in_words.should == 'nine' + expect(0.in_words).to eq( 'zero') + expect(1.in_words).to eq( 'one') + expect(2.in_words).to eq( 'two') + expect(3.in_words).to eq( 'three') + expect(4.in_words).to eq( 'four') + expect(5.in_words).to eq( 'five') + expect(6.in_words).to eq( 'six') + expect(7.in_words).to eq( 'seven') + expect(8.in_words).to eq( 'eight') + expect(9.in_words).to eq( 'nine') end it "reads 10 to 12" do - 10.in_words.should == 'ten' - 11.in_words.should == 'eleven' - 12.in_words.should == 'twelve' + expect(10.in_words).to eq( 'ten') + expect(11.in_words).to eq( 'eleven') + expect(12.in_words).to eq( 'twelve') end it "reads teens" do - 13.in_words.should == 'thirteen' - 14.in_words.should == 'fourteen' - 15.in_words.should == 'fifteen' - 16.in_words.should == 'sixteen' - 17.in_words.should == 'seventeen' - 18.in_words.should == 'eighteen' - 19.in_words.should == 'nineteen' + expect(13.in_words).to eq( 'thirteen') + expect(14.in_words).to eq( 'fourteen') + expect(15.in_words).to eq( 'fifteen') + expect(16.in_words).to eq( 'sixteen') + expect(17.in_words).to eq( 'seventeen') + expect(18.in_words).to eq( 'eighteen') + expect(19.in_words).to eq( 'nineteen') end it "reads tens" do - 20.in_words.should == 'twenty' - 30.in_words.should == 'thirty' - 40.in_words.should == 'forty' - 50.in_words.should == 'fifty' - 60.in_words.should == 'sixty' - 70.in_words.should == 'seventy' - 80.in_words.should == 'eighty' - 90.in_words.should == 'ninety' + expect(20.in_words).to eq( 'twenty') + expect(30.in_words).to eq( 'thirty') + expect(40.in_words).to eq( 'forty') + expect(50.in_words).to eq( 'fifty') + expect(60.in_words).to eq( 'sixty') + expect(70.in_words).to eq( 'seventy') + expect(80.in_words).to eq( 'eighty') + expect(90.in_words).to eq( 'ninety') end it "reads various other numbers less than 100" do - 20.in_words.should == 'twenty' - 77.in_words.should == 'seventy seven' - 99.in_words.should == 'ninety nine' + expect(20.in_words).to eq( 'twenty') + expect(77.in_words).to eq( 'seventy seven') + expect(99.in_words).to eq( 'ninety nine') end it "reads hundreds" do - 100.in_words.should == 'one hundred' - 200.in_words.should == 'two hundred' - 300.in_words.should == 'three hundred' - 123.in_words.should == 'one hundred twenty three' - 777.in_words.should == 'seven hundred seventy seven' - 818.in_words.should == 'eight hundred eighteen' - 512.in_words.should == 'five hundred twelve' - 999.in_words.should == 'nine hundred ninety nine' + expect(100.in_words).to eq( 'one hundred') + expect(200.in_words).to eq( 'two hundred') + expect(300.in_words).to eq( 'three hundred') + expect(123.in_words).to eq( 'one hundred twenty three') + expect(777.in_words).to eq( 'seven hundred seventy seven') + expect(818.in_words).to eq( 'eight hundred eighteen') + expect(512.in_words).to eq( 'five hundred twelve') + expect(999.in_words).to eq( 'nine hundred ninety nine') end it "reads thousands" do - 1000.in_words.should == 'one thousand' - 32767.in_words.should == 'thirty two thousand seven hundred sixty seven' - 32768.in_words.should == 'thirty two thousand seven hundred sixty eight' + expect(1000.in_words).to eq( 'one thousand') + expect(32767.in_words).to eq( 'thirty two thousand seven hundred sixty seven') + expect(32768.in_words).to eq( 'thirty two thousand seven hundred sixty eight') end it "reads millions" do - 10_000_001.in_words.should == 'ten million one' + expect(10_000_001.in_words).to eq( 'ten million one') end it "reads billions" do - 1_234_567_890.in_words.should == 'one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety' + expect(1_234_567_890.in_words).to eq( 'one billion two hundred thirty four million five hundred sixty seven thousand eight hundred ninety') end it "reads trillions" do - 1_000_000_000_000.in_words.should == 'one trillion' - 1_000_000_000_001.in_words.should == 'one trillion one' - 1_888_259_040_036.in_words.should == 'one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six' + expect(1_000_000_000_000.in_words).to eq( 'one trillion') + expect(1_000_000_000_001.in_words).to eq( 'one trillion one') + expect(1_888_259_040_036.in_words).to eq( 'one trillion eight hundred eighty eight billion two hundred fifty nine million forty thousand thirty six') end end diff --git a/Gemfile b/Gemfile index 812f136e0..df14d5d60 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source "https://rubygems.org" gem "rake" -gem "rspec", "~>2.0" +gem "rspec", ">=3.0" diff --git a/Rakefile b/Rakefile index 22162469d..6781a663b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ # This Rakefile has all the right settings to run the tests inside each lab -gem 'rspec', '~>2' +gem 'rspec', '~>3' require 'rspec/core/rake_task' task :default => :spec