From 1edae7714d9429ab3cd435f93738a41d86589481 Mon Sep 17 00:00:00 2001 From: George Roman Date: Sat, 5 May 2018 21:23:37 +0300 Subject: Add tests for the fun module --- spec/fun_spec.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 spec/fun_spec.lua 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 @@ +local test_env = require("spec.util.test_env") +local fun = require("luarocks.fun") + +test_env.unload_luarocks() + +describe("LuaRocks fun tests", function() + describe("fun.concat", function() + it("returns the concatenation of the two tables given as arguments", function() + local t1, t2 + + t1 = {1, 2, 3} + t2 = {4, 5, 6} + assert.same(fun.concat(t1, t2), {1, 2, 3, 4, 5, 6}) + assert.same(fun.concat(t2, t1), {4, 5, 6, 1, 2, 3}) + t1 = {1, 2, 3} + t2 = {} + assert.same(fun.concat(t1, t2), {1, 2, 3}) + assert.same(fun.concat(t2, t1), {1, 2, 3}) + t1 = {} + t2 = {} + assert.same(fun.concat(t1, t2), {}) + end) + end) + + describe("fun.contains", function() + it("checks whether a table contains a given value", function() + local t + + t = {1, 2, 3} + assert.truthy(fun.contains(t, 1)) + assert.falsy(fun.contains(t, 4)) + t = {} + assert.falsy(fun.contains(t, 1)) + end) + end) + + local addOne = function(x) return x + 1 end + + describe("fun.map", function() + it("applies a function to each element in the given table and returns the results in a new table", function() + local t + + t = {1, 2, 3} + assert.same(fun.map(t, addOne), {2, 3, 4}) + t = {} + assert.same(fun.map(t, addOne), {}) + end) + end) + + describe("fun.traverse", function() + it("recursively applies a function to each element in a given table and returns the results in a new table", function() + local t + + t = {1, 2, {3, 4, {5, 6}}} + assert.same(fun.traverse(t, addOne), {2, 3, {4, 5, {6, 7}}}) + t = {1, 2, {}, {1, {}, 2}} + assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) + end) + end) +end) -- cgit v1.2.3-55-g6feb