diff options
| author | George Roman <george.roman.99@gmail.com> | 2018-05-05 21:23:37 +0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-05-18 12:28:42 -0300 |
| commit | 1edae7714d9429ab3cd435f93738a41d86589481 (patch) | |
| tree | 807648e7ca132de4de0d54336c40e2d4b6ab1ee1 | |
| parent | ac1cb9b178fd5bd7bbd909062034fdb60434092a (diff) | |
| download | luarocks-1edae7714d9429ab3cd435f93738a41d86589481.tar.gz luarocks-1edae7714d9429ab3cd435f93738a41d86589481.tar.bz2 luarocks-1edae7714d9429ab3cd435f93738a41d86589481.zip | |
Add tests for the fun module
| -rw-r--r-- | spec/fun_spec.lua | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/fun_spec.lua b/spec/fun_spec.lua new file mode 100644 index 00000000..b5bed0da --- /dev/null +++ b/spec/fun_spec.lua | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | local test_env = require("spec.util.test_env") | ||
| 2 | local fun = require("luarocks.fun") | ||
| 3 | |||
| 4 | test_env.unload_luarocks() | ||
| 5 | |||
| 6 | describe("LuaRocks fun tests", function() | ||
| 7 | describe("fun.concat", function() | ||
| 8 | it("returns the concatenation of the two tables given as arguments", function() | ||
| 9 | local t1, t2 | ||
| 10 | |||
| 11 | t1 = {1, 2, 3} | ||
| 12 | t2 = {4, 5, 6} | ||
| 13 | assert.same(fun.concat(t1, t2), {1, 2, 3, 4, 5, 6}) | ||
| 14 | assert.same(fun.concat(t2, t1), {4, 5, 6, 1, 2, 3}) | ||
| 15 | t1 = {1, 2, 3} | ||
| 16 | t2 = {} | ||
| 17 | assert.same(fun.concat(t1, t2), {1, 2, 3}) | ||
| 18 | assert.same(fun.concat(t2, t1), {1, 2, 3}) | ||
| 19 | t1 = {} | ||
| 20 | t2 = {} | ||
| 21 | assert.same(fun.concat(t1, t2), {}) | ||
| 22 | end) | ||
| 23 | end) | ||
| 24 | |||
| 25 | describe("fun.contains", function() | ||
| 26 | it("checks whether a table contains a given value", function() | ||
| 27 | local t | ||
| 28 | |||
| 29 | t = {1, 2, 3} | ||
| 30 | assert.truthy(fun.contains(t, 1)) | ||
| 31 | assert.falsy(fun.contains(t, 4)) | ||
| 32 | t = {} | ||
| 33 | assert.falsy(fun.contains(t, 1)) | ||
| 34 | end) | ||
| 35 | end) | ||
| 36 | |||
| 37 | local addOne = function(x) return x + 1 end | ||
| 38 | |||
| 39 | describe("fun.map", function() | ||
| 40 | it("applies a function to each element in the given table and returns the results in a new table", function() | ||
| 41 | local t | ||
| 42 | |||
| 43 | t = {1, 2, 3} | ||
| 44 | assert.same(fun.map(t, addOne), {2, 3, 4}) | ||
| 45 | t = {} | ||
| 46 | assert.same(fun.map(t, addOne), {}) | ||
| 47 | end) | ||
| 48 | end) | ||
| 49 | |||
| 50 | describe("fun.traverse", function() | ||
| 51 | it("recursively applies a function to each element in a given table and returns the results in a new table", function() | ||
| 52 | local t | ||
| 53 | |||
| 54 | t = {1, 2, {3, 4, {5, 6}}} | ||
| 55 | assert.same(fun.traverse(t, addOne), {2, 3, {4, 5, {6, 7}}}) | ||
| 56 | t = {1, 2, {}, {1, {}, 2}} | ||
| 57 | assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) | ||
| 58 | end) | ||
| 59 | end) | ||
| 60 | end) | ||
