aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/while_assignment_spec.lua
blob: 289e16e54112c6188fc83cba2550ff39f1a94d71 (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
return describe("while assignment", function()
	it("should loop while value is truthy", function()
		local counter = 0
		local get_next
		get_next = function()
			if counter < 3 then
				counter = counter + 1
				return counter
			else
				return nil
			end
		end
		local results = { }
		repeat
			local val = get_next()
			if val then
				table.insert(results, val)
			else
				break
			end
		until false
		return assert.same(results, {
			1,
			2,
			3
		})
	end)
	it("should work with function results", function()
		local counter = 0
		local fn
		fn = function()
			counter = counter + 1
			if counter <= 3 then
				return counter * 10
			else
				return nil
			end
		end
		local sum = 0
		repeat
			local val = fn()
			if val then
				sum = sum + val
			else
				break
			end
		until false
		return assert.same(sum, 60)
	end)
	it("should exit immediately on nil", function()
		local get_val
		get_val = function()
			return nil
		end
		local counter = 0
		repeat
			local val = get_val()
			if val then
				counter = counter + 1
			else
				break
			end
		until false
		return assert.same(counter, 0)
	end)
	return it("should support break in loop", function()
		local items = {
			1,
			2,
			3,
			4,
			5
		}
		local sum = 0
		for _index_0 = 1, #items do
			local item = items[_index_0]
			sum = sum + item
			if sum > 6 then
				break
			end
		end
		return assert.same(sum, 10)
	end)
end)