Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 1 | function serialize(data) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 2 | const transport = new Thrift.Transport('/service'); |
| 3 | const protocol = new Thrift.Protocol(transport); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 4 | protocol.writeMessageBegin('', 0, 0); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 5 | data.write(protocol); |
| 6 | protocol.writeMessageEnd(); |
| 7 | return transport.send_buf; |
| 8 | } |
| 9 | |
| 10 | function deserialize(serialized, type) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 11 | const transport = new Thrift.Transport('/service'); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 12 | transport.setRecvBuffer(serialized); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 13 | const protocol = new Thrift.Protocol(transport); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 14 | protocol.readMessageBegin(); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 15 | const data = new type(); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 16 | data.read(protocol); |
| 17 | protocol.readMessageEnd(); |
| 18 | return data; |
| 19 | } |
| 20 | |
| 21 | |
| 22 | function createThriftObj() { |
| 23 | |
| 24 | return new Complex({ |
| 25 | |
| 26 | struct_field: new Simple({value: 'a'}), |
| 27 | |
| 28 | struct_list_field: [ |
| 29 | new Simple({value: 'b'}), |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 30 | new Simple({value: 'c'}) |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 31 | ], |
| 32 | |
| 33 | struct_set_field: [ |
| 34 | new Simple({value: 'd'}), |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 35 | new Simple({value: 'e'}) |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 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 | ] |
Philip Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 64 | }, |
| 65 | |
| 66 | list_of_list_field: [ |
| 67 | ['one', 'two'], |
| 68 | ['three', 'four'], |
| 69 | ['five', 'six'] |
| 70 | ] |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 71 | } |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | function createJsObj() { |
| 77 | |
| 78 | return { |
| 79 | |
| 80 | struct_field: {value: 'a'}, |
| 81 | |
| 82 | struct_list_field: [ |
| 83 | {value: 'b'}, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 84 | {value: 'c'} |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 85 | ], |
| 86 | |
| 87 | struct_set_field: [ |
| 88 | {value: 'd'}, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 89 | {value: 'e'} |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 90 | ], |
| 91 | |
| 92 | struct_map_field: { |
| 93 | A: {value: 'f'}, |
| 94 | B: {value: 'g'} |
| 95 | }, |
| 96 | |
| 97 | struct_nested_containers_field: [ |
| 98 | [ |
| 99 | { |
| 100 | C: [ |
| 101 | {value: 'h'}, |
| 102 | {value: 'i'} |
| 103 | ] |
| 104 | } |
| 105 | ] |
| 106 | ], |
| 107 | |
| 108 | struct_nested_containers_field2: { |
| 109 | D: [ |
| 110 | { |
| 111 | DA: {value: 'j'} |
| 112 | }, |
| 113 | { |
| 114 | DB: {value: 'k'} |
| 115 | } |
| 116 | ] |
Philip Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 117 | }, |
| 118 | |
| 119 | list_of_list_field: [ |
| 120 | ['one', 'two'], |
| 121 | ['three', 'four'], |
| 122 | ['five', 'six'] |
| 123 | ] |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 124 | }; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | function assertValues(obj, assert) { |
| 129 | assert.equal(obj.struct_field.value, 'a'); |
| 130 | assert.equal(obj.struct_list_field[0].value, 'b'); |
| 131 | assert.equal(obj.struct_list_field[1].value, 'c'); |
| 132 | assert.equal(obj.struct_set_field[0].value, 'd'); |
| 133 | assert.equal(obj.struct_set_field[1].value, 'e'); |
| 134 | assert.equal(obj.struct_map_field.A.value, 'f'); |
| 135 | assert.equal(obj.struct_map_field.B.value, 'g'); |
| 136 | assert.equal(obj.struct_nested_containers_field[0][0].C[0].value, 'h'); |
| 137 | assert.equal(obj.struct_nested_containers_field[0][0].C[1].value, 'i'); |
| 138 | assert.equal(obj.struct_nested_containers_field2.D[0].DA.value, 'j'); |
| 139 | assert.equal(obj.struct_nested_containers_field2.D[1].DB.value, 'k'); |
Philip Frank | 55ddf19 | 2018-01-02 09:00:36 +0100 | [diff] [blame] | 140 | assert.equal(obj.list_of_list_field[0][0], 'one'); |
| 141 | assert.equal(obj.list_of_list_field[0][1], 'two'); |
| 142 | assert.equal(obj.list_of_list_field[1][0], 'three'); |
| 143 | assert.equal(obj.list_of_list_field[1][1], 'four'); |
| 144 | assert.equal(obj.list_of_list_field[2][0], 'five'); |
| 145 | assert.equal(obj.list_of_list_field[2][1], 'six'); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 146 | } |
| 147 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 148 | const cases = { |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 149 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 150 | 'Serialize/deserialize simple struct should return equal object': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 151 | const tObj = new Simple({value: 'a'}); |
| 152 | const received = deserialize(serialize(tObj), Simple); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 153 | assert.ok(tObj !== received); |
| 154 | assert.deepEqual(received, tObj); |
| 155 | }, |
| 156 | |
| 157 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 158 | 'Serialize/deserialize should return equal object': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 159 | const tObj = createThriftObj(); |
| 160 | const received = deserialize(serialize(tObj), Complex); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 161 | assert.ok(tObj !== received); |
| 162 | assert.deepEqual(received, tObj); |
| 163 | }, |
| 164 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 165 | 'Nested structs and containers initialized from plain js objects should serialize same as if initialized from thrift objects': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 166 | const tObj1 = createThriftObj(); |
| 167 | const tObj2 = new Complex(createJsObj()); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 168 | assertValues(tObj2, assert); |
| 169 | assert.equal(serialize(tObj2), serialize(tObj1)); |
| 170 | }, |
| 171 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 172 | 'Modifications to args object should not affect constructed Thrift object': function(assert) { |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 173 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 174 | const args = createJsObj(); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 175 | assertValues(args, assert); |
| 176 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 177 | const tObj = new Complex(args); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 178 | assertValues(tObj, assert); |
| 179 | |
| 180 | args.struct_field.value = 'ZZZ'; |
| 181 | args.struct_list_field[0].value = 'ZZZ'; |
| 182 | args.struct_list_field[1].value = 'ZZZ'; |
| 183 | args.struct_set_field[0].value = 'ZZZ'; |
| 184 | args.struct_set_field[1].value = 'ZZZ'; |
| 185 | args.struct_map_field.A.value = 'ZZZ'; |
| 186 | args.struct_map_field.B.value = 'ZZZ'; |
| 187 | args.struct_nested_containers_field[0][0].C[0] = 'ZZZ'; |
| 188 | args.struct_nested_containers_field[0][0].C[1] = 'ZZZ'; |
| 189 | args.struct_nested_containers_field2.D[0].DA = 'ZZZ'; |
| 190 | args.struct_nested_containers_field2.D[0].DB = 'ZZZ'; |
| 191 | |
| 192 | assertValues(tObj, assert); |
| 193 | }, |
| 194 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 195 | 'nulls are ok': function(assert) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 196 | const tObj = new Complex({ |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 197 | struct_field: null, |
| 198 | struct_list_field: null, |
| 199 | struct_set_field: null, |
| 200 | struct_map_field: null, |
| 201 | struct_nested_containers_field: null, |
| 202 | struct_nested_containers_field2: null |
| 203 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 204 | const received = deserialize(serialize(tObj), Complex); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 205 | assert.ok(tObj !== received); |
| 206 | assert.deepEqual(tObj, received); |
| 207 | } |
| 208 | |
| 209 | }; |
| 210 | |
| 211 | Object.keys(cases).forEach(function(caseName) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 212 | QUnit.test(caseName, cases[caseName]); |
Henrique Mendonça | 15d9042 | 2015-06-25 22:31:41 +1000 | [diff] [blame] | 213 | }); |