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
|
return describe("prefixed return", function()
it("should return prefixed value with no explicit return", function()
local findFirstEven
findFirstEven = function(list)
for _index_0 = 1, #list do
local item = list[_index_0]
if type(item) == "table" then
for _index_1 = 1, #item do
local sub = item[_index_1]
if sub % 2 == 0 then
return sub
end
end
end
end
return nil
end
local result = findFirstEven({
1,
3,
{
4,
6
},
5
})
return assert.same(result, 4)
end)
it("should return prefixed nil when not found", function()
local findValue
findValue = function(list)
for _index_0 = 1, #list do
local item = list[_index_0]
if item == 999 then
return item
end
end
return nil
end
local result = findValue({
1,
2,
3
})
return assert.same(result, nil)
end)
it("should return prefixed string", function()
local findName
findName = function(items)
for _index_0 = 1, #items do
local item = items[_index_0]
if item.name == "target" then
return item.name
end
end
return "not found"
end
local result = findName({
{
name = "a"
},
{
name = "b"
}
})
return assert.same(result, "not found")
end)
it("should return prefixed number", function()
local calculateSum
calculateSum = function()
local total = 0
return 0
end
local result = calculateSum()
return assert.same(result, 0)
end)
return it("should work with nested logic", function()
local findNested
findNested = function(data)
for _index_0 = 1, #data do
local category = data[_index_0]
if type(category) == "table" then
for _index_1 = 1, #category do
local item = category[_index_1]
if item == "target" then
return "found"
end
end
end
end
return "missing"
end
local result = findNested({
{
1,
2
},
{
"target",
3
}
})
return assert.same(result, "found")
end)
end)
|