blob: 2caeb824dc3ed7c68f304b59c4275e828d04b89f [file] [log] [blame]
Henrique Mendonça15d90422015-06-25 22:31:41 +10001var ttypes = require('./gen-nodejs/JsDeepConstructorTest_types');
2var thrift = require('thrift');
3var test = require('tape');
4var bufferEquals = require('buffer-equals');
5
6function serializeBinary(data) {
7 var buff;
8 var transport = new thrift.TBufferedTransport(null, function(msg){
9 buff = msg;
10 });
11 var prot = new thrift.TBinaryProtocol(transport);
12 data.write(prot);
13 prot.flush();
14 return buff;
15
16}
17
18
19function deserializeBinary(serialized, type) {
20 var t = new thrift.TFramedTransport(serialized);
21 var p = new thrift.TBinaryProtocol(t);
22 var data = new type();
23 data.read(p);
24 return data;
25}
26
27
28function serializeJSON(data) {
29 var buff;
30 var transport = new thrift.TBufferedTransport(null, function(msg){
31 buff = msg;
32 });
33 var protocol = new thrift.TJSONProtocol(transport);
34 protocol.writeMessageBegin("", 0, 0);
35 data.write(protocol);
36 protocol.writeMessageEnd();
37 protocol.flush();
38 return buff;
39}
40
41
42function deserializeJSON(serialized, type) {
43 var transport = new thrift.TFramedTransport(serialized);
44 var protocol = new thrift.TJSONProtocol(transport);
45 protocol.readMessageBegin();
46 var data = new type();
47 data.read(protocol);
48 protocol.readMessageEnd();
49 return data;
50}
51
52
53function createThriftObj() {
54
55 return new ttypes.Complex({
56
57 struct_field: new ttypes.Simple({value: 'a'}),
58
59 struct_list_field: [
60 new ttypes.Simple({value: 'b'}),
61 new ttypes.Simple({value: 'c'}),
62 ],
63
64 struct_set_field: [
65 new ttypes.Simple({value: 'd'}),
66 new ttypes.Simple({value: 'e'}),
67 ],
68
69 struct_map_field: {
70 A: new ttypes.Simple({value: 'f'}),
71 B: new ttypes.Simple({value: 'g'})
72 },
73
74 struct_nested_containers_field: [
75 [
76 {
77 C: [
78 new ttypes.Simple({value: 'h'}),
79 new ttypes.Simple({value: 'i'})
80 ]
81 }
82 ]
83 ],
84
85 struct_nested_containers_field2: {
86 D: [
87 {
88 DA: new ttypes.Simple({value: 'j'})
89 },
90 {
91 DB: new ttypes.Simple({value: 'k'})
92 }
93 ]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +090094 },
95
96 list_of_list_field: [
97 ['l00', 'l01', 'l02'],
98 ['l10', 'l11', 'l12'],
99 ['l20', 'l21', 'l22'],
100 ],
101
102 list_of_list_of_list_field: [
103 [['m000', 'm001', 'm002'], ['m010', 'm011', 'm012'], ['m020', 'm021', 'm022']],
104 [['m100', 'm101', 'm102'], ['m110', 'm111', 'm112'], ['m120', 'm121', 'm122']],
105 [['m200', 'm201', 'm202'], ['m210', 'm211', 'm212'], ['m220', 'm221', 'm222']],
106 ],
107
108
109 });
Henrique Mendonça15d90422015-06-25 22:31:41 +1000110}
111
112
113function createJsObj() {
114
115 return {
116
117 struct_field: {value: 'a'},
118
119 struct_list_field: [
120 {value: 'b'},
121 {value: 'c'},
122 ],
123
124 struct_set_field: [
125 {value: 'd'},
126 {value: 'e'},
127 ],
128
129 struct_map_field: {
130 A: {value: 'f'},
131 B: {value: 'g'}
132 },
133
134 struct_nested_containers_field: [
135 [
136 {
137 C: [
138 {value: 'h'},
139 {value: 'i'}
140 ]
141 }
142 ]
143 ],
144
145 struct_nested_containers_field2: {
146 D: [
147 {
148 DA: {value: 'j'}
149 },
150 {
151 DB: {value: 'k'}
152 }
153 ]
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900154 },
155
156 list_of_list_field: [
157 ['l00', 'l01', 'l02'],
158 ['l10', 'l11', 'l12'],
159 ['l20', 'l21', 'l22'],
160 ],
161
162 list_of_list_of_list_field: [
163 [['m000', 'm001', 'm002'], ['m010', 'm011', 'm012'], ['m020', 'm021', 'm022']],
164 [['m100', 'm101', 'm102'], ['m110', 'm111', 'm112'], ['m120', 'm121', 'm122']],
165 [['m200', 'm201', 'm202'], ['m210', 'm211', 'm212'], ['m220', 'm221', 'm222']],
166 ],
167
Henrique Mendonça15d90422015-06-25 22:31:41 +1000168 };
169}
170
171
172function assertValues(obj, assert) {
173 assert.equals(obj.struct_field.value, 'a');
174 assert.equals(obj.struct_list_field[0].value, 'b');
175 assert.equals(obj.struct_list_field[1].value, 'c');
176 assert.equals(obj.struct_set_field[0].value, 'd');
177 assert.equals(obj.struct_set_field[1].value, 'e');
178 assert.equals(obj.struct_map_field.A.value, 'f');
179 assert.equals(obj.struct_map_field.B.value, 'g');
180 assert.equals(obj.struct_nested_containers_field[0][0].C[0].value, 'h');
181 assert.equals(obj.struct_nested_containers_field[0][0].C[1].value, 'i');
182 assert.equals(obj.struct_nested_containers_field2.D[0].DA.value, 'j');
183 assert.equals(obj.struct_nested_containers_field2.D[1].DB.value, 'k');
Nobuaki Sukegawaa400c692016-03-19 23:55:06 +0900184 assert.equals(obj.list_of_list_field[0][0], 'l00');
185 assert.equals(obj.list_of_list_field[0][1], 'l01');
186 assert.equals(obj.list_of_list_field[0][2], 'l02');
187 assert.equals(obj.list_of_list_field[1][0], 'l10');
188 assert.equals(obj.list_of_list_field[1][1], 'l11');
189 assert.equals(obj.list_of_list_field[1][2], 'l12');
190 assert.equals(obj.list_of_list_field[2][0], 'l20');
191 assert.equals(obj.list_of_list_field[2][1], 'l21');
192 assert.equals(obj.list_of_list_field[2][2], 'l22');
193
194 assert.equals(obj.list_of_list_of_list_field[0][0][0], 'm000');
195 assert.equals(obj.list_of_list_of_list_field[0][0][1], 'm001');
196 assert.equals(obj.list_of_list_of_list_field[0][0][2], 'm002');
197 assert.equals(obj.list_of_list_of_list_field[0][1][0], 'm010');
198 assert.equals(obj.list_of_list_of_list_field[0][1][1], 'm011');
199 assert.equals(obj.list_of_list_of_list_field[0][1][2], 'm012');
200 assert.equals(obj.list_of_list_of_list_field[0][2][0], 'm020');
201 assert.equals(obj.list_of_list_of_list_field[0][2][1], 'm021');
202 assert.equals(obj.list_of_list_of_list_field[0][2][2], 'm022');
203
204 assert.equals(obj.list_of_list_of_list_field[1][0][0], 'm100');
205 assert.equals(obj.list_of_list_of_list_field[1][0][1], 'm101');
206 assert.equals(obj.list_of_list_of_list_field[1][0][2], 'm102');
207 assert.equals(obj.list_of_list_of_list_field[1][1][0], 'm110');
208 assert.equals(obj.list_of_list_of_list_field[1][1][1], 'm111');
209 assert.equals(obj.list_of_list_of_list_field[1][1][2], 'm112');
210 assert.equals(obj.list_of_list_of_list_field[1][2][0], 'm120');
211 assert.equals(obj.list_of_list_of_list_field[1][2][1], 'm121');
212 assert.equals(obj.list_of_list_of_list_field[1][2][2], 'm122');
213
214 assert.equals(obj.list_of_list_of_list_field[2][0][0], 'm200');
215 assert.equals(obj.list_of_list_of_list_field[2][0][1], 'm201');
216 assert.equals(obj.list_of_list_of_list_field[2][0][2], 'm202');
217 assert.equals(obj.list_of_list_of_list_field[2][1][0], 'm210');
218 assert.equals(obj.list_of_list_of_list_field[2][1][1], 'm211');
219 assert.equals(obj.list_of_list_of_list_field[2][1][2], 'm212');
220 assert.equals(obj.list_of_list_of_list_field[2][2][0], 'm220');
221 assert.equals(obj.list_of_list_of_list_field[2][2][1], 'm221');
222 assert.equals(obj.list_of_list_of_list_field[2][2][2], 'm222');
Henrique Mendonça15d90422015-06-25 22:31:41 +1000223}
224
225function createTestCases(serialize, deserialize) {
226
227 var cases = {
228
229 "Serialize/deserialize should return equal object": function(assert){
230 var tObj = createThriftObj();
231 var received = deserialize(serialize(tObj), ttypes.Complex);
232 assert.ok(tObj !== received, 'not the same object');
233 assert.deepEqual(tObj, received);
234 assert.end();
235 },
236
237 "Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects": function(assert) {
238 var tObj1 = createThriftObj();
239 var tObj2 = new ttypes.Complex(createJsObj());
240 assertValues(tObj2, assert);
241 var s1 = serialize(tObj1);
242 var s2 = serialize(tObj2);
243 assert.ok(bufferEquals(s1, s2));
244 assert.end();
245 },
246
247 "Modifications to args object should not affect constructed Thrift object": function (assert) {
248
249 var args = createJsObj();
250 assertValues(args, assert);
251
252 var tObj = new ttypes.Complex(args);
253 assertValues(tObj, assert);
254
255 args.struct_field.value = 'ZZZ';
256 args.struct_list_field[0].value = 'ZZZ';
257 args.struct_list_field[1].value = 'ZZZ';
258 args.struct_set_field[0].value = 'ZZZ';
259 args.struct_set_field[1].value = 'ZZZ';
260 args.struct_map_field.A.value = 'ZZZ';
261 args.struct_map_field.B.value = 'ZZZ';
262 args.struct_nested_containers_field[0][0].C[0] = 'ZZZ';
263 args.struct_nested_containers_field[0][0].C[1] = 'ZZZ';
264 args.struct_nested_containers_field2.D[0].DA = 'ZZZ';
265 args.struct_nested_containers_field2.D[0].DB = 'ZZZ';
266
267 assertValues(tObj, assert);
268 assert.end();
269 },
270
271 "nulls are ok": function(assert) {
272 var tObj = new ttypes.Complex({
273 struct_field: null,
274 struct_list_field: null,
275 struct_set_field: null,
276 struct_map_field: null,
277 struct_nested_containers_field: null,
278 struct_nested_containers_field2: null
279 });
280 var received = deserialize(serialize(tObj), ttypes.Complex);
Henrique Mendonça738143c2015-08-16 19:17:33 +1000281 assert.strictEqual(tObj.struct_field, null);
Henrique Mendonça15d90422015-06-25 22:31:41 +1000282 assert.ok(tObj !== received);
283 assert.deepEqual(tObj, received);
284 assert.end();
Henrique Mendonça738143c2015-08-16 19:17:33 +1000285 },
286
287 "Can make list with objects": function(assert) {
288 var tObj = new ttypes.ComplexList({
289 "struct_list_field": [new ttypes.Complex({})]
290 });
291 var innerObj = tObj.struct_list_field[0];
292 assert.ok(innerObj instanceof ttypes.Complex)
293 assert.strictEqual(innerObj.struct_field, null);
294 assert.strictEqual(innerObj.struct_list_field, null);
295 assert.strictEqual(innerObj.struct_set_field, null);
296 assert.strictEqual(innerObj.struct_map_field, null);
297 assert.strictEqual(innerObj.struct_nested_containers_field, null);
298 assert.strictEqual(innerObj.struct_nested_containers_field2, null);
299 assert.end();
Henrique Mendonça15d90422015-06-25 22:31:41 +1000300 }
301
302 };
303 return cases;
304}
305
306
307function run(name, cases){
308 Object.keys(cases).forEach(function(caseName) {
309 test(name + ': ' + caseName, cases[caseName]);
310 });
311}
312
313run('binary', createTestCases(serializeBinary, deserializeBinary));
314run('json', createTestCases(serializeJSON, deserializeJSON));