blob: 687a90c206028e7938a98968b84991fcaa99dbe4 [file] [log] [blame]
Anton Muhin2f64c7a2013-05-15 15:27:26 +04001#!/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
6library validate_fail_test;
7
Brian Slesinskycbd66dc2015-07-10 19:35:08 -07008import 'package:test/test.dart';
Anton Muhin2f64c7a2013-05-15 15:27:26 +04009
Anton Muhin2f64c7a2013-05-15 15:27:26 +040010import '../out/protos/google/protobuf/unittest.pb.dart';
11
Anton Muhin21247272013-07-11 13:54:17 +040012// [ArgumentError] in production mode, [TypeError] in checked.
Brian Slesinsky23a67b42016-04-04 10:09:12 -070013final invalidArgumentException =
14 predicate((e) => e is ArgumentError || e is TypeError);
Anton Muhin21247272013-07-11 13:54:17 +040015final badArgument = throwsA(invalidArgumentException);
16
Brian Slesinsky598f4482015-08-07 11:30:37 -070017// Suppress an analyzer warning for a deliberate type mismatch.
18cast(x) => x;
19
Anton Muhin2f64c7a2013-05-15 15:27:26 +040020void main() {
Anton Muhin2f64c7a2013-05-15 15:27:26 +040021 test('testValidationFailureMessages', () {
Diego Ballesteros Villamizar4ce027a2019-03-29 12:48:39 +010022 TestAllTypes builder = TestAllTypes();
Anton Muhin2f64c7a2013-05-15 15:27:26 +040023
Brian Slesinsky23a67b42016-04-04 10:09:12 -070024 expect(() {
25 builder.optionalInt32 = null;
26 }, throwsArgumentError);
Anton Muhin2f64c7a2013-05-15 15:27:26 +040027
Brian Slesinsky23a67b42016-04-04 10:09:12 -070028 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 Muhin2f64c7a2013-05-15 15:27:26 +040037
Brian Slesinsky23a67b42016-04-04 10:09:12 -070038 expect(() {
39 builder.optionalInt64 = cast('102');
40 }, badArgument);
41 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -080042 builder.optionalInt64 = cast(-9223372036854775808);
Brian Slesinsky23a67b42016-04-04 10:09:12 -070043 }, badArgument);
44 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -080045 builder.optionalInt64 = cast(9223372036854775807);
Brian Slesinsky23a67b42016-04-04 10:09:12 -070046 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +040047
Brian Slesinsky23a67b42016-04-04 10:09:12 -070048 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 Muhin2f64c7a2013-05-15 15:27:26 +040057
Brian Slesinsky23a67b42016-04-04 10:09:12 -070058 expect(() {
59 builder.optionalUint64 = cast('104');
60 }, badArgument);
61 expect(() {
62 builder.optionalUint64 = cast(-1);
63 }, badArgument);
64 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -080065 builder.optionalUint64 = cast(8446744073709551616);
Brian Slesinsky23a67b42016-04-04 10:09:12 -070066 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +040067
Brian Slesinsky23a67b42016-04-04 10:09:12 -070068 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 Muhin2f64c7a2013-05-15 15:27:26 +040077
Brian Slesinsky23a67b42016-04-04 10:09:12 -070078 expect(() {
79 builder.optionalSint64 = cast('106');
80 }, badArgument);
81 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -080082 builder.optionalSint64 = cast(-9223372036854775808);
Brian Slesinsky23a67b42016-04-04 10:09:12 -070083 }, badArgument);
84 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -080085 builder.optionalSint64 = cast(9223372036854775807);
Brian Slesinsky23a67b42016-04-04 10:09:12 -070086 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +040087
Brian Slesinsky23a67b42016-04-04 10:09:12 -070088 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 Muhin2f64c7a2013-05-15 15:27:26 +040097
Brian Slesinsky23a67b42016-04-04 10:09:12 -070098 expect(() {
99 builder.optionalFixed64 = cast('108');
100 }, badArgument);
101 expect(() {
102 builder.optionalFixed64 = cast(-1);
103 }, badArgument);
104 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -0800105 builder.optionalFixed64 = cast(8446744073709551616);
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700106 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400107
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700108 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 Muhin2f64c7a2013-05-15 15:27:26 +0400117
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700118 expect(() {
119 builder.optionalSfixed64 = cast('110');
120 }, badArgument);
121 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -0800122 builder.optionalSfixed64 = cast(-9223372036854775808);
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700123 }, badArgument);
124 expect(() {
Keerti Parthasarathydd763902018-01-11 01:40:28 -0800125 builder.optionalSfixed64 = cast(9223372036854775807);
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700126 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400127
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700128 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 Muhin2f64c7a2013-05-15 15:27:26 +0400137
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700138 expect(() {
139 builder.optionalDouble = cast('112');
140 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400141
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700142 expect(() {
143 builder.optionalBool = cast('113');
144 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400145
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700146 expect(() {
147 builder.optionalString = cast(false);
148 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400149
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700150 // Can't test this easily in strong mode.
151 // expect(() {
152 // builder.optionalBytes = cast('115');
153 // }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400154
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700155 expect(() {
156 builder.optionalNestedMessage = cast('118');
157 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400158
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700159 expect(() {
160 builder.optionalNestedEnum = cast('121');
161 }, badArgument);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400162
163 // Set repeating value (no setter should exist).
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700164 expect(() {
165 cast(builder).repeatedInt32 = 201;
166 }, throwsNoSuchMethodError);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400167
168 // Unknown tag.
Brian Slesinsky23a67b42016-04-04 10:09:12 -0700169 expect(() {
170 builder.setField(999, 'field');
171 }, throwsArgumentError);
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400172
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400173 expect(() {
Diego Ballesteros Villamizar4ce027a2019-03-29 12:48:39 +0100174 TestAllExtensions().setExtension(Unittest.optionalInt32Extension, '101');
Anton Muhin2f64c7a2013-05-15 15:27:26 +0400175 }, throwsArgumentError);
176 });
177}