File: func_spec.lua

package info (click to toggle)
lua-penlight 1.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,708 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,650 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local func = require("pl.func")

describe("pl.func", function ()

  describe("compose", function ()

    it("compose(f)(x) == f(x)", function ()
      local f = function(x) return x + 1 end
      assert.equals(func.compose(f)(1), f(1))
    end)

    it("compose(f, g)(x) == f(g(x))", function ()
      local f = function(x) return x + 1 end
      local g = function(x) return x + 2 end
      assert.equals(func.compose(f, g)(1), f(g(1)))
    end)

    it("compose(f, g, h)(x) == f(g(h(x)))", function ()
      local f = function(x) return x + 1 end
      local g = function(x) return x + 2 end
      local h = function(x) return x + 3 end
      assert.equals(func.compose(f, g, h)(1), f(g(h(1))))
    end)

    it("compose(f)(x, y) == f(x, y)", function ()
      local f = function(x, y) return x + 1, y + 1 end
      local ax, ay = func.compose(f)(1, 2)
      local bx, by = f(1, 2)
      assert.equals(ax, bx)
      assert.equals(ay, by)
    end)

    it("compose(f, g)(x, y) == f(g(x, y))", function ()
      local f = function(x, y) return x + 1, y + 1 end
      local g = function(x, y) return x + 2, y + 2 end
      local ax, ay = func.compose(f, g)(1, 2)
      local bx, by = f(g(1, 2))
      assert.equals(ax, bx)
      assert.equals(ay, by)
    end)

    it("compose(f, g, h)(x, y) == f(g(h(x, y)))", function ()
      local f = function(x, y) return x + 1, y + 1 end
      local g = function(x, y) return x + 2, y + 2 end
      local h = function(x, y) return x + 3, y + 3 end
      local ax, ay = func.compose(f, g, h)(1, 2)
      local bx, by = f(g(h(1, 2)))
      assert.equals(ax, bx)
      assert.equals(ay, by)
    end)

  end)

end)