aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/backcall_spec.lua
blob: 1eb9f02bc7c1ca62d7fb919543e97f07af368008 (plain)
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
55
56
57
58
59
60
61
62
63
64
return describe("backcall", function()
	it("should support basic backcall with <-", function()
		local results = { }
		local mock_map
		mock_map = function(list, fn)
			for _index_0 = 1, #list do
				local item = list[_index_0]
				table.insert(results, fn(item))
			end
		end
		do
			mock_map({
				1,
				2,
				3
			}, function(x)
				return x * 2
			end)
		end
		return assert.same(results, {
			2,
			4,
			6
		})
	end)
	it("should support nested backcalls", function()
		local results = { }
		local mock_map
		mock_map = function(list, fn)
			for _index_0 = 1, #list do
				local item = list[_index_0]
				fn(item)
			end
		end
		mock_map({
			1,
			2,
			3,
			4
		}, function(x)
			if x > 2 then
				return table.insert(results, x)
			end
		end)
		return assert.same(results, {
			3,
			4
		})
	end)
	return it("should work with method call backcall", function()
		local results = { }
		local obj = {
			process = function(self, fn)
				return fn(42)
			end
		}
		return obj:process(function(value)
			table.insert(results, value)
			return assert.same(results, {
				42
			})
		end)
	end)
end)