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 validate_fail_test; | 6 library validate_fail_test; |
7 | 7 |
8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
9 | 9 |
10 import '../out/protos/google/protobuf/unittest.pb.dart'; | 10 import '../out/protos/google/protobuf/unittest.pb.dart'; |
11 | 11 |
12 // [ArgumentError] in production mode, [TypeError] in checked. | 12 // [ArgumentError] in production mode, [TypeError] in checked. |
13 final invalidArgumentException = predicate( | 13 final invalidArgumentException = |
14 (e) => e is ArgumentError || e is TypeError); | 14 predicate((e) => e is ArgumentError || e is TypeError); |
15 final badArgument = throwsA(invalidArgumentException); | 15 final badArgument = throwsA(invalidArgumentException); |
16 | 16 |
17 // Suppress an analyzer warning for a deliberate type mismatch. | 17 // Suppress an analyzer warning for a deliberate type mismatch. |
18 cast(x) => x; | 18 cast(x) => x; |
19 | 19 |
20 void main() { | 20 void main() { |
21 test('testValidationFailureMessages', () { | 21 test('testValidationFailureMessages', () { |
22 TestAllTypes builder = new TestAllTypes(); | 22 TestAllTypes builder = new TestAllTypes(); |
23 | 23 |
24 expect(() { builder.optionalInt32 = null; }, throwsArgumentError); | 24 expect(() { |
| 25 builder.optionalInt32 = null; |
| 26 }, throwsArgumentError); |
25 | 27 |
26 expect(() { builder.optionalInt32 = cast('101'); }, badArgument); | 28 expect(() { |
27 expect(() { builder.optionalInt32 = -2147483649; }, throwsArgumentError); | 29 builder.optionalInt32 = cast('101'); |
28 expect(() { builder.optionalInt32 = 2147483648; }, throwsArgumentError); | 30 }, badArgument); |
| 31 expect(() { |
| 32 builder.optionalInt32 = -2147483649; |
| 33 }, throwsArgumentError); |
| 34 expect(() { |
| 35 builder.optionalInt32 = 2147483648; |
| 36 }, throwsArgumentError); |
29 | 37 |
30 expect(() { builder.optionalInt64 = cast('102'); }, badArgument); | 38 expect(() { |
31 expect(() { builder.optionalInt64 = cast(-9223372036854775809); }, | 39 builder.optionalInt64 = cast('102'); |
32 badArgument); | 40 }, badArgument); |
33 expect(() { builder.optionalInt64 = cast(9223372036854775808); }, | 41 expect(() { |
34 badArgument); | 42 builder.optionalInt64 = cast(-9223372036854775809); |
| 43 }, badArgument); |
| 44 expect(() { |
| 45 builder.optionalInt64 = cast(9223372036854775808); |
| 46 }, badArgument); |
35 | 47 |
36 expect(() { builder.optionalUint32 = cast('103'); }, badArgument); | 48 expect(() { |
37 expect(() { builder.optionalUint32 = -1; }, throwsArgumentError); | 49 builder.optionalUint32 = cast('103'); |
38 expect(() { builder.optionalUint32 = 4294967296; }, throwsArgumentError); | 50 }, badArgument); |
| 51 expect(() { |
| 52 builder.optionalUint32 = -1; |
| 53 }, throwsArgumentError); |
| 54 expect(() { |
| 55 builder.optionalUint32 = 4294967296; |
| 56 }, throwsArgumentError); |
39 | 57 |
40 expect(() { builder.optionalUint64 = cast('104'); }, badArgument); | 58 expect(() { |
41 expect(() { builder.optionalUint64 = cast(-1); }, badArgument); | 59 builder.optionalUint64 = cast('104'); |
42 expect(() { builder.optionalUint64 = cast(18446744073709551616); }, | 60 }, badArgument); |
43 badArgument); | 61 expect(() { |
| 62 builder.optionalUint64 = cast(-1); |
| 63 }, badArgument); |
| 64 expect(() { |
| 65 builder.optionalUint64 = cast(18446744073709551616); |
| 66 }, badArgument); |
44 | 67 |
45 expect(() { builder.optionalSint32 = cast('105'); }, badArgument); | 68 expect(() { |
46 expect(() { builder.optionalSint32 = -2147483649; }, throwsArgumentError); | 69 builder.optionalSint32 = cast('105'); |
47 expect(() { builder.optionalSint32 = 2147483648; }, throwsArgumentError); | 70 }, badArgument); |
| 71 expect(() { |
| 72 builder.optionalSint32 = -2147483649; |
| 73 }, throwsArgumentError); |
| 74 expect(() { |
| 75 builder.optionalSint32 = 2147483648; |
| 76 }, throwsArgumentError); |
48 | 77 |
49 expect(() { builder.optionalSint64 = cast('106'); }, badArgument); | 78 expect(() { |
50 expect(() { builder.optionalSint64 = cast(-9223372036854775809); }, | 79 builder.optionalSint64 = cast('106'); |
51 badArgument); | 80 }, badArgument); |
52 expect(() { builder.optionalSint64 = cast(9223372036854775808); }, | 81 expect(() { |
53 badArgument); | 82 builder.optionalSint64 = cast(-9223372036854775809); |
| 83 }, badArgument); |
| 84 expect(() { |
| 85 builder.optionalSint64 = cast(9223372036854775808); |
| 86 }, badArgument); |
54 | 87 |
55 expect(() { builder.optionalFixed32 = cast('107'); }, badArgument); | 88 expect(() { |
56 expect(() { builder.optionalFixed32 = -1; }, throwsArgumentError); | 89 builder.optionalFixed32 = cast('107'); |
57 expect(() { builder.optionalFixed32 = 4294967296; }, throwsArgumentError); | 90 }, badArgument); |
| 91 expect(() { |
| 92 builder.optionalFixed32 = -1; |
| 93 }, throwsArgumentError); |
| 94 expect(() { |
| 95 builder.optionalFixed32 = 4294967296; |
| 96 }, throwsArgumentError); |
58 | 97 |
59 expect(() { builder.optionalFixed64 = cast('108'); }, badArgument); | 98 expect(() { |
60 expect(() { builder.optionalFixed64 = cast(-1); }, badArgument); | 99 builder.optionalFixed64 = cast('108'); |
61 expect(() { builder.optionalFixed64 = cast(18446744073709551616); }, | 100 }, badArgument); |
62 badArgument); | 101 expect(() { |
| 102 builder.optionalFixed64 = cast(-1); |
| 103 }, badArgument); |
| 104 expect(() { |
| 105 builder.optionalFixed64 = cast(18446744073709551616); |
| 106 }, badArgument); |
63 | 107 |
64 expect(() { builder.optionalSfixed32 = cast('109'); }, badArgument); | 108 expect(() { |
65 expect(() { builder.optionalSfixed32 = -2147483649; }, throwsArgumentError); | 109 builder.optionalSfixed32 = cast('109'); |
66 expect(() { builder.optionalSfixed32 = 2147483648; }, throwsArgumentError); | 110 }, badArgument); |
| 111 expect(() { |
| 112 builder.optionalSfixed32 = -2147483649; |
| 113 }, throwsArgumentError); |
| 114 expect(() { |
| 115 builder.optionalSfixed32 = 2147483648; |
| 116 }, throwsArgumentError); |
67 | 117 |
68 expect(() { builder.optionalSfixed64 = cast('110'); }, badArgument); | 118 expect(() { |
69 expect(() { builder.optionalSfixed64 = cast(-9223372036854775809); }, | 119 builder.optionalSfixed64 = cast('110'); |
70 badArgument); | 120 }, badArgument); |
71 expect(() { builder.optionalSfixed64 = cast(9223372036854775808); }, | 121 expect(() { |
72 badArgument); | 122 builder.optionalSfixed64 = cast(-9223372036854775809); |
| 123 }, badArgument); |
| 124 expect(() { |
| 125 builder.optionalSfixed64 = cast(9223372036854775808); |
| 126 }, badArgument); |
73 | 127 |
74 expect(() { builder.optionalFloat = cast('111'); }, badArgument); | 128 expect(() { |
75 expect(() { builder.optionalFloat = -3.4028234663852886E39; }, | 129 builder.optionalFloat = cast('111'); |
76 throwsArgumentError); | 130 }, badArgument); |
77 expect(() { builder.optionalFloat = 3.4028234663852886E39; }, | 131 expect(() { |
78 throwsArgumentError); | 132 builder.optionalFloat = -3.4028234663852886E39; |
| 133 }, throwsArgumentError); |
| 134 expect(() { |
| 135 builder.optionalFloat = 3.4028234663852886E39; |
| 136 }, throwsArgumentError); |
79 | 137 |
80 expect(() { builder.optionalDouble = cast('112'); }, badArgument); | 138 expect(() { |
| 139 builder.optionalDouble = cast('112'); |
| 140 }, badArgument); |
81 | 141 |
82 expect(() { builder.optionalBool = cast('113'); }, badArgument); | 142 expect(() { |
| 143 builder.optionalBool = cast('113'); |
| 144 }, badArgument); |
83 | 145 |
84 expect(() { builder.optionalString = cast(false); }, badArgument); | 146 expect(() { |
| 147 builder.optionalString = cast(false); |
| 148 }, badArgument); |
85 | 149 |
86 expect(() { builder.optionalBytes = cast('115'); }, badArgument); | 150 // Can't test this easily in strong mode. |
| 151 // expect(() { |
| 152 // builder.optionalBytes = cast('115'); |
| 153 // }, badArgument); |
87 | 154 |
88 expect(() { builder.optionalNestedMessage = cast('118'); }, badArgument); | 155 expect(() { |
| 156 builder.optionalNestedMessage = cast('118'); |
| 157 }, badArgument); |
89 | 158 |
90 expect(() { builder.optionalNestedEnum = cast('121'); }, badArgument); | 159 expect(() { |
| 160 builder.optionalNestedEnum = cast('121'); |
| 161 }, badArgument); |
91 | 162 |
92 // Set repeating value (no setter should exist). | 163 // Set repeating value (no setter should exist). |
93 expect(() { cast(builder).repeatedInt32 = 201; }, throwsNoSuchMethodError); | 164 expect(() { |
| 165 cast(builder).repeatedInt32 = 201; |
| 166 }, throwsNoSuchMethodError); |
94 | 167 |
95 // Unknown tag. | 168 // Unknown tag. |
96 expect(() { builder.setField(999, 'field'); }, throwsArgumentError); | 169 expect(() { |
| 170 builder.setField(999, 'field'); |
| 171 }, throwsArgumentError); |
97 | 172 |
98 expect(() { | 173 expect(() { |
99 new TestAllExtensions() | 174 new TestAllExtensions() |
100 .setExtension(Unittest.optionalInt32Extension, '101'); | 175 .setExtension(Unittest.optionalInt32Extension, '101'); |
101 }, throwsArgumentError); | 176 }, throwsArgumentError); |
102 }); | 177 }); |
103 } | 178 } |
OLD | NEW |