Skip to content

Commit

Permalink
upgrade rspec should notation to expect notation
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Handekyn committed Feb 18, 2018
1 parent 742ac25 commit 4e8cbd7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 91 deletions.
12 changes: 6 additions & 6 deletions examples/spec/prelude_lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
describe PreludeLambdaUsage, "basic lambda prelude usage" do

it "sums" do
SumOfSquares.([2, 3, 4]).should == 2*2+3*3+4*4
SumOf.(Square).([2, 3, 1]).should == 2*2+3*3+1*1
expect(SumOfSquares.([2, 3, 4])).to eq(2*2+3*3+4*4)
expect(SumOf.(Square).([2, 3, 1])).to eq(2*2+3*3+1*1)
end

it "averages" do
Average.([2, 3, 8]).should == 4
expect(Average.([2, 3, 8])).to eq(4)
end

it "flattens" do
Flatten.([[1, 2, 3], [2, 3]]).should == [1, 2, 3, 2, 3]
expect(Flatten.([[1, 2, 3], [2, 3]])).to eq([1, 2, 3, 2, 3])
end

it "folds" do
Foldl.(->(n, a) { n/a }, 1.0, [1.0, 2.0, 3.0]).should == 1.0/1.0/2.0/3.0
Foldr.(->(n, a) { n/a }, 1.0, [1.0, 2.0, 3.0]).should == 1.0/3.0/2.0/1.0
expect(Foldl.(->(n, a) { n/a }, 1.0, [1.0, 2.0, 3.0])).to eq(1.0/1.0/2.0/3.0)
expect(Foldr.(->(n, a) { n/a }, 1.0, [1.0, 2.0, 3.0])).to eq(1.0/3.0/2.0/1.0)
end

it "knows about GCD alternatives" do
Expand Down
108 changes: 54 additions & 54 deletions spec/prelude_enumerable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,122 @@
describe "enumerable" do

it "zip_map" do
[1,2,3].zip([2,3,4]).map { |a,b| a+b }.should eq([3,5,7])
[1,2,3].zip_map([2,3,4]) { |a,b| a+b }.should eq([3,5,7])
[1,2,3].zip_map([2,3,4],[0,1,0]) { |a,b,c| a+b+c }.should eq([3,6,7])
[1,2,3].zip_map([2,3,4]).with_index { |(a,b),n| a+b+n }.should eq([3,6,9])
expect([1,2,3].zip([2,3,4]).map { |a,b| a+b }).to eq([3,5,7])
expect([1,2,3].zip_map([2,3,4]) { |a,b| a+b }).to eq([3,5,7])
expect([1,2,3].zip_map([2,3,4],[0,1,0]) { |a,b,c| a+b+c }).to eq([3,6,7])
expect([1,2,3].zip_map([2,3,4]).with_index { |(a,b),n| a+b+n }).to eq([3,6,9])
end

it "transpose" do
[[1,2,3]].transpose.should eq([[1],[2],[3]])
[[1,2,3],[:a,:b,:c]].transpose.should eq([[1,:a],[2,:b],[3,:c]])
expect([[1,2,3]].transpose).to eq([[1],[2],[3]])
expect([[1,2,3],[:a,:b,:c]].transpose).to eq([[1,:a],[2,:b],[3,:c]])
end

it "zip, unzip" do
ns = [1,2,3]
as = [:a,:b,:c]
ns.zip(as).unzip.should eq([ns, as])
expect(ns.zip(as).unzip).to eq([ns, as])
end

it "split_in" do
[1,2,3].split_in(1).should eq([[1,2,3]])
[1,2,3].split_in(2).should eq([[1,2],[3]])
[1,2,3].split_in(3).should eq([[1],[2],[3]])
[1,2,3].split_in(4).should eq([[1],[2],[3],[]]) # do we want this ? length 3 ? or lenght 4 ?
[1,2,3].split_in_half.should eq([[1,2],[3]])
expect([1,2,3].split_in(1)).to eq([[1,2,3]])
expect([1,2,3].split_in(2)).to eq([[1,2],[3]])
expect([1,2,3].split_in(3)).to eq([[1],[2],[3]])
expect([1,2,3].split_in(4)).to eq([[1],[2],[3],[]]) # do we want this ? length 3 ? or lenght 4 ?
expect([1,2,3].split_in_half).to eq([[1,2],[3]])
end

