blob: 9a19809430431358cb389e35b0970434ccc438c2 [file] [log] [blame]
Henrique Mendonça15d90422015-06-25 22:31:41 +10001function serialize(data) {
2 var transport = new Thrift.Transport("/service");
3 var protocol = new Thrift.Protocol(transport);
4 protocol.writeMessageBegin("", 0, 0);
5 data.write(protocol);
6 protocol.writeMessageEnd();
7 return transport.send_buf;
8}
9
10function deserialize(serialized, type) {
11 var transport = new Thrift.Transport("/service");
12 transport.setRecvBuffer(serialized);
13 var protocol = new Thrift.Protocol(transport);
14 protocol.readMessageBegin();
15 var data = new type();
16 data.read(protocol);
17 protocol.readMessageEnd();
18 return data;
19}
20
21
22function createThriftObj() {
23
24 return new Complex({
25
26 struct_field: new Simple({value: 'a'}),
27
28 struct_list_field: [
29 new Simple({value: 'b'}),
30 new Simple({value: 'c'}),
31 ],
32
33 struct_set_field: [
34 new Simple({value: 'd'}),
35 new Simple({value: 'e'}),
36 ],
37
38 struct_map_field: {
39 A: new Simple({value: 'f'}),
40 B: new Simple({value: 'g'})
41 },
42
43 struct_nested_containers_field: [
44 [
45 {
46 C: [
47 new Simple({value: 'h'}),
48 new Simple({value: 'i'})
49 ]
50 }
51 ]
52 ],
53
54
55 struct_nested_containers_field2: {
56 D: [
57 {
58 DA: new Simple({value: 'j'})
59 },
60 {
61 DB: new Simple({value: 'k'})
62 }
63 ]
64 }
65 }
66 );
67}
68
69
70function createJsObj() {
71
72 return {
73
74 struct_field: {value: 'a'},
75
76 struct_list_field: [
77 {value: 'b'},
78 {value: 'c'},
79 ],
80
81 struct_set_field: [
82 {value: 'd'},
83 {value: 'e'},
84 ],
85
86 struct_map_field: {
87 A: {value: 'f'},
88 B: {value: 'g'}
89 },
90
91 struct_nested_containers_field: [
92 [
93 {
94 C: [
95 {value: 'h'},
96 {value: 'i'}
97 ]
98 }
99 ]
100 ],
101
102 struct_nested_containers_field2: {
103 D: [
104 {
105 DA: {value: 'j'}
106 },
107 {
108 DB: {value: 'k'}
109 }
110 ]
111 }
112 };
113}
114
115
116function assertValues(obj, assert) {
117 assert.equal(obj.struct_field.value, 'a');
118 assert.equal(obj.struct_list_field[0].value, 'b');
119 assert.equal(obj.struct_list_field[1].value, 'c');
120 assert.equal(obj.struct_set_field[0].value, 'd');
121 assert.equal(obj.struct_set_field[1].value, 'e');
122 assert.equal(obj.struct_map_field.A.value, 'f');
123 assert.equal(obj.struct_map_field.B.value, 'g');
124 assert.equal(obj.struct_nested_containers_field[0][0].C[0].value, 'h');
125 assert.equal(obj.struct_nested_containers_field[0][0].C[1].value, 'i');
126 assert.equal(obj.struct_nested_containers_field2.D[0].DA.value, 'j');
127 assert.equal(obj.struct_nested_containers_field2.D[1].DB.value, 'k');
128}
129
130var cases = {
131
132 "Serialize/deserialize simple struct should return equal object": function(assert){
133 var tObj = new Simple({value: 'a'});
134 var received = deserialize(serialize(tObj), Simple);
135 assert.ok(tObj !== received);
136 assert.deepEqual(received, tObj);
137 },
138
139
140 "Serialize/deserialize should return equal object": function(assert){
141 var tObj = createThriftObj();
142 var received = deserialize(serialize(tObj), Complex);
143 assert.ok(tObj !== received);
144 assert.deepEqual(received, tObj);
145 },
146
147 "Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects": function(assert) {
148 var tObj1 = createThriftObj();
149 var tObj2 = new Complex(createJsObj());
150 assertValues(tObj2, assert);
151 assert.equal(serialize(tObj2), serialize(tObj1));
152 },
153
154 "Modifications to args object should not affect constructed Thrift object": function (assert) {
155
156 var args = createJsObj();
157 assertValues(args, assert);
158
159 var tObj = new Complex(args);
160 assertValues(tObj, assert);
161
162 args.struct_field.value = 'ZZZ';
163 args.struct_list_field[0].value = 'ZZZ';
164 args.struct_list_field[1].value = 'ZZZ';
165 args.struct_set_field[0].value = 'ZZZ';
166 args.struct_set_field[1].value = 'ZZZ';
167 args.struct_map_field.A.value = 'ZZZ';
168 args.struct_map_field.B.value = 'ZZZ';
169 args.struct_nested_containers_field[0][0].C[0] = 'ZZZ';
170 args.struct_nested_containers_field[0][0].C[1] = 'ZZZ';
171 args.struct_nested_containers_field2.D[0].DA = 'ZZZ';
172 args.struct_nested_containers_field2.D[0].DB = 'ZZZ';
173
174 assertValues(tObj, assert);
175 },
176
177 "nulls are ok": function(assert) {
178 var tObj = new Complex({
179 struct_field: null,
180 struct_list_field: null,
181 struct_set_field: null,
182 struct_map_field: null,
183 struct_nested_containers_field: null,
184 struct_nested_containers_field2: null
185 });
186 var received = deserialize(serialize(tObj), Complex);
187 assert.ok(tObj !== received);
188 assert.deepEqual(tObj, received);
189 }
190
191};
192
193Object.keys(cases).forEach(function(caseName) {
194 test(caseName, cases[caseName]);
195});