aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/class_spec.yue
blob: 4f7af7c4a9c22b8c01fcfb3af9f97ad3d557fc6f (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
describe "class", ->
	it "should make a class with constructor", ->
		class Thing
			new: =>
				@color = "blue"

		instance = Thing!

		assert.same instance, { color: "blue" }

	it "should have instance methods", ->
		class Thing
			get_color: => @color

			new: =>
				@color = "blue"

		instance = Thing!
		assert.same instance\get_color!, "blue"

	it "should have base properies from class", ->
		class Thing
			color: "blue"
			get_color: => @color

		instance = Thing!
		assert.same instance\get_color!, "blue"
		assert.same Thing.color, "blue"

	it "should inherit another class", ->
		class Base
			get_property: => @[@property]

			new: (@property) =>

		class Thing extends Base
			color: "green"

		instance = Thing "color"
		assert.same instance\get_property!, "green"


	it "should have class properties", ->
		class Base
		class Thing extends Base

		instance = Thing!

		assert.same Base.__name, "Base"
		assert.same Thing.__name, "Thing"
		assert.is_true Thing.__parent == Base

		assert.is_true instance.__class == Thing

	it "should have name when assigned", ->
		Thing = class
		assert.same Thing.__name, "Thing"

	it "should not expose class properties on instance", ->
		class Thing
			@height: 10

		Thing.color = "blue"

		instance = Thing!
		assert.same instance.color, nil
		assert.same instance.height, nil

	it "should expose new things added to __base", ->
		class Thing

		instance = Thing!
		Thing.__base.color = "green"

		assert.same instance.color, "green"

	it "should call with correct receiver", ->
		local instance

		class Thing
			is_class: => assert.is_true @ == Thing
			is_instance: => assert.is_true @ == instance

			go: =>
				@@is_class!
				@is_instance!

		instance = Thing!
		instance\go!

	it "should have class properies take precedence over base properties", ->
		class Thing
			@prop: "hello"
			prop: "world"

		assert.same "hello", Thing.prop

	describe "super", ->
		it "should call super constructor", ->
			class Base
				new: (@property) =>

			class Thing extends Base
				new: (@name) =>
					super "name"

			instance = Thing "the_thing"

			assert.same instance.property, "name"
			assert.same instance.name, "the_thing"

		it "should call super method", ->
			class Base
				_count: 111
				counter: => @_count

			class Thing extends Base
				counter: => "%08d"\format super!

			instance = Thing!
			assert.same instance\counter!, "00000111"

		it "should call other method from super", ->
			class Base
				_count: 111
				counter: =>
					@_count

			class Thing extends Base
				other_method: => super\counter!

			instance = Thing!
			assert.same instance\other_method!, 111

		it "should get super class", ->
			class Base
			class Thing extends Base
				get_super: => super

			instance = Thing!
			assert.is_true instance\get_super! == Base

		it "should get a bound method from super", ->
			class Base
				count: 1
				get_count: => @count

			class Thing extends Base
				get_count: => "this is wrong"
				get_method: => super\get_count

			instance = Thing!
			assert.same instance\get_method!!, 1

		it "class properties take precedence in super class over base", ->
			class Thing
				@prop: "hello"
				prop: "world"

			class OtherThing extends Thing

			assert.same "hello", OtherThing.prop

		it "gets value from base in super class", ->
			class Thing
				prop: "world"

			class OtherThing extends Thing
			assert.same "world", OtherThing.prop

		it "should let parent be replaced on class", ->
			class A
				@prop: "yeah"
				cool: => 1234
				plain: => "a"

			class B
				@prop: "okay"
				cool: => 9999
				plain: => "b"

			class Thing extends A
				cool: =>
					super! + 1

				get_super: =>
					super

			instance = Thing!

			assert.same "a", instance\plain!
			assert.same 1235, instance\cool!
			assert A == instance\get_super!, "expected super to be B"

			Thing.__parent = B
			setmetatable Thing.__base, B.__base

			assert.same "b", instance\plain!
			assert.same 10000, instance\cool!
			assert B == instance\get_super!, "expected super to be B"

		it "should resolve many levels of super", ->
			class One
				a: =>
					1

			class Two extends One
				a: =>
					super! + 2

			class Three extends Two
				a: =>
					super! + 3

			i = Three!

			assert.same 6, i\a!


		it "should resolve many levels of super with a gap", ->
			class One
				a: =>
					1

			class Two extends One

			class Three extends Two
				a: =>
					super! + 3

			class Four extends Three
				a: =>
					super! + 4

			i = Four!

			assert.same 8, i\a!


		it "should call correct class/instance super methods", ->
			class Base
				doit: =>
					"instance"

				@doit: =>
					"class"

			class One extends Base
				doit: => super!
				@doit: => super!

			assert.same "instance", One!\doit!
			assert.same "class", One\doit!


		it "should resolve many levels of super on class methods", ->
			class One
				@a: =>
					1

			class Two extends One

			class Three extends Two
				@a: =>
					super! + 3

			class Four extends Three
				@a: =>
					super! + 4

			assert.same 8, Four\a!

		it "super should still work when method wrapped", ->
			add_some = (opts) ->
				=> opts.amount + opts[1] @

			class Base
				value: => 1

			class Sub extends Base
				value: add_some {
					amount: 12
					=>
						super! + 100
				}

			class OtherSub extends Base
				value: if true
					=> 5 + super!
				else
					=> 2 + super!

			assert.same 1 + 100 + 12, Sub!\value!
			assert.same 6, OtherSub!\value!

		it "should copy metamethod from super", ->
			class A
				val: 1
				<tostring>: => "Object #{ @val }"

			class B extends A
				val: 2

			a, b = A!, B!

			assert.same "Object 1", tostring a
			assert.same "Object 2", tostring b