it "merge" do
# starts with ordered sets
[1,3,5].merge([3,4,8]).should eq([1,3,3,4,5,8])
[1,3,5].merge([2]).should eq([1,2,3,5])
[].merge([1,2,3]).should eq([1,2,3])
[1,2,3].merge([]).should eq([1,2,3])
[3,2,1].merge([4,2,1]) { |a,b| a > b }.should eq([4,3,2,2,1,1])
expect([1,3,5].merge([3,4,8])).to eq([1,3,3,4,5,8])
expect([1,3,5].merge([2])).to eq([1,2,3,5])
expect([].merge([1,2,3])).to eq([1,2,3])
expect([1,2,3].merge([])).to eq([1,2,3])
expect([3,2,1].merge([4,2,1]) { |a,b| a > b }).to eq([4,3,2,2,1,1])
end

it "interleave" do
%w(sex druggs rock roll).interleave([", "," and "," & "]).join("").should eq("sex, druggs and rock & roll")
[3,1,4].interleave([:a,:d,:b]).should eq([3,:a,1,:d,4,:b])
[3,1,4].interleave([:a,:d]).should eq([3,:a,1,:d,4])
[3,1,4].interleave([:a]).should eq([3,:a,1,4])
[3,1,4].interleave([]).should eq([3,1,4])
expect(%w(sex druggs rock roll).interleave([", "," and "," & "]).join("")).to eq("sex, druggs and rock & roll")
expect([3,1,4].interleave([:a,:d,:b])).to eq([3,:a,1,:d,4,:b])
expect([3,1,4].interleave([:a,:d])).to eq([3,:a,1,:d,4])
expect([3,1,4].interleave([:a])).to eq([3,:a,1,4])
expect([3,1,4].interleave([])).to eq([3,1,4])
end

it "zip_hash" do
ab = {a: 'a', b: 'b'}
ad = {a: 'a', d: 'd'}
cb = {c: 'c', b: 'b'}
ab.zip_hash_left(ad).should eq({a: ['a','a'], b:['b',nil]})
ab.zip_hash_left(cb).should eq({a: ['a',nil], b:['b','b']})
ad.zip_hash_left(cb).should eq({a: ['a',nil], d:['d',nil]})
ab.zip_hash_inner(ad).should eq({a:['a','a']})
ab.zip_hash_inner(cb).should eq({b:['b','b']})
ad.zip_hash_inner(cb).should eq({})
expect(ab.zip_hash_left(ad)).to eq({a: ['a','a'], b:['b',nil]})
expect(ab.zip_hash_left(cb)).to eq({a: ['a',nil], b:['b','b']})
expect(ad.zip_hash_left(cb)).to eq({a: ['a',nil], d:['d',nil]})
expect(ab.zip_hash_inner(ad)).to eq({a:['a','a']})
expect(ab.zip_hash_inner(cb)).to eq({b:['b','b']})
expect(ad.zip_hash_inner(cb)).to eq({})
end

it "map_values" do
ab = {a: 1, b: 2}
ab.map_values { |x| x*2}.should == {a: 2, b: 4}
ab.map_values { |x| x.even? }.should == {a: false, b: true}
expect(ab.map_values { |x| x*2}).to eq({a: 2, b: 4})
expect(ab.map_values { |x| x.even? }).to eq({a: false, b: true})
end

it "map_values_recurse" do
abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
abcde.map_values_recurse { |x| x*2}.should == {a: 2, b: 4, c: { d: 2, e: 0}}
abcde.map_values_recurse { |x| x.even? }.should == {a: false, b: true, c: { d:false, e: true}}
expect(abcde.map_values_recurse { |x| x*2}).to eq({a: 2, b: 4, c: { d: 2, e: 0}})
expect(abcde.map_values_recurse { |x| x.even? }).to eq({a: false, b: true, c: { d:false, e: true}})
end

it "map_keys" do
ab = {a: 1, b: 2}
ab.map_keys { |k| k.to_s }.should == {"a" => 1, "b" => 2}
ab.map_keys { |k| k.to_s[0].ord }.should == { 97 => 1, 98 => 2 }
ab.map_keys { |k| k.to_s.length }.should == { 1 => 2 }
expect(ab.map_keys { |k| k.to_s }).to eq({"a" => 1, "b" => 2})
expect(ab.map_keys { |k| k.to_s[0].ord }).to eq({ 97 => 1, 98 => 2 })
expect(ab.map_keys { |k| k.to_s.length }).to eq({ 1 => 2 })
end

it "map_keys_recurse" do
abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
abcde.map_keys_recurse { |k| k.to_s }.should == {"a" => 1, "b" => 2, "c" => { "d" => 1, "e" => 0 }}
expect(abcde.map_keys_recurse { |k| k.to_s }).to eq({"a" => 1, "b" => 2, "c" => { "d" => 1, "e" => 0 }})
end

