Skip to content

Commit

Permalink
Update session4 specs to RSpec3
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Dec 8, 2014
1 parent 259c376 commit 99a96c0
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 273 deletions.
62 changes: 29 additions & 33 deletions session4/spec/1.rb
Original file line number Diff line number Diff line change
@@ -1,42 1,38 @@
describe 'Stack#inspect' do

before :each do
@stack = Stack.new
end

it 'should be ()' do
@stack.inspect.should == "()"
RSpec.describe 'Stack#inspect' do
let(:stack) { Stack.new }

example 'initially is ()' do
expect(stack.inspect).to eq "()"
end

it 'should be (1)' do
@stack.push 1
@stack.inspect.should == '(1)'
example 'after pushing 1: (1)' do
stack.push 1
expect(stack.inspect).to eq '(1)'
end

it 'should be ({1=>2})' do
@stack.push({1=>2})
@stack.inspect.should == '({1=>2})'
example 'after pushing a hash: ({1=>2})' do
stack.push({1=>2})
expect(stack.inspect).to eq '({1=>2})'
end
it 'should be (3)2)1)' do
(1..3).each { |i| @stack.push i }
@stack.inspect.should == '(3)2)1)'

example 'after pushing 1, then 2, then 3: (3)2)1)' do
(1..3).each { |i| stack.push i }
expect(stack.inspect).to eq '(3)2)1)'
end
it 'should be (5)4)3)2)1)' do
(1..5).each { |i| @stack.push i }
@stack.inspect.should == "(5)4)3)2)1)"

example 'after pushing 1 through 5: (5)4)3)2)1)' do
(1..5).each { |i| stack.push i }
expect(stack.inspect).to eq "(5)4)3)2)1)"
end
it 'should be (/abc/)3)2)1)' do
(1..3).each { |i| @stack.push i }
@stack.push(/abc/)
@stack.inspect.should == "(/abc/)3)2)1)"

example 'after pushing 1 through 3, and then /abc/: (/abc/)3)2)1)' do
(1..3).each { |i| stack.push i }
stack.push(/abc/)
expect(stack.inspect).to eq "(/abc/)3)2)1)"
end
it 'should be ([1, 2, 3])"abc")true)false)nil)' do
[nil,false,true,"abc",[1,2,3]].each { |e| @stack.push e }
@stack.inspect.should == '([1, 2, 3])"abc")true)false)nil)'

example 'after pushing lotsa stuff: ([1, 2, 3])"abc")true)false)nil)' do
[nil, false, true, "abc", [1,2,3]].each { |e| stack.push e }
expect(stack.inspect).to eq '([1, 2, 3])"abc")true)false)nil)'
end

end
end
80 changes: 38 additions & 42 deletions session4/spec/2.rb
Original file line number Diff line number Diff line change
@@ -1,60 1,56 @@
describe 'context' do
RSpec.describe 'context' do
specify '1_stack_classes_inspect should have been required' do
path = File.dirname(__FILE__) "/../challenge/1_stack_classes_inspect.rb"
path = File.expand_path(path)
$LOADED_FEATURES.map { |filename| File.expand_path filename }.should include path
path = File.expand_path("../../challenge/1_stack_classes_inspect.rb", __FILE__)
expanded_loaded_features = $LOADED_FEATURES.map { |filename| File.expand_path filename }
expect(expanded_loaded_features).to include path
end
end

describe 'StackInDisguise' do

RSpec.describe 'StackInDisguise' do
specify 'it should be a subclass of Stack' do
StackInDisguise.superclass.should == Stack
expect(StackInDisguise.superclass).to eq Stack
end

describe '#inspect' do

before :each do
@stack = StackInDisguise.new
end

it 'should be overridden' do
@stack.method(:inspect).owner.should == StackInDisguise
let(:stack) { StackInDisguise.new }

it 'overrides inspect' do
expect(stack.method(:inspect).owner).to eq StackInDisguise
end
it 'should be []' do
@stack.inspect.should == "[]"

it 'inspects with square brackets []' do
expect(stack.inspect).to eq "[]"
end

