tests: use a Char in test_deserialization
[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 import json_serialization
19
20 # Simple class
21 class A
22 auto_serializable
23 super Serializable
24
25 var b: Bool
26 var c: Char
27 var f: Float
28 var i: Int
29 var s: String
30 var n: nullable Int
31
32 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int)
33 do
34 self.b = b
35 self.c = c
36 self.f = f
37 self.i = i
38 self.s = s
39 end
40
41 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
42 end
43
44 # Sub-class of A
45 class B
46 auto_serializable
47 super A
48
49 var ii: Int
50 var ss: String
51
52 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int, ii: Int, ss: String)
53 do
54 super(b, c, f, i, s, n)
55
56 self.ii = ii
57 self.ss = ss
58 end
59
60 redef fun to_s do return "<B: {super} {ii} {ss}>"
61 end
62
63 # Composed of an A and a B
64 class C
65 auto_serializable
66 super Serializable
67
68 var a: A
69 var b: B
70 var aa: A
71
72 init(a: A, b: B)
73 do
74 self.a = a
75 self.b = b
76 self.aa = a
77 end
78
79 redef fun to_s do return "<C: {a} {b}>"
80 end
81
82 # Class with cyclic reference
83 class D
84 auto_serializable
85 super B
86
87 var d: nullable D = null
88
89 redef fun to_s do return "<D: {super} {d != null}>"
90 end
91
92 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
93 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
94 var c = new C(a, b)
95 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
96 d.d = d
97
98 for o in new Array[nullable Serializable].with_items(a, b, c, d) do
99 var stream = new StringOStream
100 var serializer = new JsonSerializer(stream)
101 serializer.serialize(o)
102
103 var deserializer = new JsonDeserializer(stream.to_s)
104 var deserialized = deserializer.deserialize
105
106 print "# Nit:\n{o}\n"
107 print "# Json:\n{stream}\n"
108 print "# Back in Nit:\n{deserialized}\n"
109 end