forked from dart-archive/dart-protoc-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension_test.dart
173 lines (145 loc) · 6.33 KB
/
extension_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env dart
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library extension_test;
import 'package:test/test.dart';
import '../out/protos/google/protobuf/unittest.pb.dart';
import '../out/protos/enum_extension.pb.dart';
import '../out/protos/nested_extension.pb.dart';
import '../out/protos/non_nested_extension.pb.dart';
import '../out/protos/ExtensionNameConflict.pb.dart';
import '../out/protos/ExtensionEnumNameConflict.pb.dart';
import 'test_util.dart';
throwsArgError(String expectedMessage) => throwsA(predicate((x) {
expect(x, isArgumentError);
expect(x.message, expectedMessage);
return true;
}));
void main() {
test('can set all extension types', () {
TestAllExtensions message = new TestAllExtensions();
setAllExtensions(message);
assertAllExtensionsSet(message);
});
test('can modify all repeated extension types', () {
TestAllExtensions message = new TestAllExtensions();
setAllExtensions(message);
modifyRepeatedExtensions(message);
assertRepeatedExtensionsModified(message);
});
test('unset extensions return default values', () {
assertExtensionsClear(new TestAllExtensions());
});
// void testExtensionReflectionGetters() {} // UNSUPPORTED -- reflection
// void testExtensionReflectionSetters() {} // UNSUPPORTED -- reflection
// void testExtensionReflectionSettersRejectNull() {} // UNSUPPORTED
// void testExtensionReflectionRepeatedSetters() {} // UNSUPPORTED
// void testExtensionReflectionRepeatedSettersRejectNull() // UNSUPPORTED
// void testExtensionReflectionDefaults() // UNSUPPORTED
test('can clear an optional extension', () {
// clearExtension() is not actually used in test_util, so try it manually.
var message = new TestAllExtensions();
message.setExtension(Unittest.optionalInt32Extension, 1);
message.clearExtension(Unittest.optionalInt32Extension);
expect(message.hasExtension(Unittest.optionalInt32Extension), isFalse);
});
test('can clear a repeated extension', () {
var message = new TestAllExtensions();
message.addExtension(Unittest.repeatedInt32Extension, 1);
message.clearExtension(Unittest.repeatedInt32Extension);
expect(message.getExtension(Unittest.repeatedInt32Extension).length, 0);
});
test('can clone an extension field', () {
TestAllExtensions original = new TestAllExtensions();
original.setExtension(Unittest.optionalInt32Extension, 1);
TestAllExtensions clone = original.clone();
expect(clone.hasExtension(Unittest.optionalInt32Extension), isTrue);
expect(clone.getExtension(Unittest.optionalInt32Extension), 1);
});
test('can clone all types of extension fields', () {
assertAllExtensionsSet(getAllExtensionsSet().clone());
});
test("throws if field number isn't allowed for extension", () {
var message = new TestAllTypes(); // does not allow extensions
expect(() {
message.setExtension(Unittest.optionalInt32Extension, 0);
},
throwsArgError(
"Extension optionalInt32Extension not legal for message TestAllTypes"));
expect(() {
message.getExtension(Unittest.optionalInt32Extension);
},
throwsArgError(
"Extension optionalInt32Extension not legal for message TestAllTypes"));
});
test("throws if an int32 extension is set to a bad value", () {
var message = new TestAllExtensions();
expect(() {
message.setExtension(Unittest.optionalInt32Extension, "hello");
},
throwsArgError(
"Illegal to set field optionalInt32Extension (1) of TestAllExtensions"
" to value (hello): not type int"));
});
test('throws if an int64 extension is set to a bad value', () {
var message = new TestAllExtensions();
expect(() {
message.setExtension(Unittest.optionalInt64Extension, 123);
},
throwsArgError(
"Illegal to set field optionalInt64Extension (2) of TestAllExtensions"
" to value (123): not Int64"));
});
test('throws if a message extension is set to a bad value', () {
var message = new TestAllExtensions();
// For a non-repeated message, we only check for a GeneratedMessage.
expect(() {
message.setExtension(Unittest.optionalNestedMessageExtension, 123);
},
throwsArgError(
"Illegal to set field optionalNestedMessageExtension (18)"
" of TestAllExtensions to value (123): not a GeneratedMessage"));
// For a repeated message, the type check is exact.
expect(() {
message.addExtension(
Unittest.repeatedNestedMessageExtension, new TestAllTypes());
}, throwsATypeError);
});
test('throws if an enum extension is set to a bad value', () {
var message = new TestAllExtensions();
// For a non-repeated enum, we only check for a ProtobufEnum.
expect(() {
message.setExtension(Unittest.optionalNestedEnumExtension, 123);
},
throwsArgError("Illegal to set field optionalNestedEnumExtension (21)"
" of TestAllExtensions to value (123): not type ProtobufEnum"));
// For a repeated enum, the type check is exact.
expect(() {
message.addExtension(
Unittest.repeatedForeignEnumExtension, TestAllTypes_NestedEnum.FOO);
}, throwsATypeError);
});
test('can extend a message with a message field with a different type', () {
expect(Non_nested_extension.nonNestedExtension.makeDefault(),
new TypeMatcher<MyNonNestedExtension>());
expect(Non_nested_extension.nonNestedExtension.name, 'nonNestedExtension');
});
test('can extend a message with a message field of the same type', () {
expect(
MyNestedExtension.recursiveExtension.makeDefault()
is MessageToBeExtended,
isTrue);
expect(MyNestedExtension.recursiveExtension.name, 'recursiveExtension');
});
test('can extend message with enum', () {
var msg = new Extendable();
msg.setExtension(Enum_extension.animal, Animal.CAT);
});
test('extension class was renamed to avoid conflict with message', () {
expect(ExtensionNameConflictExt.someExtension.tagNumber, 1);
});
test('extension class was renamed to avoid conflict with enum', () {
expect(ExtensionEnumNameConflictExt.enumConflictExtension.tagNumber, 1);
});
}