aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/with_spec.lua
blob: 252fe7ff371deb4070754c89046441c5e10642e6 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
return describe("with", function()
	it("should access property with . syntax", function()
		local obj = {
			value = 42
		}
		local result = obj.value
		assert.same(result, 42)
		return obj
	end)
	it("should call method with : syntax", function()
		local obj = {
			func = function()
				return "result"
			end
		}
		local result = obj:func()
		assert.same(result, "result")
		return obj
	end)
	it("should work as statement", function()
		local obj = {
			x = 10,
			y = 20
		}
		obj.sum = obj.x + obj.y
		return assert.same(obj.sum, 30)
	end)
	it("should support nested with", function()
		local outer = {
			inner = {
				value = 100
			}
		}
		local _with_0 = outer.inner
		local result = _with_0.value
		assert.same(result, 100)
		return _with_0
	end)
	it("should work with? safely", function()
		local obj = {
			x = 5
		}
		local result = obj.x
		assert.same(result, 5)
		return obj
	end)
	it("should work with if inside with", function()
		local obj = {
			x = 10,
			y = 20
		}
		if obj.x > 5 then
			local result = obj.x + obj.y
			assert.same(result, 30)
		end
		return obj
	end)
	it("should work with switch inside with", function()
		local obj = {
			type = "add",
			a = 5,
			b = 3
		}
		local result
		do
			local _exp_0 = obj.type
			if "add" == _exp_0 then
				result = obj.a + obj.b
			else
				result = 0
			end
		end
		assert.same(result, 8)
		return obj
	end)
	it("should work with loop inside with", function()
		local obj = {
			items = {
				1,
				2,
				3
			}
		}
		local sum = 0
		local _list_0 = obj.items
		for _index_0 = 1, #_list_0 do
			local item = _list_0[_index_0]
			sum = sum + item
		end
		return assert.same(sum, 6)
	end)
	it("should work with destructure", function()
		local obj = {
			x = 1,
			y = 2,
			z = 3
		}
		local x, y, z = obj.x, obj.y, obj.z
		assert.same(x, 1)
		assert.same(y, 2)
		assert.same(z, 3)
		return obj
	end)
	it("should handle simple with body", function()
		local obj = {
			value = 42
		}
		obj.value2 = 100
		return assert.same(obj.value2, 100)
	end)
	it("should work with return inside", function()
		local obj = {
			value = 100
		}
		local fn
		fn = function()
			return obj.value
		end
		return assert.same(fn(), 100)
	end)
	it("should work with break inside", function()
		local sum = 0
		for i = 1, 5 do
			local obj = {
				value = i
			}
			if obj.value == 3 then
				break
			end
			sum = sum + obj.value
		end
		return assert.same(sum, 3)
	end)
	it("should chain property access", function()
		local obj = {
			a = {
				b = {
					c = 42
				}
			}
		}
		local _with_0 = obj.a.b
		local result = _with_0.c
		assert.same(result, 42)
		return _with_0
	end)
	it("should work with multiple statements", function()
		local obj = {
			x = 1,
			y = 2
		}
		local sum = 0
		do
			sum = sum + obj.x
			sum = sum + obj.y
		end
		return assert.same(sum, 3)
	end)
	return it("should preserve object reference", function()
		local obj = {
			value = 42
		}
		obj.value = 100
		return assert.same(obj.value, 100)
	end)
end)