Merge: doc: fixed some typos and other misc. corrections
[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: Bool
24 var c: Char
25 var f: Float
26 var i: Int
27 var s: String is serialize_as "serialization_specific_name"
28 var n: nullable Int
29 var password = "p4ssw0rd" is lazy, noserialize
30
31 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null} {password}>"
32 end
33
34 # Sub-class of A
35 class B
36 auto_serializable
37 super A
38
39 var ii: Int
40 var ss: String
41
42 redef fun to_s do return "<B: {super} {ii} {ss}>"
43 end
44
45 # Composed of an A and a B
46 class C
47 auto_serializable
48
49 var a: A
50 var b: B
51 var aa: A
52
53 redef fun to_s do return "<C: {a} {b}>"
54 end
55
56 # Class with cyclic reference
57 class D
58 auto_serializable
59 super B
60
61 var d: nullable D = null
62
63 redef fun to_s do return "<D: {super} {d != null}>"
64 end
65
66 # Class with generics
67 class E
68 auto_serializable
69
70 var a = new Array[Object].with_items("hello", 1234, 123.4)
71 var b = new Array[nullable Serializable].with_items("hella", 2345, 234.5)
72
73 redef fun to_s do return "<E: a: {a.join(", ")}; b: {b.join(", ")}>"
74 end
75
76 # Parameterized class
77 class F[N: Numeric]
78 auto_serializable
79
80 var n: N
81
82 redef fun to_s do return "<F: {n}>"
83 end
84
85 # Other collections
86 class G
87 auto_serializable
88
89 var hs = new HashSet[Int]
90 var s: Set[String] = new ArraySet[String]
91 var hm = new HashMap[String, Int]
92 var am = new ArrayMap[String, String]
93
94 init
95 do
96 hs.add -1
97 hs.add 0
98 s.add "one"
99 s.add "two"
100 hm["one"] = 1
101 hm["two"] = 2
102 am["three"] = 3.to_s
103 am["four"] = 4.to_s
104 end
105
106 redef fun to_s do return "<G: hs: {hs.join(", ")}; s: {s.join(", ")}; "+
107 "hm: {hm.join(", ", ". ")}; am: {am.join(", ", ". ")}>"
108 end
109
110 class TestEntities
111 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
112 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
113 var c = new C(a, b, a)
114 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
115 init do d.d = d
116 var e = new E
117 var fi = new F[Int](2222)
118 var ff = new F[Float](33.33)
119 var g = new G
120
121 # should work without nitserial
122 var without_generics: Array[Serializable] = [a, b, c, d: Serializable]
123
124 # Default works only with Nit serial
125 var with_generics: Array[Serializable] = [a, b, c, d, e, fi, ff, g: Serializable]
126 end
127
128 # We instantiate it here so that `nitserial` detects generic types as being alive
129 var entities = new TestEntities