File: class.imba

package info (click to toggle)
cloc 2.04-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,776 kB
  • sloc: perl: 29,368; cpp: 1,219; ansic: 334; asm: 267; makefile: 240; sh: 186; sql: 144; java: 136; ruby: 111; cs: 104; python: 84; pascal: 52; lisp: 50; cobol: 35; f90: 35; haskell: 35; objc: 25; php: 22; javascript: 15; fortran: 9; ml: 8; xml: 7; tcl: 2
file content (209 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download | duplicates (4)
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
# https://github.com/imba/imba/raw/master/test/syntax/class.imba
extern describe, test, ok, eq, it

class Organism

	var lvar = 10

	def self.type
		'organism'

	def initialize
		@ivar = 1
	
	def lineage
		'organism'
		
	def name
		'organism'

	def speak
		'ghaarg'

	def alive
		yes

	def lvar
		lvar

	# hmm, maybe we shouldnt allow this?
	#	class Other
	#
	#		def inner
	#			yes
		
class Virus < Organism

	def initialize
		@ivar = 2

	def lineage
		"{name}.{super.lineage}"

	def name
		'virus'

class Animal < Organism

	# def self.type
	#    "animal.{super}"

	def lineage
		"animal.{super.lineage}"

class Cat < Animal

	# def self.type
	#    "cat.{super}"

	def lineage
		"cat.{super.lineage}"

	def speak
		'miau'

class Dog < Animal

	# def self.type
	#    "dog.{super}"
	def lineage
		"dog.{super.lineage}"
		
	def speak
		'woff'
		

class Human < Animal

	def lineage
		"human.{super.lineage}"

	def speak
		'hello'

class Zombie < Human

	def lineage
		"zombie.{super.lineage}"

	def alive
		no


describe 'Syntax - Class' do

	# test 'nested classes work' do
	# 	ok !!Organism.Other

	test 'should' do

		# you can define variables local to classbody
		var obj = Organism.new
		eq obj.lvar, 10

	describe 'Methods' do

		it 'should define class methods' do
			eq Organism.type, 'organism'

		it 'should inherit class methods' do
			eq Virus:type, Organism:type

		# it 'should call super in class methods' do
		#   eq Dog.type, "dog.animal.organism"
		#   eq Cat.type, "cat.animal.organism"

	describe 'Instance' do

		it 'should call the parent constructor by default' do
			var obj = Cat.new
			eq obj.@ivar, 1

		it 'should define instance methods' do
			var obj = Organism.new
			var val = obj.alive
			# eq val, true
			ok obj.alive
			eq obj.speak, 'ghaarg'

		it 'should inherit instance methods' do
			var obj = Virus.new
			ok obj.alive


		it 'should override instance methods' do
			eq Organism.new.name, 'organism'
			eq Virus.new.name, 'virus'

		it 'should call super in instance methods' do
			# Should not refer to the prototype directly?
			eq Virus.new.lineage, 'virus.organism'
			eq Zombie.new.lineage, 'zombie.human.animal.organism'

	test 'define methods outside scope' do
		class Cls
			def self.a do 1
			def a do 2

		def Cls.b
			1

		extend class Cls
			def b
				2

		eq Cls.a, 1
		eq Cls.b, 1

		eq Cls.new.a, 2
		eq Cls.new.b, 2

	
	test 'Scoping' do

		var variable = 1

		local class A
			var variable = 2

			def self.base
				variable

			def self.add add
				variable += add

			def initialize add
				@sum = variable + add
				self

			def base
				variable

			def sum
				@sum

		eq variable, 1
		eq A.base, 2
		eq A.new.base, 2
		eq A.new(5).sum, 7

		A.add(2)

		eq variable, 1
    ###		eq A.base, 4 ###

###					
	test 'issue #71' do
		var res
		var def ping cb
			res = cb()

		class A
			ping do self

		eq res, A

###