tests: add base_safe.nit for safe calls
[nit.git] / tests / base_safe.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 core::kernel
16
17 class A
18 fun foo(i: Int) do
19 'F'.output
20 i.output
21 end
22
23 fun bar(i: Int): A do
24 'B'.output
25 i.output
26 return self
27 end
28 end
29
30 fun test(a: nullable A) do
31 'a'.output
32 (a == null).output
33 a?.foo(1)
34
35 var r = a?.bar(2)
36 'r'.output
37 (r == null).output
38
39 a?.bar(10)?.bar(11)?.foo(12)
40 #alt2#a.bar(20).foo(21)
41 #alt3#a?.bar(20).foo(21)
42 #alt4#a.bar(20)?.foo(21)
43 end
44
45 var a = new A
46 test(a)
47 '\n'.output
48 test(null)
49
50 #alt1#a?.foo(10)
51 #alt1#null?.foo(11)