Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_deriving.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 deriving
16
17 redef class Object
18 # Redef to avoid unstable `object_id`
19 redef fun inspect_head do return "{class_name}"
20 end
21
22 class A
23 auto_inspect
24 auto_derive
25 super DeriveToS#alt1#
26 super DeriveEqual#alt2#
27
28 var i: Int
29 var s: String
30 end
31
32 class A2
33 super DeriveToS
34 super DeriveEqual
35
36 redef fun derive_to_map
37 do
38 var res = super
39 res["string"] = s # drop i
40 return res
41 end
42
43 var i: Int
44 var s: String
45 end
46
47 class B
48 super A#alt4#super A2
49
50 auto_inspect
51 auto_derive#alt3#
52
53 var a: A
54 end
55
56 var a = new A(5, "Hello")
57 print a.inspect
58 print a.derive_to_map.join(" ", "=")
59 print a
60
61 print ""
62
63 var a2 = new A(5, "Hel" + "lo")
64 var a3 = new A(6, "Hel" + "lo")
65 print a == a2
66 print a.hash == a2.hash
67 print a != a3
68
69 print ""
70
71 var b = new B(100, "World", a)
72 print b.inspect
73 print b.derive_to_map.join(" ", "=")
74 print b
75
76 print ""
77
78 var b2 = new B(100, "World", a2)
79 var b3 = new B(100, "World", a3)
80 print b == b2
81 print b.hash == b2.hash
82 print b != b3