Makefile&gitignore: remove .nit_compile
[nit.git] / tests / test_deserialization.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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 serialization
18
19 # Simple class
20 class A
21 auto_serializable
22
23 var b = false
24 var c: Char
25 var f: Float
26 var i = 123
27 var s = "asdf"
28 var n: nullable Int
29
30 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int)
31 do
32 self.b = b
33 self.c = c
34 self.f = f
35 self.i = i
36 self.s = s
37 end
38
39 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
40 end
41
42 # Sub-class of A
43 class B
44 auto_serializable
45 super A
46
47 var ii: Int
48 var ss: String
49
50 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int, ii: Int, ss: String)
51 do
52 super(b, c, f, i, s, n)
53
54 self.ii = ii
55 self.ss = ss
56 end
57
58 redef fun to_s do return "<B: {super} {ii} {ss}>"
59 end
60
61 # Composed of an A and a B
62 class C
63 auto_serializable
64
65 var a: A
66 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
67 var aa: A
68
69 init(a: A, b: B)
70 do
71 self.a = a
72 self.b = b
73 self.aa = a
74 end
75
76 redef fun to_s do return "<C: {a} {b}>"
77 end
78
79 # Class with cyclic reference
80 class D
81 auto_serializable
82 super B
83
84 var d: nullable D = null
85
86 redef fun to_s do return "<D: {super} {d != null}>"
87 end
88
89 # Class with generics
90 class E
91 auto_serializable
92
93 var a = new Array[Object].with_items("hello", 1234, 123.4)
94 var b = new Array[nullable Serializable].with_items("hella", 2345, 234.5)
95
96 redef fun to_s do return "<E: a: {a.join(", ")}; b: {b.join(", ")}>"
97 end
98
99 # Parameterized class
100 class F[N: Numeric]
101 auto_serializable
102
103 var n: N
104
105 redef fun to_s do return "<E: {n}>"
106 end
107
108 # Other collections
109 class G
110 auto_serializable
111
112 var hs = new HashSet[Int]
113 var s: Set[String] = new ArraySet[String]
114 var hm = new HashMap[String, Int]
115 var am = new ArrayMap[String, String]
116
117 init
118 do
119 hs.add -1
120 hs.add 0
121 s.add "one"
122 s.add "two"
123 hm["one"] = 1
124 hm["two"] = 2
125 am["three"] = 3.to_s
126 am["four"] = 4.to_s
127 end
128
129 redef fun to_s do return "<G: hs: {hs.join(", ")}; s: {s.join(", ")}; "+
130 "hm: {hm.join(", ", ". ")}; am: {am.join(", ", ". ")}>"
131 end
132
133 class TestEntities
134 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
135 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
136 var c = new C(a, b)
137 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
138 init do d.d = d
139 var e = new E
140 var fi = new F[Int](2222)
141 var ff = new F[Float](33.33)
142 var g = new G
143
144 # should work without nitserial
145 var without_generics: Array[Serializable] = [a, b, c, d: Serializable]
146
147 # Default works only with Nit serial
148 var with_generics: Array[Serializable] = [a, b, c, d, e, fi, ff, g: Serializable]
149 end
150
151 # We instanciate it here so that `nitserial` detects generic types as being alive
152 var entities = new TestEntities