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