Debian Codegraph @

tests/inputs/class.imba

raw
1 # https://github.com/imba/imba/raw/master/test/syntax/class.imba
2 extern describe, test, ok, eq, it
3
4 class Organism
5
6 var lvar = 10
7
8 def self.type
9 'organism'
10
11 def initialize
12 @ivar = 1
13
14 def lineage
15 'organism'
16
17 def name
18 'organism'
19
20 def speak
21 'ghaarg'
22
23 def alive
24 yes
25
26 def lvar
27 lvar
28
29 # hmm, maybe we shouldnt allow this?
30 # class Other
31 #
32 # def inner
33 # yes
34
35 class Virus < Organism
36
37 def initialize
38 @ivar = 2
39
40 def lineage
41 "{name}.{super.lineage}"
42
43 def name
44 'virus'
45
46 class Animal < Organism
47
48 # def self.type
49 # "animal.{super}"
50
51 def lineage
52 "animal.{super.lineage}"
53
54 class Cat < Animal
55
56 # def self.type
57 # "cat.{super}"
58
59 def lineage
60 "cat.{super.lineage}"
61
62 def speak
63 'miau'
64
65 class Dog < Animal
66
67 # def self.type
68 # "dog.{super}"
69 def lineage
70 "dog.{super.lineage}"
71
72 def speak
73 'woff'
74
75
76 class Human < Animal
77
78 def lineage
79 "human.{super.lineage}"
80
81 def speak
82 'hello'
83
84 class Zombie < Human
85
86 def lineage
87 "zombie.{super.lineage}"
88
89 def alive
90 no
91
92
93 describe 'Syntax - Class' do
94
95 # test 'nested classes work' do
96 # ok !!Organism.Other
97
98 test 'should' do
99
100 # you can define variables local to classbody
101 var obj = Organism.new
102 eq obj.lvar, 10
103
104 describe 'Methods' do
105
106 it 'should define class methods' do
107 eq Organism.type, 'organism'
108
109 it 'should inherit class methods' do
110 eq Virus:type, Organism:type
111
112 # it 'should call super in class methods' do
113 # eq Dog.type, "dog.animal.organism"
114 # eq Cat.type, "cat.animal.organism"
115
116 describe 'Instance' do
117
118 it 'should call the parent constructor by default' do
119 var obj = Cat.new
120 eq obj.@ivar, 1
121
122 it 'should define instance methods' do
123 var obj = Organism.new
124 var val = obj.alive
125 # eq val, true
126 ok obj.alive
127 eq obj.speak, 'ghaarg'
128
129 it 'should inherit instance methods' do
130 var obj = Virus.new
131 ok obj.alive
132
133
134 it 'should override instance methods' do
135 eq Organism.new.name, 'organism'
136 eq Virus.new.name, 'virus'
137
138 it 'should call super in instance methods' do
139 # Should not refer to the prototype directly?
140 eq Virus.new.lineage, 'virus.organism'
141 eq Zombie.new.lineage, 'zombie.human.animal.organism'
142
143 test 'define methods outside scope' do
144 class Cls
145 def self.a do 1
146 def a do 2
147
148 def Cls.b
149 1
150
151 extend class Cls
152 def b
153 2
154
155 eq Cls.a, 1
156 eq Cls.b, 1
157
158 eq Cls.new.a, 2
159 eq Cls.new.b, 2
160
161
162 test 'Scoping' do
163
164 var variable = 1
165
166 local class A
167 var variable = 2
168
169 def self.base
170 variable
171
172 def self.add add
173 variable += add
174
175 def initialize add
176 @sum = variable + add
177 self
178
179 def base
180 variable
181
182 def sum
183 @sum
184
185 eq variable, 1
186 eq A.base, 2
187 eq A.new.base, 2
188 eq A.new(5).sum, 7
189
190 A.add(2)
191
192 eq variable, 1
193 ### eq A.base, 4 ###
194
195 ###
196 test 'issue #71' do
197 var res
198 var def ping cb
199 res = cb()
200
201 class A
202 ping do self
203
204 eq res, A
205
206 ###
207
208
209