it "map_hash" do
ab = {a: 1, b: 2}
ab.map_hash { |k,v| [k.to_s, v*2] }.should == {"a" => 2, "b" => 4}
ab.map_hash { |k,v| [k.to_s[0].ord, v.even?] }.should == {97 => false, 98 => true}
ab.map_hash { |k,v| [k.to_s.length, v.even?] }.should == {1 => true}
expect(ab.map_hash { |k,v| [k.to_s, v*2] }).to eq({"a" => 2, "b" => 4})
expect(ab.map_hash { |k,v| [k.to_s[0].ord, v.even?] }).to eq({97 => false, 98 => true})
expect(ab.map_hash { |k,v| [k.to_s.length, v.even?] }).to eq({1 => true})
end

it "map_recursive" do
abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }
abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s, v] : [k.to_s, v*2] }.should == {"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }}
abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s[0].ord, v] : [k.to_s[0].ord, v.even?] }.should == {97 => false, 98 => true, 99 => { 100 => false, 101 => true }}
abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s.length, v] : [k.to_s.length, v.even?] }.should == {1 => true, 1 => { 1=>true } }
expect(abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s, v] : [k.to_s, v*2] }).to eq({"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }})
expect(abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s[0].ord, v] : [k.to_s[0].ord, v.even?] }).to eq({97 => false, 98 => true, 99 => { 100 => false, 101 => true }})
expect(abcde.map_recurse { |k,v| v.is_a?(Hash) ? [k.to_s.length, v] : [k.to_s.length, v.even?] }).to eq({1 => { 1=>true } })
end

it "map_keys_and_values" do

s = ->(x) { x.to_s }
s_ord = ->(x) { x.to_s[0].ord }
s_length = ->(x) { x.to_s.length }
times_2 = ->(x) { x * 2 }
even = ->(x) { x.even? }

abcde = {a: 1, b: 2, c: { d: 1, e: 0 } }

