tests: adds test for the serialization phase and json service
[nit.git] / tests / test_serialization.nit
1 import serialization
2 import json_serialization
3
4 # Simple class
5 class A
6 auto_serializable
7 super Serializable
8
9 var b: Bool
10 var c: Char
11 var f: Float
12 var i: Int
13 var s: String
14 var n: nullable Int
15
16 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int)
17 do
18 self.b = b
19 self.c = c
20 self.f = f
21 self.i = i
22 self.s = s
23 end
24
25 redef fun to_s do return "<A: {b} {c} {f} {i} {s} {n != null}>"
26 end
27
28 # Sub-class of A
29 class B
30 auto_serializable
31 super A
32
33 var ii: Int
34 var ss: String
35
36 init(b: Bool, c: Char, f: Float, i: Int, s: String, n: nullable Int, ii: Int, ss: String)
37 do
38 super(b, c, f, i, s, n)
39
40 self.ii = ii
41 self.ss = ss
42 end
43
44 redef fun to_s do return "<B: {super} {ii} {ss}>"
45 end
46
47 # Composed of an A and a B
48 class C
49 auto_serializable
50 super Serializable
51
52 var a: A
53 var b: B
54 var aa: A
55
56 init(a: A, b: B)
57 do
58 self.a = a
59 self.b = b
60 self.aa = a
61 end
62
63 redef fun to_s do return "<C: {a} {b}>"
64 end
65
66 # Class with cyclic reference
67 class D
68 auto_serializable
69 super B
70
71 var d: nullable D = null
72
73 redef fun to_s do return "<D: {super} {d != null}>"
74 end
75
76 var a = new A(true, 'a', 0.1234, 1234, "asdf", null)
77 var b = new B(false, 'b', 123.123, 2345, "hjkl", 12, 1111, "qwer")
78 var c = new C(a, b)
79 var d = new D(false, 'b', 123.123, 2345, "new line ->\n<-", null, 1111, "\t\f\"\r\\/")
80 d.d = d
81
82 for o in new Array[nullable Serializable].with_items(a, b, c, d) do
83 var stream = new StringOStream
84 var serializer = new JsonSerializer(stream)
85 serializer.serialize(o)
86
87 print "# Nit:\n{o}\n"
88 print "# Json:\n{stream}\n"
89 end