From 64148b57c1ca70675022ebb7025a116a292802a9 Mon Sep 17 00:00:00 2001 From: George Roman Date: Tue, 24 Jul 2018 10:36:56 +0300 Subject: Tests: add more unit tests for fun.lua --- spec/fun_spec.lua | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) 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() assert.same(fun.traverse(t, addOne), {2, 3, {}, {2, {}, 3}}) end) end) + + describe("fun.filter", function() + it("filters the elements in the given table and returns the result in a new table", function() + local t + + t = {1, 2, "a", "b", 3} + assert.same(fun.filter(t, function(x) return type(x) == "number" end), {1, 2, 3}) + t = {2, 4, 7, 8} + assert.same(fun.filter(t, function(x) return x % 2 == 0 end), {2, 4, 8}) + end) + end) + + describe("fun.reverse_in", function() + it("reverses the elements in the given table and returns the result in a new table", function() + local t + + t = {1, 2, 3, 4} + assert.same(fun.reverse_in(t), {4, 3, 2, 1}) + t = {"a", "b", "c"} + assert.same(fun.reverse_in(t), {"c", "b", "a"}) + end) + end) + + describe("fun.sort_in", function() + it("sorts the elements in the given table and returns the result in a new table", function() + local t + + t = {4, 2, 3, 1} + assert.same(fun.sort_in(t), {1, 2, 3, 4}) + t = {"d", "a", "c", "b"} + assert.same(fun.sort_in(t), {"a", "b", "c", "d"}) + end) + end) + + describe("fun.flip", function() + it("returns a function behaving as the one given in the argument but with the arguments interchanged", function() + local a, b = fun.flip(function(a, b) return a, b end)(5, 6) + assert.same(a, 6) + assert.same(b, 5) + end) + end) + + describe("fun.partial", function() + it("partially applies the given arguments to the given function and returns it", function() + local addOne = fun.partial(function(x, y) return x + y end, 1) + assert.same(addOne(1), 2) + assert.same(addOne(2), 3) + + local addTwo = fun.partial(function(x, y, z) return x + y + z end, 1, 1) + assert.same(addTwo(1), 3) + assert.same(addTwo(2), 4) + + local addThree = fun.partial(function(x, y, z, t, u) return x + y + z + t + u end, 1, 1, 1) + assert.same(addThree(1, 1), 5) + assert.same(addThree(1, 2), 6) + end) + end) end) -- cgit v1.2.3-55-g6feb