James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 20 | var ttypes = require('./gen-nodejs/JsDeepConstructorTest_types'); |
| 21 | var thrift = require('thrift'); |
| 22 | var test = require('tape'); |
| 23 | var bufferEquals = require('buffer-equals'); |
| 24 | |
| 25 | function serializeBinary(data) { |
| 26 | var buff; |
| 27 | var transport = new thrift.TBufferedTransport(null, function(msg){ |
| 28 | buff = msg; |
| 29 | }); |
| 30 | var prot = new thrift.TBinaryProtocol(transport); |
| 31 | data.write(prot); |
| 32 | prot.flush(); |
| 33 | return buff; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | |
| 38 | function deserializeBinary(serialized, type) { |
| 39 | var t = new thrift.TFramedTransport(serialized); |
| 40 | var p = new thrift.TBinaryProtocol(t); |
| 41 | var data = new type(); |
| 42 | data.read(p); |
| 43 | return data; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | function serializeJSON(data) { |
| 48 | var buff; |
| 49 | var transport = new thrift.TBufferedTransport(null, function(msg){ |
| 50 | buff = msg; |
| 51 | }); |
| 52 | var protocol = new thrift.TJSONProtocol(transport); |
| 53 | protocol.writeMessageBegin("", 0, 0); |
| 54 | data.write(protocol); |
| 55 | protocol.writeMessageEnd(); |
| 56 | protocol.flush(); |
| 57 | return buff; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | function deserializeJSON(serialized, type) { |
| 62 | var transport = new thrift.TFramedTransport(serialized); |
| 63 | var protocol = new thrift.TJSONProtocol(transport); |
| 64 | protocol.readMessageBegin(); |
| 65 | var data = new type(); |
| 66 | data.read(protocol); |
| 67 | protocol.readMessageEnd(); |
| 68 | return data; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | function createThriftObj() { |
| 73 | |
| 74 | return new ttypes.Complex({ |
| 75 | |
| 76 | struct_field: new ttypes.Simple({value: 'a'}), |
| 77 | |
| 78 | struct_list_field: [ |
| 79 | new ttypes.Simple({value: 'b'}), |
| 80 | new ttypes.Simple({value: 'c'}), |
| 81 | ], |
| 82 | |
| 83 | struct_set_field: [ |
| 84 | new ttypes.Simple({value: 'd'}), |
| 85 | new ttypes.Simple({value: 'e'}), |
| 86 | ], |
| 87 | |
| 88 | struct_map_field: { |
| 89 | A: new ttypes.Simple({value: 'f'}), |
| 90 | B: new ttypes.Simple({value: 'g'}) |
| 91 | }, |
| 92 | |
| 93 | struct_nested_containers_field: [ |
| 94 | [ |
| 95 | { |
| 96 | C: [ |
| 97 | new ttypes.Simple({value: 'h'}), |
| 98 | new ttypes.Simple({value: 'i'}) |
| 99 | ] |
| 100 | } |
| 101 | ] |
| 102 | ], |
| 103 | |
| 104 | struct_nested_containers_field2: { |
| 105 | D: [ |
| 106 | { |
| 107 | DA: new ttypes.Simple({value: 'j'}) |
| 108 | }, |
| 109 | { |
| 110 | DB: new ttypes.Simple({value: 'k'}) |
| 111 | } |
| 112 | ] |
Nobuaki Sukegawa | a400c69 | 2016-03-19 23:55:06 +0900 | [diff] [blame] | 113 | }, |
| 114 | |
| 115 | list_of_list_field: [ |
| 116 | ['l00', 'l01', 'l02'], |
| 117 | ['l10', 'l11', 'l12'], |
| 118 | ['l20', 'l21', 'l22'], |
| 119 | ], |
| 120 | |
| 121 | list_of_list_of_list_field: [ |
| 122 | [['m000', 'm001', 'm002'], ['m010', 'm011', 'm012'], ['m020', 'm021', 'm022']], |
| 123 | [['m100', 'm101', 'm102'], ['m110', 'm111', 'm112'], ['m120', 'm121', 'm122']], |
| 124 | [['m200', 'm201', 'm202'], ['m210', 'm211', 'm212'], ['m220', 'm221', 'm222']], |
| 125 | ], |
| 126 | |
| 127 | |
| 128 | }); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | function createJsObj() { |
| 133 | |
| 134 | return { |
| 135 | |
| 136 | struct_field: {value: 'a'}, |
| 137 | |
| 138 | struct_list_field: [ |
| 139 | {value: 'b'}, |
| 140 | {value: 'c'}, |
| 141 | ], |
| 142 | |
| 143 | struct_set_field: [ |
| 144 | {value: 'd'}, |
| 145 | {value: 'e'}, |
| 146 | ], |
| 147 | |
| 148 | struct_map_field: { |
| 149 | A: {value: 'f'}, |
| 150 | B: {value: 'g'} |
| 151 | }, |
| 152 | |
| 153 | struct_nested_containers_field: [ |
| 154 | [ |
| 155 | { |
| 156 | C: [ |
| 157 | {value: 'h'}, |
| 158 | {value: 'i'} |
| 159 | ] |
| 160 | } |
| 161 | ] |
| 162 | ], |
| 163 | |
| 164 | struct_nested_containers_field2: { |
| 165 | D: [ |
| 166 | { |
| 167 | DA: {value: 'j'} |
| 168 | }, |
| 169 | { |
| 170 | DB: {value: 'k'} |
| 171 | } |
| 172 | ] |
Nobuaki Sukegawa | a400c69 | 2016-03-19 23:55:06 +0900 | [diff] [blame] | 173 | }, |
| 174 | |
| 175 | list_of_list_field: [ |
| 176 | ['l00', 'l01', 'l02'], |
| 177 | ['l10', 'l11', 'l12'], |
| 178 | ['l20', 'l21', 'l22'], |
| 179 | ], |
| 180 | |
| 181 | list_of_list_of_list_field: [ |
| 182 | [['m000', 'm001', 'm002'], ['m010', 'm011', 'm012'], ['m020', 'm021', 'm022']], |
| 183 | [['m100', 'm101', 'm102'], ['m110', 'm111', 'm112'], ['m120', 'm121', 'm122']], |
| 184 | [['m200', 'm201', 'm202'], ['m210', 'm211', 'm212'], ['m220', 'm221', 'm222']], |
| 185 | ], |
| 186 | |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 187 | }; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | function assertValues(obj, assert) { |
| 192 | assert.equals(obj.struct_field.value, 'a'); |
| 193 | assert.equals(obj.struct_list_field[0].value, 'b'); |
| 194 | assert.equals(obj.struct_list_field[1].value, 'c'); |
| 195 | assert.equals(obj.struct_set_field[0].value, 'd'); |
| 196 | assert.equals(obj.struct_set_field[1].value, 'e'); |
| 197 | assert.equals(obj.struct_map_field.A.value, 'f'); |
| 198 | assert.equals(obj.struct_map_field.B.value, 'g'); |
| 199 | assert.equals(obj.struct_nested_containers_field[0][0].C[0].value, 'h'); |
| 200 | assert.equals(obj.struct_nested_containers_field[0][0].C[1].value, 'i'); |
| 201 | assert.equals(obj.struct_nested_containers_field2.D[0].DA.value, 'j'); |
| 202 | assert.equals(obj.struct_nested_containers_field2.D[1].DB.value, 'k'); |
Nobuaki Sukegawa | a400c69 | 2016-03-19 23:55:06 +0900 | [diff] [blame] | 203 | assert.equals(obj.list_of_list_field[0][0], 'l00'); |
| 204 | assert.equals(obj.list_of_list_field[0][1], 'l01'); |
| 205 | assert.equals(obj.list_of_list_field[0][2], 'l02'); |
| 206 | assert.equals(obj.list_of_list_field[1][0], 'l10'); |
| 207 | assert.equals(obj.list_of_list_field[1][1], 'l11'); |
| 208 | assert.equals(obj.list_of_list_field[1][2], 'l12'); |
| 209 | assert.equals(obj.list_of_list_field[2][0], 'l20'); |
| 210 | assert.equals(obj.list_of_list_field[2][1], 'l21'); |
| 211 | assert.equals(obj.list_of_list_field[2][2], 'l22'); |
| 212 | |
| 213 | assert.equals(obj.list_of_list_of_list_field[0][0][0], 'm000'); |
| 214 | assert.equals(obj.list_of_list_of_list_field[0][0][1], 'm001'); |
| 215 | assert.equals(obj.list_of_list_of_list_field[0][0][2], 'm002'); |
| 216 | assert.equals(obj.list_of_list_of_list_field[0][1][0], 'm010'); |
| 217 | assert.equals(obj.list_of_list_of_list_field[0][1][1], 'm011'); |
| 218 | assert.equals(obj.list_of_list_of_list_field[0][1][2], 'm012'); |
| 219 | assert.equals(obj.list_of_list_of_list_field[0][2][0], 'm020'); |
| 220 | assert.equals(obj.list_of_list_of_list_field[0][2][1], 'm021'); |
| 221 | assert.equals(obj.list_of_list_of_list_field[0][2][2], 'm022'); |
| 222 | |
| 223 | assert.equals(obj.list_of_list_of_list_field[1][0][0], 'm100'); |
| 224 | assert.equals(obj.list_of_list_of_list_field[1][0][1], 'm101'); |
| 225 | assert.equals(obj.list_of_list_of_list_field[1][0][2], 'm102'); |
| 226 | assert.equals(obj.list_of_list_of_list_field[1][1][0], 'm110'); |
| 227 | assert.equals(obj.list_of_list_of_list_field[1][1][1], 'm111'); |
| 228 | assert.equals(obj.list_of_list_of_list_field[1][1][2], 'm112'); |
| 229 | assert.equals(obj.list_of_list_of_list_field[1][2][0], 'm120'); |
| 230 | assert.equals(obj.list_of_list_of_list_field[1][2][1], 'm121'); |
| 231 | assert.equals(obj.list_of_list_of_list_field[1][2][2], 'm122'); |
| 232 | |
| 233 | assert.equals(obj.list_of_list_of_list_field[2][0][0], 'm200'); |
| 234 | assert.equals(obj.list_of_list_of_list_field[2][0][1], 'm201'); |
| 235 | assert.equals(obj.list_of_list_of_list_field[2][0][2], 'm202'); |
| 236 | assert.equals(obj.list_of_list_of_list_field[2][1][0], 'm210'); |
| 237 | assert.equals(obj.list_of_list_of_list_field[2][1][1], 'm211'); |
| 238 | assert.equals(obj.list_of_list_of_list_field[2][1][2], 'm212'); |
| 239 | assert.equals(obj.list_of_list_of_list_field[2][2][0], 'm220'); |
| 240 | assert.equals(obj.list_of_list_of_list_field[2][2][1], 'm221'); |
| 241 | assert.equals(obj.list_of_list_of_list_field[2][2][2], 'm222'); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | function createTestCases(serialize, deserialize) { |
| 245 | |
| 246 | var cases = { |
| 247 | |
| 248 | "Serialize/deserialize should return equal object": function(assert){ |
| 249 | var tObj = createThriftObj(); |
| 250 | var received = deserialize(serialize(tObj), ttypes.Complex); |
| 251 | assert.ok(tObj !== received, 'not the same object'); |
| 252 | assert.deepEqual(tObj, received); |
| 253 | assert.end(); |
| 254 | }, |
| 255 | |
| 256 | "Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects": function(assert) { |
| 257 | var tObj1 = createThriftObj(); |
| 258 | var tObj2 = new ttypes.Complex(createJsObj()); |
| 259 | assertValues(tObj2, assert); |
| 260 | var s1 = serialize(tObj1); |
| 261 | var s2 = serialize(tObj2); |
| 262 | assert.ok(bufferEquals(s1, s2)); |
| 263 | assert.end(); |
| 264 | }, |
| 265 | |
| 266 | "Modifications to args object should not affect constructed Thrift object": function (assert) { |
| 267 | |
| 268 | var args = createJsObj(); |
| 269 | assertValues(args, assert); |
| 270 | |
| 271 | var tObj = new ttypes.Complex(args); |
| 272 | assertValues(tObj, assert); |
| 273 | |
| 274 | args.struct_field.value = 'ZZZ'; |
| 275 | args.struct_list_field[0].value = 'ZZZ'; |
| 276 | args.struct_list_field[1].value = 'ZZZ'; |
| 277 | args.struct_set_field[0].value = 'ZZZ'; |
| 278 | args.struct_set_field[1].value = 'ZZZ'; |
| 279 | args.struct_map_field.A.value = 'ZZZ'; |
| 280 | args.struct_map_field.B.value = 'ZZZ'; |
| 281 | args.struct_nested_containers_field[0][0].C[0] = 'ZZZ'; |
| 282 | args.struct_nested_containers_field[0][0].C[1] = 'ZZZ'; |
| 283 | args.struct_nested_containers_field2.D[0].DA = 'ZZZ'; |
| 284 | args.struct_nested_containers_field2.D[0].DB = 'ZZZ'; |
| 285 | |
| 286 | assertValues(tObj, assert); |
| 287 | assert.end(); |
| 288 | }, |
| 289 | |
| 290 | "nulls are ok": function(assert) { |
| 291 | var tObj = new ttypes.Complex({ |
| 292 | struct_field: null, |
| 293 | struct_list_field: null, |
| 294 | struct_set_field: null, |
| 295 | struct_map_field: null, |
| 296 | struct_nested_containers_field: null, |
| 297 | struct_nested_containers_field2: null |
| 298 | }); |
| 299 | var received = deserialize(serialize(tObj), ttypes.Complex); |
Henrique Mendonça | 738143c | 2015-08-16 19:17:33 +1000 | [diff] [blame] | 300 | assert.strictEqual(tObj.struct_field, null); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 301 | assert.ok(tObj !== received); |
| 302 | assert.deepEqual(tObj, received); |
| 303 | assert.end(); |
Henrique Mendonça | 738143c | 2015-08-16 19:17:33 +1000 | [diff] [blame] | 304 | }, |
| 305 | |
| 306 | "Can make list with objects": function(assert) { |
| 307 | var tObj = new ttypes.ComplexList({ |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame^] | 308 | "struct_list_field": [new ttypes.Complex({})] |
Henrique Mendonça | 738143c | 2015-08-16 19:17:33 +1000 | [diff] [blame] | 309 | }); |
| 310 | var innerObj = tObj.struct_list_field[0]; |
| 311 | assert.ok(innerObj instanceof ttypes.Complex) |
| 312 | assert.strictEqual(innerObj.struct_field, null); |
| 313 | assert.strictEqual(innerObj.struct_list_field, null); |
| 314 | assert.strictEqual(innerObj.struct_set_field, null); |
| 315 | assert.strictEqual(innerObj.struct_map_field, null); |
| 316 | assert.strictEqual(innerObj.struct_nested_containers_field, null); |
| 317 | assert.strictEqual(innerObj.struct_nested_containers_field2, null); |
| 318 | assert.end(); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | }; |
| 322 | return cases; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | function run(name, cases){ |
| 327 | Object.keys(cases).forEach(function(caseName) { |
| 328 | test(name + ': ' + caseName, cases[caseName]); |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | run('binary', createTestCases(serializeBinary, deserializeBinary)); |
| 333 | run('json', createTestCases(serializeJSON, deserializeJSON)); |