blob: 41e7dd0b62e83cdd805082d4436f5c15b84645d0 [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 ]
94 }
95 }
96 );
97}
98
99
100function createJsObj() {
101
102 return {
103
104 struct_field: {value: 'a'},
105
106 struct_list_field: [
107 {value: 'b'},
108 {value: 'c'},
109 ],
110
111 struct_set_field: [
112 {value: 'd'},
113 {value: 'e'},
114 ],
115
116 struct_map_field: {
117 A: {value: 'f'},
118 B: {value: 'g'}
119 },
120
121 struct_nested_containers_field: [
122 [
123 {
124 C: [
125 {value: 'h'},
126 {value: 'i'}
127 ]
128 }
129 ]
130 ],
131
132 struct_nested_containers_field2: {
133 D: [
134 {
135 DA: {value: 'j'}
136 },
137 {
138 DB: {value: 'k'}
139 }
140 ]
141 }
142 };
143}
144
145
146function assertValues(obj, assert) {
147 assert.equals(obj.struct_field.value, 'a');
148 assert.equals(obj.struct_list_field[0].value, 'b');
149 assert.equals(obj.struct_list_field[1].value, 'c');
150 assert.equals(obj.struct_set_field[0].value, 'd');
151 assert.equals(obj.struct_set_field[1].value, 'e');
152 assert.equals(obj.struct_map_field.A.value, 'f');
153 assert.equals(obj.struct_map_field.B.value, 'g');
154 assert.equals(obj.struct_nested_containers_field[0][0].C[0].value, 'h');
155 assert.equals(obj.struct_nested_containers_field[0][0].C[1].value, 'i');
156 assert.equals(obj.struct_nested_containers_field2.D[0].DA.value, 'j');
157 assert.equals(obj.struct_nested_containers_field2.D[1].DB.value, 'k');
158}
159
160function createTestCases(serialize, deserialize) {
161
162 var cases = {
163
164 "Serialize/deserialize should return equal object": function(assert){
165 var tObj = createThriftObj();
166 var received = deserialize(serialize(tObj), ttypes.Complex);
167 assert.ok(tObj !== received, 'not the same object');
168 assert.deepEqual(tObj, received);
169 assert.end();
170 },
171
172 "Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects": function(assert) {
173 var tObj1 = createThriftObj();
174 var tObj2 = new ttypes.Complex(createJsObj());
175 assertValues(tObj2, assert);
176 var s1 = serialize(tObj1);
177 var s2 = serialize(tObj2);
178 assert.ok(bufferEquals(s1, s2));
179 assert.end();
180 },
181
182 "Modifications to args object should not affect constructed Thrift object": function (assert) {
183
184 var args = createJsObj();
185 assertValues(args, assert);
186
187 var tObj = new ttypes.Complex(args);
188 assertValues(tObj, assert);
189
190 args.struct_field.value = 'ZZZ';
191 args.struct_list_field[0].value = 'ZZZ';
192 args.struct_list_field[1].value = 'ZZZ';
193 args.struct_set_field[0].value = 'ZZZ';
194 args.struct_set_field[1].value = 'ZZZ';
195 args.struct_map_field.A.value = 'ZZZ';
196 args.struct_map_field.B.value = 'ZZZ';
197 args.struct_nested_containers_field[0][0].C[0] = 'ZZZ';
198 args.struct_nested_containers_field[0][0].C[1] = 'ZZZ';
199 args.struct_nested_containers_field2.D[0].DA = 'ZZZ';
200 args.struct_nested_containers_field2.D[0].DB = 'ZZZ';
201
202 assertValues(tObj, assert);
203 assert.end();
204 },
205
206 "nulls are ok": function(assert) {
207 var tObj = new ttypes.Complex({
208 struct_field: null,
209 struct_list_field: null,
210 struct_set_field: null,
211 struct_map_field: null,
212 struct_nested_containers_field: null,
213 struct_nested_containers_field2: null
214 });
215 var received = deserialize(serialize(tObj), ttypes.Complex);
216 assert.ok(tObj !== received);
217 assert.deepEqual(tObj, received);
218 assert.end();
219 }
220
221 };
222 return cases;
223}
224
225
226function run(name, cases){
227 Object.keys(cases).forEach(function(caseName) {
228 test(name + ': ' + caseName, cases[caseName]);
229 });
230}
231
232run('binary', createTestCases(serializeBinary, deserializeBinary));
233run('json', createTestCases(serializeJSON, deserializeJSON));