it 'should be [1]' do
@stack.push 1
@stack.inspect.should == '[1]'
it 'after pushing 1: [1]' do
stack.push 1
expect(stack.inspect).to eq '[1]'
end

it 'should be [{1=>2}]' do
@stack.push({1=>2})
@stack.inspect.should == '[{1=>2}]'
it 'after pushing a hash: [{1=>2}]' do
stack.push({1=>2})
expect(stack.inspect).to eq '[{1=>2}]'
end
it 'should be [1, 2, 3]' do
(1..3).each { |i| @stack.push i }
@stack.inspect.should == '[1, 2, 3]'

it 'after pushing 1, then 2, then 3: [1, 2, 3]' do
(1..3).each { |i| stack.push i }
expect(stack.inspect).to eq '[1, 2, 3]'
end
it 'should be [1, 2, 3, 4, 5]' do
(1..5).each { |i| @stack.push i }
@stack.inspect.should == '[1, 2, 3, 4, 5]'

it 'after pushing 1 through 5: [1, 2, 3, 4, 5]' do
(1..5).each { |i| stack.push i }
expect(stack.inspect).to eq '[1, 2, 3, 4, 5]'
end
it 'should be [1, 2, 3, /abc/]' do
(1..3).each { |i| @stack.push i }
@stack.push(/abc/)
@stack.inspect.should == '[1, 2, 3, /abc/]'

it 'after pushing 1 through 3, and then /abc/: [1, 2, 3, /abc/]' do
(1..3).each { |i| stack.push i }
stack.push(/abc/)
expect(stack.inspect).to eq '[1, 2, 3, /abc/]'
end
it 'should be [nil, false, true, "abc", [1, 2, 3]]' do
[nil,false,true,"abc",[1,2,3]].each { |e| @stack.push e }
@stack.inspect.should == '[nil, false, true, "abc", [1, 2, 3]]'

it 'after pushing lotsa stuff [nil, false, true, "abc", [1, 2, 3]]' do
[nil, false, true, "abc", [1,2,3]].each { |e| stack.push e }
expect(stack.inspect).to eq '[nil, false, true, "abc", [1, 2, 3]]'
end
end
end
end
66 changes: 28 additions & 38 deletions session4/spec/3.rb
Original file line number Diff line number Diff line change
@@ -1,58 1,48 @@
describe 'passthrough' do

it 'should return what is passed in, if the enum is empty' do
passthrough( Array.new , :passed_in ) { |a,b| :from_block }.should == :passed_in
RSpec.describe 'passthrough' do
it 'returns what is passed in, if the enum is empty' do
result = passthrough([], :passed_in) { |a, b| :from_block }
expect(result).to eq :passed_in
end

it 'should return 45' do
result = passthrough 5..10 , 0 do |sum,num|
sum num
end
result.should == 45

it 'summing 5..10, starting at 0, returns 45' do
result = passthrough(5..10, 0) { |sum, num| sum num }
expect(result).to eq 45
end


