summaryrefslogtreecommitdiff
path: root/spec/outputs/test/stub_spec.lua
blob: 0daf5b69850c1ab112f357ee9c29dfc7664dc460 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local stub_fn
stub_fn = function()
	return function() end
end
local _anon_func_0 = function(stub_fn)
	stub_fn()
	return true
end
return describe("function stub", function()
	it("should create empty function", function()
		stub_fn()
		return assert.is_true(true)
	end)
	it("should support stub in table", function()
		local obj = {
			stub = stub_fn()
		}
		return assert.is_true(true)
	end)
	it("should work with method stub", function()
		local obj = {
			method = stub_fn()
		}
		return assert.is_true(true)
	end)
	it("should handle stub in assignment", function()
		local my_func = stub_fn()
		return assert.is_true(true)
	end)
	it("should support stub in return", function()
		local get_stub
		get_stub = function()
			return stub_fn()
		end
		local fn = get_stub()
		return assert.is_true(true)
	end)
	it("should work in conditional", function()
		if stub_fn() then
			return assert.is_true(true)
		end
	end)
	it("should support stub as callback", function()
		local call_fn
		call_fn = function(fn)
			return fn()
		end
		local result = call_fn(stub_fn())
		return assert.is_true(true)
	end)
	it("should handle stub in table literal", function()
		local tb = {
			on_click = stub_fn(),
			on_hover = stub_fn()
		}
		return assert.is_true(true)
	end)
	it("should work with fat arrow stub", function()
		local obj = {
			value = 10,
			method = stub_fn()
		}
		local result = obj:method()
		return assert.is_true(true)
	end)
	it("should support stub in array", function()
		local callbacks = {
			stub_fn(),
			stub_fn(),
			stub_fn()
		}
		return assert.same(#callbacks, 3)
	end)
	it("should handle stub in expression", function()
		local result = stub_fn() and true or false
		return assert.is_true(result)
	end)
	it("should work with chained stub calls", function()
		stub_fn()
		stub_fn()
		stub_fn()
		return assert.is_true(true)
	end)
	it("should support stub in comprehension", function()
		local result
		do
			local _accum_0 = { }
			local _len_0 = 1
			for i = 1, 3 do
				_accum_0[_len_0] = stub_fn()
				_len_0 = _len_0 + 1
			end
			result = _accum_0
		end
		return assert.same(#result, 3)
	end)
	it("should handle stub in switch", function()
		local value = "test"
		local result
		if "test" == value then
			stub_fn()
			result = "matched"
		else
			result = "not matched"
		end
		return assert.same(result, "matched")
	end)
	it("should work in with statement", function()
		local obj = {
			stub = stub_fn()
		}
		obj.stub()
		return assert.is_true(true)
	end)
	it("should support stub as argument default", function()
		local fn
		fn = function(callback)
			if callback == nil then
				callback = stub_fn()
			end
			return callback()
		end
		local result = fn()
		return assert.is_true(true)
	end)
	it("should handle stub in varargs", function()
		local collect
		collect = function(...)
			return {
				...
			}
		end
		local result = collect(stub_fn(), stub_fn())
		return assert.same(#result, 2)
	end)
	it("should work in do block", function()
		do
			stub_fn()
		end
		return assert.is_true(true)
	end)
	return it("should support stub in try block", function()
		local success = xpcall(_anon_func_0, function(err)
			return false
		end, stub_fn)
		return assert.is_true(success)
	end)
end)