Permalink
Browse files

add a should_equal luaspec matcher and refactor specs to use it

  • Loading branch information...
1 parent c049c49 commit 1bb487f5a0e523a4438a9d126a7ed44accea5a5a @mirven committed Dec 14, 2009
View
3 spec/all_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.all"] = function()
describe["when providing a truth function"] = function()
View
3 spec/any_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.any"] = function()
describe["when providing a truth function"] = function()
View
8 spec/chain_spec.lua
@@ -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
View
4 spec/compose_spec.lua
@@ -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()
View
4 spec/curry_spec.lua
@@ -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()
View
3 spec/detect_spec.lua
@@ -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()
View
4 spec/each_spec.lua
@@ -1,6 +1,4 @@
-require 'luaspec'
-require 'luamock'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.each"] = function()
before = function()
View
7 spec/extend_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.extend"] = function()
before = function()
@@ -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()
View
7 spec/first_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.first"] = function()
it["should return the first item"] = function()
@@ -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
View
6 spec/flatten_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.flatten"] = function()
before = function()
@@ -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
View
3 spec/include_spec.lua
@@ -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()
View
4 spec/invoke_spec.lua
@@ -1,6 +1,4 @@
-require 'luaspec'
-require 'luamock'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.invoke"] = function()
before = function()
View
3 spec/is_empty_spec.lua
@@ -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()
View
3 spec/is_equal_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.is_equal"] = function()
describe["when objects are different types"] = function()
View
17 spec/iter_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.iter"] = function()
describe["when passed an array"] = function()
@@ -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
@@ -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
View
3 spec/join_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.join"] = function()
describe["when not specifying a separator"] = function()
View
4 spec/keys_spec.lua
@@ -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)
View
8 spec/map_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.map"] = function()
before = function()
@@ -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
View
3 spec/max_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.max"] = function()
describe["when not providing a transformation function"] = function()
View
3 spec/min_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.min"] = function()
describe["when not providing a transformation function"] = function()
View
8 spec/pluck_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.pluck"] = function()
before = function()
@@ -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
View
6 spec/pop_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.pop"] = function()
before = function()
@@ -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
View
8 spec/push_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.push"] = function()
before = function()
@@ -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()
View
23 spec/range_spec.lua
@@ -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
View
7 spec/reject_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.reject"] = function()
before = function()
@@ -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
View
12 spec/rest_spec.lua
@@ -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
View
9 spec/reverse_spec.lua
@@ -1,15 +1,10 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.reverse"] = function()
it["should return a reversed array"] = function()
input = { 1,2,3 }
result = _.reverse(input)
-
- expect(#result).should_be(3)
- expect(result[1]).should_be(3)
- expect(result[2]).should_be(2)
- expect(result[3]).should_be(1)
+ expect(result).should_equal {3,2,1}
end
end
View
6 spec/select_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.select"] = function()
before = function()
@@ -8,8 +7,7 @@ describe["_.select"] = function()
end
it["should return an array with only elements that do pass the truth function"] = function()
- expect(#result).should_be(1)
- expect(result[1]).should_be(3)
+ expect(result).should_equal {3}
end
end
View
6 spec/shift_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.shift"] = function()
before = function()
@@ -12,8 +11,7 @@ describe["_.shift"] = function()
end
it["should remove the first item from the array"] = function()
- expect(#input).should_be(1)
- expect(input[1]).should_be(2)
+ expect(input).should_equal {2}
end
end
View
8 spec/slice_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.slice"] = function()
before = function()
@@ -20,10 +19,7 @@ describe["_.slice"] = function()
end
it["should return all the values of the input after the specified index"] = function()
- 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
end
View
13 spec/sort_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.sort"] = function()
describe["when not providing a comparison function"] = function()
@@ -9,10 +8,7 @@ describe["_.sort"] = function()
end
it["should return an array sorted by <"] = 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
@@ -26,10 +22,7 @@ describe["_.sort"] = function()
end
it["should return the items sorted by the comparison function"] = function()
- expect(#result).should_be(3)
- expect(result[1]).should_be(i2)
- expect(result[2]).should_be(i1)
- expect(result[3]).should_be(i3)
+ expect(result).should_equal {i2,i1,i3}
end
end
end
View
17 spec/spec_helper.lua
@@ -0,0 +1,17 @@
+require 'luaspec'
+require 'luamock'
+_ = require 'underscore'
+
+function matchers.should_equal(value, expected)
+ if not _.is_equal(value, expected) then
+ return false, "expecting "..tostring(expected)..", not ".. tostring(value)
+ end
+ return true
+end
+
+function matchers.should_not_equal(value, expected)
+ if _.is_equal(value, expected) then
+ return false, "should not be "..tostring(value)
+ end
+ return true
+end
View
8 spec/to_array_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.to_array"] = function()
before = function()
@@ -15,10 +14,7 @@ describe["_.to_array"] = function()
end
it["should return the values produced by the iterator in an array"] = function()
- expect(#result).should_be(3)
- expect(result[1]).should_be(1)
- expect(result[2]).should_be(4)
- expect(result[3]).should_be(9)
+ expect(result).should_equal {1,4,9}
end
end
View
8 spec/unshift_spec.lua
@@ -1,5 +1,4 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.unshift"] = function()
before = function()
@@ -8,10 +7,7 @@ describe["_.unshift"] = function()
end
it["should add the item onto the beginning of the array"] = function()
- expect(#result).should_be(3)
- expect(result[1]).should_be(3)
- expect(result[2]).should_be(1)
- expect(result[3]).should_be(2)
+ expect(result).should_equal {3,1,2}
end
it["should return the input array"] = function()
View
4 spec/values_spec.lua
@@ -1,10 +1,10 @@
-require 'luaspec'
-_ = require 'underscore'
+require 'spec_helper'
describe["_.values"] = function()
it["should return an array with all property values"] = function()
input = { a = 1, b = 2, c = 3 }
values = _.values(input)
+
expect(#values).should_be(3)
expect(_.include(values, 1)).should_be(true)
expect(_.include(values, 2)).should_be(true)
View
21 spec/wrap_spec.lua
@@ -1,9 +1,16 @@
-require 'luaspec'
+require 'spec_helper'
-describe["_.map"] = function()
- before = function()
+describe["_.wrap"] = function()
+ it["should create a new function that is pased the wrapped function when called"] = function()
+ f = Mock:new()
+ g = _.wrap(f, function(func, p1, p2)
+ func(p1+1,p2+1)
+ end)
+
+ g(2,4)
+
+ expect(f).was_called_with(3,5)
end
-
- it["should"] = function()
- end
-end
+end
+
+spec:report(true)

0 comments on commit 1bb487f

Please sign in to comment.