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
|
return describe("return", function()
it("should return from comprehension", function()
local fn
fn = function()
local _accum_0 = { }
local _len_0 = 1
for x = 1, 5 do
_accum_0[_len_0] = x * 2
_len_0 = _len_0 + 1
end
return _accum_0
end
local result = fn()
return assert.same(result, {
2,
4,
6,
8,
10
})
end)
it("should return from table comprehension", function()
local fn
fn = function()
local _tbl_0 = { }
for k, v in pairs({
a = 1,
b = 2
}) do
_tbl_0[k] = v
end
return _tbl_0
end
local result = fn()
return assert.same(type(result), "table")
end)
it("should return from nested if", function()
local fn
fn = function(a, b)
if a then
if b then
return "both"
else
return "only a"
end
else
return "neither"
end
end
assert.same(fn(true, true), "both")
assert.same(fn(true, false), "only a")
return assert.same(fn(false, false), "neither")
end)
it("should return from switch", function()
local fn
fn = function(value)
if 1 == value then
return "one"
elseif 2 == value then
return "two"
else
return "other"
end
end
assert.same(fn(1), "one")
assert.same(fn(2), "two")
return assert.same(fn(3), "other")
end)
it("should return table literal", function()
local fn
fn = function()
return {
value = 42,
name = "test"
}
end
local result = fn()
assert.same(result.value, 42)
return assert.same(result.name, "test")
end)
it("should return array literal", function()
local fn
fn = function()
return {
1,
2,
3
}
end
local result = fn()
return assert.same(result, {
1,
2,
3
})
end)
it("should return from with statement", function()
local fn
fn = function(obj)
local result = obj.value
return result
end
return assert.same(fn({
value = 100
}), 100)
end)
it("should return nil implicitly", function()
local fn
fn = function()
local _ = "no return"
end
return assert.same(fn(), nil)
end)
it("should return multiple values", function()
local fn
fn = function()
return 1, 2, 3
end
local a, b, c = fn()
assert.same(a, 1)
assert.same(b, 2)
return assert.same(c, 3)
end)
it("should return from function call", function()
local fn
fn = function()
local inner
inner = function()
return 42
end
return inner()
end
return assert.same(fn(), 42)
end)
return it("should handle return in expression context", function()
local fn
fn = function(cond)
if cond then
return "yes"
else
return "no"
end
end
assert.same(fn(true), "yes")
return assert.same(fn(false), "no")
end)
end)
|