Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported code to use rspec 3 from rspec 2 #32

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions 00_hello/hello_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 deletions 01_temperature/temperature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,39 @@
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

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
Expand Down
17 changes: 9 additions & 8 deletions 02_calculator/calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions 03_simon_says/simon_says_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions 04_pig_latin/pig_latin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 9 additions & 10 deletions 05_silly_blocks/silly_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -46,23 +45,23 @@
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
n = 0
repeater(3) do
n += 1
end
n.should == 3
expect(n).to eq(3)
end

it "executes the default block 10 times" do
n = 0
repeater(10) do
n += 1
end
n.should == 10
expect(n).to eq(10)
end

end
Expand Down
54 changes: 35 additions & 19 deletions 06_performance_monitor/performance_monitor_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Loading