it 'should pass in the expected params' do
expected_params = [
0 , 5 ,
5 , 6 ,
11 , 7 ,
18 , 8 ,
26 , 9 ,
35 , 10 ,
it 'summing 5..10 from 0 again, but checking params this time' do
expected_params = [
0, 5,
5, 6,
11, 7,
18, 8,
26, 9,
35, 10,
]
actual_params = Array.new
result = passthrough 5..10 , 0 do |sum,num|
actual_params = []
result = passthrough 5..10, 0 do |sum, num|
actual_params << sum << num
sum num
end
actual_params.should == expected_params
expect(actual_params).to eq expected_params
end


it 'should return {5=>["hound"], 6=>["around"], 3=>["the", "fox", "and", "the", "ran", "all"]}' do
result = passthrough %w(the fox and the hound ran all around) , Hash.new do |words,word|
it '"the fox and the hound ran all around", with words aggregated by length' do
result = passthrough %w(the fox and the hound ran all around), Hash.new do |words, word|
words[word.length] ||= Array.new
words[word.length] << word
words
end

result.should == { 5 => ["hound"] , 6 => ["around"] , 3 => ["the", "fox", "and", "the", "ran", "all"] }
expect(result).to eq 5 => ["hound"],
6 => ["around"],
3 => ["the", "fox", "and", "the", "ran", "all"]
end


it 'should return ["Will Jones", "Robert Jones", "John Smith", "Sally Smith"]' do
married_couples = { 'Smith' => ['John' , 'Sally'] , 'Jones' => ['Will','Robert'] }

people = passthrough married_couples , Array.new do |people,(last_name,first_names)|
it 'passing a hash in, and expanding it to names' do
married_couples = {'Smith' => ['John', 'Sally'], 'Jones' => ['Will', 'Robert']}
people = passthrough married_couples, Array.new do |people, (last_name, first_names)|
full_names = first_names.map { |first_name| "#{first_name} #{last_name}" }
people full_names
end

people.sort.should == ["Will Jones", "Robert Jones", "John Smith", "Sally Smith"].sort
expect(people).to eq ["John Smith", "Sally Smith", "Will Jones", "Robert Jones"]
end

end



29 changes: 18 additions & 11 deletions session4/spec/4.rb
Original file line number Diff line number Diff line change
@@ -1,12 1,19 @@
describe 'first_object' do
specify { first_object(1, nil, nil).should == 1 }
specify { first_object(nil, 1, nil).should == 1 }
specify { first_object(nil, nil, 1).should == 1 }
specify { first_object(nil, 1, 2).should == 1 }
specify { first_object(nil, nil, nil).should == nil }
specify { first_object(false, false, false).should == nil }
specify { first_object(false, false, true).should == true }
specify { first_object(5, 4, 3).should == 5 }
specify { first_object(5, nil, 3).should == 5 }
specify { first_object(5, 4, nil).should == 5 }
describe 'first_object' do
def self.assert_first(first, args)
example "first_object(#{args.map(&:inspect).join(", ")}) # => #{first.inspect}" do
obj = first_object(*args)
expect(obj).to eq first
end
end

assert_first 1, [1, nil, nil]
assert_first 1, [nil, 1, nil]
assert_first 1, [nil, nil, 1]
assert_first 1, [nil, 1, 2]
assert_first nil, [nil, nil, nil]
assert_first nil, [false, false, false]
assert_first true, [false, false, true]
assert_first 5, [5, 4, 3]
assert_first 5, [5, nil, 3]
assert_first 5, [5, 4, nil]
end
53 changes: 20 additions & 33 deletions session4/spec/5.rb
Original file line number Diff line number Diff line change
@@ -1,39 1,26 @@
require 'pathname'
require 'fileutils'
include FileUtils

def get_expected_answer(path)
{
'8.1' => 4399 ,
'8.2' => 0 ,
'8.3' => 4399 ,
'8.4' => 12 ,
'8.5' => 3 ,
'8.6' => 5 ,
'8.7' => 23793343701 ,
'8.8' => 880754605216 ,
'8.9' => 2987648672 ,
}.fetch path[%r(\d \.\d $)] do |problem|
raise "get_expected_answer generated #{problem} from #{path} but #{problem} was not in the expected answer list"
end
end



resource_dir = Pathname.new(__FILE__).parent.parent 'resources'


describe 'line_sums' do
Dir[ (resource_dir '*.template').to_s ].each do |template_path|

real_path = template_path.sub /\.template$/ , ''

cp template_path , real_path

it "should return #{get_expected_answer real_path} for #{real_path}" do
line_sums(real_path).should == get_expected_answer(real_path)
rm real_path
expected_answers = {
'8.1' => 4399,
'8.2' => 0,
'8.3' => 4399,
'8.4' => 12,
'8.5' => 3,
'8.6' => 5,
'8.7' => 23793343701,
'8.8' => 880754605216,
'8.9' => 2987648672,
}

template_pattern = File.expand_path "../../resources/*.template", __FILE__
Dir[template_pattern].each do |template_path|
real_path = template_path.sub /\.template$/, ''
FileUtils.cp template_path, real_path
expected_answer = expected_answers.fetch real_path[/(\d \.\d $)/]
example "#{real_path} sums to #{expected_answer}" do
expect(line_sums real_path).to eq expected_answer
FileUtils.rm real_path
end

end
end
Loading

0 comments on commit 99a96c0

Please sign in to comment.