| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 library json_test; | 6 library json_test; |
| 7 | 7 |
| 8 import 'package:protobuf/protobuf.dart'; | 8 import 'package:protobuf/protobuf.dart'; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 | 10 |
| 11 import '../out/protos/google/protobuf/unittest.pb.dart'; | 11 import '../out/protos/google/protobuf/unittest.pb.dart'; |
| 12 | 12 |
| 13 import 'test_util.dart'; | 13 import 'test_util.dart'; |
| 14 | 14 |
| 15 void main() { | 15 void main() { |
| 16 final String TEST_ALL_TYPES_JSON = '{"1":101,"2":"102","3":103,"4":"104",' | 16 final String TEST_ALL_TYPES_JSON = '{"1":101,"2":102,"3":103,"4":104,' |
| 17 '"5":105,"6":"106","7":107,"8":"108","9":109,"10":"110","11":111.0,' | 17 '"5":105,"6":106,"7":107,"8":108,"9":109,"10":110,"11":111.0,' |
| 18 '"12":112.0,"13":true,"14":"115","15":"MTE2","16":{"17":117},' | 18 '"12":112.0,"13":true,"14":"115","15":"MTE2","16":{"17":117},' |
| 19 '"18":{"1":118},"19":{"1":119},"20":{"1":120},"21":3,"22":6,"23":9,' | 19 '"18":{"1":118},"19":{"1":119},"20":{"1":120},"21":3,"22":6,"23":9,' |
| 20 '"24":"124","25":"125","31":[201,301],"32":["202","302"],' | 20 '"24":"124","25":"125","31":[201,301],"32":[202,302],' |
| 21 '"33":[203,303],"34":["204","304"],"35":[205,305],"36":["206","306"],' | 21 '"33":[203,303],"34":[204,304],"35":[205,305],"36":[206,306],' |
| 22 '"37":[207,307],"38":["208","308"],"39":[209,309],"40":["210","310"],' | 22 '"37":[207,307],"38":[208,308],"39":[209,309],"40":[210,310],' |
| 23 '"41":[211.0,311.0],"42":[212.0,312.0],"43":[true,false],' | 23 '"41":[211.0,311.0],"42":[212.0,312.0],"43":[true,false],' |
| 24 '"44":["215","315"],"45":["MjE2","MzE2"],"46":[{"47":217},{"47":317}],' | 24 '"44":["215","315"],"45":["MjE2","MzE2"],"46":[{"47":217},{"47":317}],' |
| 25 '"48":[{"1":218},{"1":318}],"49":[{"1":219},{"1":319}],' | 25 '"48":[{"1":218},{"1":318}],"49":[{"1":219},{"1":319}],' |
| 26 '"50":[{"1":220},{"1":320}],"51":[2,3],"52":[5,6],"53":[8,9],' | 26 '"50":[{"1":220},{"1":320}],"51":[2,3],"52":[5,6],"53":[8,9],' |
| 27 '"54":["224","324"],"55":["225","325"],"61":401,"62":"402","63":403,' | 27 '"54":["224","324"],"55":["225","325"],"61":401,"62":402,"63":403,' |
| 28 '"64":"404","65":405,"66":"406","67":407,"68":"408","69":409,' | 28 '"64":404,"65":405,"66":406,"67":407,"68":408,"69":409,' |
| 29 '"70":"410","71":411.0,"72":412.0,"73":false,"74":"415","75":"NDE2",' | 29 '"70":410,"71":411.0,"72":412.0,"73":false,"74":"415","75":"NDE2",' |
| 30 '"81":1,"82":4,"83":7,"84":"424","85":"425"}'; | 30 '"81":1,"82":4,"83":7,"84":"424","85":"425"}'; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Checks that message once serialized to JSON | 33 * Checks that message once serialized to JSON |
| 34 * matches TEST_ALL_TYPES_JSON massaged with [:.replaceAll(from, to):]. | 34 * matches TEST_ALL_TYPES_JSON massaged with [:.replaceAll(from, to):]. |
| 35 */ | 35 */ |
| 36 expectedJson(from, to) { | 36 expectedJson(from, to) { |
| 37 var expectedJson = TEST_ALL_TYPES_JSON.replaceAll(from, to); | 37 var expectedJson = TEST_ALL_TYPES_JSON.replaceAll(from, to); |
| 38 return predicate( | 38 return predicate( |
| 39 (message) => message.writeToJson() == expectedJson, 'Incorrect output'); | 39 (message) => message.writeToJson() == expectedJson, 'Incorrect output'); |
| 40 } | 40 } |
| 41 | 41 |
| 42 test('testOutput', () { | 42 test('testOutput', () { |
| 43 expect(getAllSet().writeToJson(), TEST_ALL_TYPES_JSON); | 43 expect(getAllSet().writeToJson(), TEST_ALL_TYPES_JSON); |
| 44 | 44 |
| 45 // Test empty list. | 45 // Test empty list. |
| 46 expect(getAllSet()..repeatedBool.clear(), | 46 expect(getAllSet()..repeatedBool.clear(), |
| 47 expectedJson('"43":[true,false],', '')); | 47 expectedJson('"43":[true,false],', '')); |
| 48 | 48 |
| 49 // Test negative number. | 49 // Test negative number. |
| 50 expect(getAllSet()..optionalInt32 = -1234567, | 50 expect(getAllSet()..optionalInt32 = -1234567, |
| 51 expectedJson(':101,', ':-1234567,')); | 51 expectedJson(':101,', ':-1234567,')); |
| 52 | 52 |
| 53 // 64-bit numbers are quoted. | 53 // 64-bit numbers outside 53-bit range are quoted. |
| 54 expect(getAllSet()..optionalInt64 = make64(0, 0x200000), | 54 expect(getAllSet()..optionalInt64 = make64(0, 0x200000), |
| 55 expectedJson(':"102",', ':"9007199254740992",')); | 55 expectedJson(':102,', ':9007199254740992,')); |
| 56 expect(getAllSet()..optionalInt64 = make64(1, 0x200000), |
| 57 expectedJson(':102,', ':"9007199254740993",')); |
| 58 expect(getAllSet()..optionalInt64 = -make64(0, 0x200000), |
| 59 expectedJson(':102,', ':-9007199254740992,')); |
| 60 expect(getAllSet()..optionalInt64 = -make64(1, 0x200000), |
| 61 expectedJson(':102,', ':"-9007199254740993",')); |
| 56 | 62 |
| 57 // Quotes, backslashes, and control characters in strings are quoted. | 63 // Quotes, backslashes, and control characters in strings are quoted. |
| 58 expect(getAllSet()..optionalString = 'a\u0000b\u0001cd\\e\"fg', | 64 expect(getAllSet()..optionalString = 'a\u0000b\u0001cd\\e\"fg', |
| 59 expectedJson(':"115",', ':"a\\u0000b\\u0001cd\\\\e\\"fg",')); | 65 expectedJson(':"115",', ':"a\\u0000b\\u0001cd\\\\e\\"fg",')); |
| 60 }); | 66 }); |
| 61 | 67 |
| 62 test('testBase64Encode', () { | 68 test('testBase64Encode', () { |
| 63 expect(getAllSet()..optionalBytes = 'Hello, world'.codeUnits, | 69 expect(getAllSet()..optionalBytes = 'Hello, world'.codeUnits, |
| 64 expectedJson(':"MTE2",', ':"SGVsbG8sIHdvcmxk",')); | 70 expectedJson(':"MTE2",', ':"SGVsbG8sIHdvcmxk",')); |
| 65 | 71 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 test('testExtensionsOutput', () { | 115 test('testExtensionsOutput', () { |
| 110 expect(getAllExtensionsSet().writeToJson(), TEST_ALL_TYPES_JSON); | 116 expect(getAllExtensionsSet().writeToJson(), TEST_ALL_TYPES_JSON); |
| 111 }); | 117 }); |
| 112 | 118 |
| 113 test('testExtensionsParse', () { | 119 test('testExtensionsParse', () { |
| 114 ExtensionRegistry registry = getExtensionRegistry(); | 120 ExtensionRegistry registry = getExtensionRegistry(); |
| 115 expect(new TestAllExtensions.fromJson(TEST_ALL_TYPES_JSON, registry), | 121 expect(new TestAllExtensions.fromJson(TEST_ALL_TYPES_JSON, registry), |
| 116 getAllExtensionsSet()); | 122 getAllExtensionsSet()); |
| 117 }); | 123 }); |
| 118 } | 124 } |
| OLD | NEW |