abcde.map_keys_and_values(s, times_2).should == {"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }}
abcde.map_keys_and_values(s_ord, even).should == {97 => false, 98 => true, 99 => { 100 => false, 101 => true }}
abcde.map_keys_and_values(s_length, even).should == {1 => true, 1 => { 1=>true } }
expect(abcde.map_keys_and_values(s, times_2)).to eq({"a" => 2, "b" => 4, "c" => { "d" => 2, "e" => 0 }})
expect(abcde.map_keys_and_values(s_ord, even)).to eq({97 => false, 98 => true, 99 => { 100 => false, 101 => true }})
expect(abcde.map_keys_and_values(s_length, even)).to eq({1 => { 1=>true } })

end

it "counted_set" do
[1,2,3,2,2,1,2].counted_set.should eq({1 => 2, 2 => 4, 3 => 1})
['a','b','a','d'].counted_set.should eq({'a' => 2, 'b' => 1, 'd' => 1})
expect([1,2,3,2,2,1,2].counted_set).to eq({1 => 2, 2 => 4, 3 => 1})
expect(['a','b','a','d'].counted_set).to eq({'a' => 2, 'b' => 1, 'd' => 1})
end

it "grouped_by" do
[1,2,3,2,2,1,2].grouped_by { |x| x.odd? }.should eq([[1,3,1],[2,2,2,2]])
%w(some words are longer then others).grouped_by { |x| x.length > 3 }.should eq([%w(some words longer then others),%w(are)])
expect([1,2,3,2,2,1,2].grouped_by { |x| x.odd? }).to eq([[1,3,1],[2,2,2,2]])
expect(%w(some words are longer then others).grouped_by { |x| x.length > 3 }).to eq([%w(some words longer then others),%w(are)])
end

it "multi_map" do
Expand All @@ -136,8 +136,8 @@
end

multiplied = folders.multi_map &multiply_folder
multiplied.length.should eq(2)
multiplied['main_1'].length.should eq(4)
expect(multiplied.length).to eq(2)
expect(multiplied['main_1'].length).to eq(4)

end
end
end
36 changes: 18 additions & 18 deletions spec/prelude_lambda_math_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@
describe Functions::Prelude, "math" do

it "divides" do
Divide.([9,2]).should eq(9/2)
Divide.([8,2,2]).should eq(8/2/2)
expect(Divide.([9,2])).to eq(9/2)
expect(Divide.([8,2,2])).to eq(8/2/2)
end

it "power" do
Power.(2,3).should eq(9)
Power.(0,3).should eq(1)
Power.(3,2).should eq(8)
expect(Power.(2,3)).to eq(9)
expect(Power.(0,3)).to eq(1)
expect(Power.(3,2)).to eq(8)
end

it "knows about divisors" do
IsDivisor.(8,2).should eq(true)
IsDivisor.(8,3).should eq(false)
Divisors.(12).sort.should eq([1,2,3,4,6,12])
expect(IsDivisor.(8,2)).to eq(true)
expect(IsDivisor.(8,3)).to eq(false)
expect(Divisors.(12).sort).to eq([1,2,3,4,6,12])
end

it "knows about range building" do
expect(FromOneTo.(3)).to eq(1..3)
FromOneTo.(3).to_a.should eq([1,2,3])
FromTo.(2).(8).to_a.should eq([2,3,4,5,6,7,8])
expect(FromOneTo.(3).to_a).to eq([1,2,3])
expect(FromTo.(2).(8).to_a).to eq([2,3,4,5,6,7,8])
end

it "knows about gcd" do
Gcd.(12,9).should eq(3)
Gcd.(4,8).should eq(4)
GcdA.([12,9,6]).should eq(3)
GcdA.([4,8,2]).should eq(2)
expect(Gcd.(12,9)).to eq(3)
expect(Gcd.(4,8)).to eq(4)
expect(GcdA.([12,9,6])).to eq(3)
expect(GcdA.([4,8,2])).to eq(2)
end

it "knows about lcm" do
Lcm.(12,9).should eq(36)
Lcm.(6,8).should eq(24)
LcmA.([12,9]).should eq(36)
LcmA.([12,9,2]).should eq(36)
expect(Lcm.(12,9)).to eq(36)
expect(Lcm.(6,8)).to eq(24)
expect(LcmA.([12,9])).to eq(36)
expect(LcmA.([12,9,2])).to eq(36)
end

end
Expand Down
24 changes: 12 additions & 12 deletions spec/prelude_lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

it "composes" do
sum_of_squares = Compose.(Sum).(Squares)
sum_of_squares.([2, 3, 4]).should eq(4+9+16)
expect(sum_of_squares.([2, 3, 4])).to eq(4+9+16)
end

it "has a compose operator" do
Expand All @@ -24,7 +24,7 @@

it "composes using after with implicit parallel" do
average = After.([Sum, Length]).(Divide)
average.([2, 3, 8]).should eq((2+3+8)/3)
expect(average.([2, 3, 8])).to eq((2+3+8)/3)
end

it "mixes parallel with after operator" do
Expand All @@ -34,26 +34,26 @@

it "mixes par with after operator" do
average = Par.([Sum, Length]) > Divide
average.([2, 3, 8]).should eq((2+3+8)/3)
expect(average.([2, 3, 8])).to eq((2+3+8)/3)
end

it "flattens arrays" do
Flatten.([[1, 2, 3], [2, 3]]).should eq([1,2,3,2,3])
expect(Flatten.([[1, 2, 3], [2, 3]])).to eq([1,2,3,2,3])
end

it "sorts arrays" do
QuickSort.([3,3,5,6,7,1,2]).should == [1,2,3,3,5,6,7]
QuickSort.([1,1,1,1,1,1,1]).should == [1,1,1,1,1,1,1]
MergeSort.([3,3,5,6,7,1,2]).should == [1,2,3,3,5,6,7]
expect(QuickSort.([3,3,5,6,7,1,2])).to eq([1,2,3,3,5,6,7])
expect(QuickSort.([1,1,1,1,1,1,1])).to eq([1,1,1,1,1,1,1])
expect(MergeSort.([3,3,5,6,7,1,2])).to eq([1,2,3,3,5,6,7])
end

it "merges hashes" do
a = { a: 'a', b: 'b' }
b = { a: '1', c: '3' }
MergeHash.(a,b).should == { a: ['a','1'], b: 'b', c: '3' }
ZipHashLeft.(a,b).should == { a: ['a','1'], b: ['b', nil] }
ZipHashRight.(a,b).should == { a: ['a','1'], c: [nil, '3'] }
ZipHashInner.(a,b).should == { a: ['a','1'] }
expect(MergeHash.(a,b)).to eq({ a: ['a','1'], b: 'b', c: '3' })
expect(ZipHashLeft.(a,b)).to eq({ a: ['a','1'], b: ['b', nil] })
expect(ZipHashRight.(a,b)).to eq({ a: ['a','1'], c: [nil, '3'] })
expect(ZipHashInner.(a,b)).to eq({ a: ['a','1'] })
end

end
end
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

config.run_all_when_everything_filtered = true
config.filter_run :focus

config.raise_errors_for_deprecations!

include Functions::Prelude

# Run specs in random order to surface order dependencies. If you find an
Expand Down

0 comments on commit 4e8cbd7

Please sign in to comment.