Callref bugfix in interpreter and compilers + autosav
[nit.git] / tests / test_callref.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import functional
16
17 redef class Object
18 fun toto(x: Int): Int
19 do
20 return x + 1
21 end
22 end
23
24 redef class Int
25 redef fun toto(x) do return x + self
26
27 fun mult_by(x: Int): Int do return x * self
28 end
29
30 class A
31 fun fun1: String
32 do
33 return "in A::fun1"
34 end
35 end
36
37 class B
38 super A
39
40 redef fun fun1
41 do
42 return "in B::fun1"
43 end
44 end
45
46 class Counter
47 var x = 0
48 fun incr do x += 1
49 end
50
51 class C[E]
52 var x: E
53 redef fun to_s
54 do
55 if x != null then
56 return "x is {x.as(not null)}"
57 end
58 return "x is null"
59 end
60
61 fun bar: C[E] do return self
62 fun foo: Fun0[C[E]] do return &bar
63 end
64
65 var a = new A
66 var b: A = new B
67
68 var f1 = &a.fun1
69 print f1.call # "in A::fun1"
70
71 var f2 = &b.fun1
72 print f2.call # "in B::fun1"
73
74 var f3 = &10.mult_by
75 print f3.call(10) # 100
76
77 var f4 = &f2.call
78 print f4.call # "in B::fun1"
79
80 var f5: Fun0[Object] = &f4.call
81 print f5.call
82 print f5.call
83
84 print((&10.toto).call(100)) # 110
85 print((&"allo".toto).call(2)) # 3
86
87 var cnt = new Counter
88 var p1 = &cnt.incr
89 var ps = [p1,p1,p1,p1,p1]
90
91 for p in ps do p.call
92 print cnt.x # 5
93
94 var c1 = new C[nullable Object](null)
95 var c2 = new C[nullable Int](null)
96
97 var f6 = &c1.to_s
98 var f7 = &c2.to_s
99
100 print f6.call # "x is null"
101 print f7.call # "x is null"
102
103 c1.x = "test"
104 c2.x = 100
105
106 print f6.call # "x is test"
107 print f7.call # "x is 100"
108
109 var f8 = c2.foo
110 print f8.call # "x is 100"