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