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