Skip to content

Commit

Permalink
add a should_equal luaspec matcher and refactor specs to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
mirven committed Dec 14, 2009
1 parent c049c49 commit 1bb487f
Show file tree
Hide file tree
Showing 36 changed files with 94 additions and 170 deletions.
3 changes: 1 addition & 2 deletions spec/all_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.all"] = function()
describe["when providing a truth function"] = function()
Expand Down
3 changes: 1 addition & 2 deletions spec/any_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.any"] = function()
describe["when providing a truth function"] = function()
Expand Down
8 changes: 2 additions & 6 deletions spec/chain_spec.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["chaining"] = function()
it["should be able to chain calls and retrieve the value"] = function()
result = _({ 1,2,3 }):chain():map(function(i) return i*2 end):map(function(i) return i*2 end):value()

expect(#result).should_be(3)
expect(result[1]).should_be(4)
expect(result[2]).should_be(8)
expect(result[3]).should_be(12)
expect(result).should_equal {4,8,12}
end
end

Expand Down
4 changes: 1 addition & 3 deletions spec/compose_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'luaspec'
require 'luamock'
_ = require 'underscore'
require 'spec_helper'

describe["_.compose"] = function()
it["should create a function that when called calls all the functions like f(g(h()))"] = function()
Expand Down
4 changes: 1 addition & 3 deletions spec/curry_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'luaspec'
require 'luamock'
_ = require 'underscore'
require 'spec_helper'

describe["_.curry"] = function()
it["should use the provided argument as the first argument for calls"] = function()
Expand Down
3 changes: 1 addition & 2 deletions spec/detect_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.detect"] = function()
it["should return the first element that passes the test"] = function()
Expand Down
4 changes: 1 addition & 3 deletions spec/each_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'luaspec'
require 'luamock'
_ = require 'underscore'
require 'spec_helper'

describe["_.each"] = function()
before = function()
Expand Down
7 changes: 2 additions & 5 deletions spec/extend_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.extend"] = function()
before = function()
Expand All @@ -9,9 +8,7 @@ describe["_.extend"] = function()
end

it["should add all values from the destination table into the source table"] = function()
expect(result.a).should_be(1)
expect(result.b).should_be(2)
expect(result.c).should_be(3)
expect(result).should_equal {a=1,b=2,c=3}
end

it["should return the source table"] = function()
Expand Down
7 changes: 2 additions & 5 deletions spec/first_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.first"] = function()
it["should return the first item"] = function()
Expand All @@ -12,9 +11,7 @@ describe["_.first"] = function()
it["should return an array with the first n items"] = function()
input = { 1,2,3,4 }
result = _.first(input,2)
expect(#result).should_be(2)
expect(result[1]).should_be(1)
expect(result[2]).should_be(2)
expect(result).should_equal {1,2}
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/flatten_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.flatten"] = function()
before = function()
Expand All @@ -8,8 +7,7 @@ describe["_.flatten"] = function()
end

it["should return an array with each item"] = function()
expect(#result).should_be(7)
_.each(result, function(i) expect(i).should_be(i) end)
expect(result).should_equal {1,2,3,4,5,6,7}
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/include_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.include"] = function()
it["should return the true if the item is in the list"] = function()
Expand Down
4 changes: 1 addition & 3 deletions spec/invoke_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'luaspec'
require 'luamock'
_ = require 'underscore'
require 'spec_helper'

describe["_.invoke"] = function()
before = function()
Expand Down
3 changes: 1 addition & 2 deletions spec/is_empty_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.is_empty"] = function()
it["should return false when the table has elements in it"] = function()
Expand Down
3 changes: 1 addition & 2 deletions spec/is_equal_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.is_equal"] = function()
describe["when objects are different types"] = function()
Expand Down
17 changes: 5 additions & 12 deletions spec/iter_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.iter"] = function()
describe["when passed an array"] = function()
Expand All @@ -10,11 +9,8 @@ describe["_.iter"] = function()
for i in _.iter(input) do
output[#output+1] = i
end

expect(#output).should_be(3)
expect(output[1]).should_be(1)
expect(output[2]).should_be(2)
expect(output[3]).should_be(3)

expect(output).should_equal {1,2,3}
end
end

Expand All @@ -32,11 +28,8 @@ describe["_.iter"] = function()
for i in _.iter(sq(3)) do
output[#output+1] = i
end

expect(#output).should_be(3)
expect(output[1]).should_be(1)
expect(output[2]).should_be(4)
expect(output[3]).should_be(9)

expect(output).should_equal {1,4,9}
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/join_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.join"] = function()
describe["when not specifying a separator"] = function()
Expand Down
4 changes: 2 additions & 2 deletions spec/keys_spec.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.keys"] = function()
it["should return an array with all property names"] = function()
input = { a = 1, b = 2, c = 3 }
keys = _.keys(input)

expect(#keys).should_be(3)
expect(_.include(keys, 'a')).should_be(true)
expect(_.include(keys, 'b')).should_be(true)
Expand Down
8 changes: 2 additions & 6 deletions spec/map_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.map"] = function()
before = function()
Expand All @@ -8,10 +7,7 @@ describe["_.map"] = function()
end

it["should return an array of the same size with all elements transformed by the function"] = function()
expect(#result).should_be(3)
expect(result[1]).should_be(2)
expect(result[2]).should_be(4)
expect(result[3]).should_be(6)
expect(result).should_equal {2,4,6}
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/max_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.max"] = function()
describe["when not providing a transformation function"] = function()
Expand Down
3 changes: 1 addition & 2 deletions spec/min_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.min"] = function()
describe["when not providing a transformation function"] = function()
Expand Down
8 changes: 2 additions & 6 deletions spec/pluck_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.pluck"] = function()
before = function()
Expand All @@ -8,10 +7,7 @@ describe["_.pluck"] = function()
end

it["should return an array of the same size with the value of the specified property for each input element"] = function()
expect(#result).should_be(3)
expect(result[1]).should_be(1)
expect(result[2]).should_be(2)
expect(result[3]).should_be(3)
expect(result).should_equal({1,2,3})
end
end

Expand Down
6 changes: 2 additions & 4 deletions spec/pop_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.pop"] = function()
before = function()
Expand All @@ -12,8 +11,7 @@ describe["_.pop"] = function()
end

it["should remove the last item from the array"] = function()
expect(#input).should_be(1)
expect(input[1]).should_be(1)
expect(input).should_equal {1}
end
end

Expand Down
8 changes: 2 additions & 6 deletions spec/push_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.push"] = function()
before = function()
Expand All @@ -8,10 +7,7 @@ describe["_.push"] = function()
end

it["should add the item onto the end of the array"] = function()
expect(#result).should_be(3)
expect(result[1]).should_be(1)
expect(result[2]).should_be(2)
expect(result[3]).should_be(3)
expect(result).should_equal {1,2,3}
end

it["should return the input array"] = function()
Expand Down
23 changes: 5 additions & 18 deletions spec/range_spec.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.range"] = function()
describe["when only passing a length"] = function()
it["should iterate from 1 to the length"] = function()
result = _.range(3):to_array()
expect(#result).should_be(3)
expect(result[1]).should_be(1)
expect(result[2]).should_be(2)
expect(result[3]).should_be(3)
expect(result).should_equal {1,2,3}
end
end

describe["when only passing a start and end value"] = function()
it["should iterate from start to the end inclusively"] = function()
result = _.range(2,4):to_array()
expect(#result).should_be(3)
expect(result[1]).should_be(2)
expect(result[2]).should_be(3)
expect(result[3]).should_be(4)
expect(result).should_equal {2,3,4}
end
end

describe["when passing a start and end value and a step"] = function()
describe["when step is positive"] = function()
it["should iterate from start to the end inclusively incremented by step"] = function()
result = _.range(2,6,2):to_array()
expect(#result).should_be(3)
expect(result[1]).should_be(2)
expect(result[2]).should_be(4)
expect(result[3]).should_be(6)
expect(result).should_equal {2,4,6}
end
end

describe["when step is negative"] = function()
it["should iterate from start to the end inclusively decremented by step"] = function()
result = _.range(6,2,-2):to_array()
expect(#result).should_be(3)
expect(result[1]).should_be(6)
expect(result[2]).should_be(4)
expect(result[3]).should_be(2)
expect(result).should_equal {6,4,2}
end
end
end
Expand Down
7 changes: 2 additions & 5 deletions spec/reject_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.reject"] = function()
before = function()
Expand All @@ -8,9 +7,7 @@ describe["_.reject"] = function()
end

it["should return an array with only elements that don't pass the truth function"] = function()
expect(#result).should_be(2)
expect(result[1]).should_be(1)
expect(result[2]).should_be(2)
expect(result).should_equal {1,2}
end
end

Expand Down
12 changes: 3 additions & 9 deletions spec/rest_spec.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
require 'luaspec'
_ = require 'underscore'
require 'spec_helper'

describe["_.rest"] = function()
it["should all items but the first"] = function()
input = { 1,2,3,4 }
result = _.rest(input)
expect(#result).should_be(3)
expect(result[1]).should_be(2)
expect(result[2]).should_be(3)
expect(result[3]).should_be(4)
expect(result).should_equal {2,3,4}
end

describe["when specifying a starting index"] = function()
it["should all items but the first, starting with the specified index"] = function()
input = { 1,2,3,4 }
result = _.rest(input, 3)
expect(#result).should_be(2)
expect(result[1]).should_be(3)
expect(result[2]).should_be(4)
expect(result).should_equal {3,4}
end
end
end
Expand Down
Loading

0 comments on commit 1bb487f

Please # to comment.