diff options
Diffstat (limited to 'spec/unit/fun_spec.lua')
-rw-r--r-- | spec/unit/fun_spec.lua | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/spec/unit/fun_spec.lua b/spec/unit/fun_spec.lua new file mode 100644 index 00000000..9844ec27 --- /dev/null +++ b/spec/unit/fun_spec.lua | |||
@@ -0,0 +1,130 @@ | |||
1 | local test_env = require("spec.util.test_env") | ||
2 | local testing_paths = test_env.testing_paths | ||
3 | |||
4 | test_env.unload_luarocks() | ||
5 | local fun = require("luarocks.fun") | ||
6 | |||
7 | describe("luarocks.fun #unit", function() | ||
8 | local runner | ||
9 | |||
10 | setup(function() | ||
11 | runner = require("luacov.runner") | ||
12 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | ||
13 | runner.tick = true | ||
14 | end) | ||
15 | |||
16 | teardown(function() | ||
17 | runner.shutdown() | ||
18 | end) | ||
19 | |||
20 | describe("fun.concat", function() | ||
21 | it("returns the concatenation of the two tables given as arguments", function() | ||
22 | local t1, t2 | ||
23 | |||
24 | t1 = {1, 2, 3} | ||
25 | t2 = {4, 5, 6} | ||
26 | assert.same(fun.concat(t1, t2), {1, 2, 3, 4, 5, 6}) | ||
27 | assert.same(fun.concat(t2, t1), {4, 5, 6, 1, 2, 3}) | ||
28 | t1 = {1, 2, 3} | ||
29 | t2 = {} | ||
30 | assert.same(fun.concat(t1, t2), {1, 2, 3}) | ||
31 | assert.same(fun.concat(t2, t1), {1, 2, 3}) | ||
32 | t1 = {} | ||
33 | t2 = {} | ||
34 | assert.same(fun.concat(t1, t2), {}) | ||
35 | end) | ||
36 | end) | ||
37 | |||
38 | describe("fun.contains", function() | ||
39 | it("checks whether a table contains a given value", function() | ||
40 | local t | ||
41 | |||
42 | t = {1, 2, 3} | ||
43 | assert.truthy(fun.contains(t, 1)) | ||
44 | assert.falsy(fun.contains(t, 4)) | ||
45 | t = {} | ||
46 | assert.falsy(fun.contains(t, 1)) | ||
47 | end) | ||
48 | end) | ||
49 | |||
50 | local addOne = function(x) return x + 1 end | ||
51 | |||
52 | describe("fun.map", function() | ||
53 | it("applies a function to each element in the given table and returns the results in a new table", function() | ||
54 | local t | ||
55 | |||
56 | t = {1, 2, 3} | ||
57 | assert.same(fun.map(t, addOne), {2, 3, 4}) | ||
58 | t = {} | ||
59 | assert.same(fun.map(t, addOne), {}) | ||
60 | end) | ||
61 | end) | ||
62 | |||
63 | describe("fun.traverse", function() | ||
64 | it("recursively applies a function to each element in a given table and returns the results in a new table", function() | ||
65 | local t | ||
66 | |||
67 | t = {1, 2, {3, 4, {5, 6}}} | ||
68 | assert.same(fun.traverse(t, addOne), {2, 3, {4, 5, {6, 7}}}) | ||
69 | t = {1, 2, {}, {1, {}, 2}} | ||
70 | assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) | ||
71 | 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) | ||
130 | end) | ||