diff options
author | George Roman <george.roman.99@gmail.com> | 2018-07-24 10:36:56 +0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-24 15:37:54 -0300 |
commit | 64148b57c1ca70675022ebb7025a116a292802a9 (patch) | |
tree | ecfa73366873ba926da0efaa6e1103f3c886f8ee | |
parent | fa0925e382335d01f50ea26130aedc692483b8cf (diff) | |
download | luarocks-64148b57c1ca70675022ebb7025a116a292802a9.tar.gz luarocks-64148b57c1ca70675022ebb7025a116a292802a9.tar.bz2 luarocks-64148b57c1ca70675022ebb7025a116a292802a9.zip |
Tests: add more unit tests for fun.lua
-rw-r--r-- | spec/fun_spec.lua | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/spec/fun_spec.lua b/spec/fun_spec.lua index fe945921..064440e0 100644 --- a/spec/fun_spec.lua +++ b/spec/fun_spec.lua | |||
@@ -70,4 +70,61 @@ describe("LuaRocks fun tests #unit", function() | |||
70 | assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) | 70 | assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) |
71 | end) | 71 | end) |
72 | end) | 72 | end) |
73 | |||
74 | describe("fun.filter", function() | ||
75 | it("filters the elements in the given table and returns the result in a new table", function() | ||
76 | local t | ||
77 | |||
78 | t = {1, 2, "a", "b", 3} | ||
79 | assert.same(fun.filter(t, function(x) return type(x) == "number" end), {1, 2, 3}) | ||
80 | t = {2, 4, 7, 8} | ||
81 | assert.same(fun.filter(t, function(x) return x % 2 == 0 end), {2, 4, 8}) | ||
82 | end) | ||
83 | end) | ||
84 | |||
85 | describe("fun.reverse_in", function() | ||
86 | it("reverses the elements in the given table and returns the result in a new table", function() | ||
87 | local t | ||
88 | |||
89 | t = {1, 2, 3, 4} | ||
90 | assert.same(fun.reverse_in(t), {4, 3, 2, 1}) | ||
91 | t = {"a", "b", "c"} | ||
92 | assert.same(fun.reverse_in(t), {"c", "b", "a"}) | ||
93 | end) | ||
94 | end) | ||
95 | |||
96 | describe("fun.sort_in", function() | ||
97 | it("sorts the elements in the given table and returns the result in a new table", function() | ||
98 | local t | ||
99 | |||
100 | t = {4, 2, 3, 1} | ||
101 | assert.same(fun.sort_in(t), {1, 2, 3, 4}) | ||
102 | t = {"d", "a", "c", "b"} | ||
103 | assert.same(fun.sort_in(t), {"a", "b", "c", "d"}) | ||
104 | end) | ||
105 | end) | ||
106 | |||
107 | describe("fun.flip", function() | ||
108 | it("returns a function behaving as the one given in the argument but with the arguments interchanged", function() | ||
109 | local a, b = fun.flip(function(a, b) return a, b end)(5, 6) | ||
110 | assert.same(a, 6) | ||
111 | assert.same(b, 5) | ||
112 | end) | ||
113 | end) | ||
114 | |||
115 | describe("fun.partial", function() | ||
116 | it("partially applies the given arguments to the given function and returns it", function() | ||
117 | local addOne = fun.partial(function(x, y) return x + y end, 1) | ||
118 | assert.same(addOne(1), 2) | ||
119 | assert.same(addOne(2), 3) | ||
120 | |||
121 | local addTwo = fun.partial(function(x, y, z) return x + y + z end, 1, 1) | ||
122 | assert.same(addTwo(1), 3) | ||
123 | assert.same(addTwo(2), 4) | ||
124 | |||
125 | local addThree = fun.partial(function(x, y, z, t, u) return x + y + z + t + u end, 1, 1, 1) | ||
126 | assert.same(addThree(1, 1), 5) | ||
127 | assert.same(addThree(1, 2), 6) | ||
128 | end) | ||
129 | end) | ||
73 | end) | 130 | end) |