Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/protobuf/objectivec/Tests/GPBDescriptorTests.m

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #import "GPBTestUtilities.h"
32
33 #import <objc/runtime.h>
34
35 #import "GPBDescriptor.h"
36 #import "google/protobuf/Unittest.pbobjc.h"
37
38 @interface DescriptorTests : GPBTestCase
39 @end
40
41 @implementation DescriptorTests
42
43 - (void)testFieldDescriptor {
44 GPBDescriptor *descriptor = [TestAllTypes descriptor];
45
46 // Nested Enum
47 GPBFieldDescriptor *fieldDescriptorWithName =
48 [descriptor fieldWithName:@"optionalNestedEnum"];
49 XCTAssertNotNil(fieldDescriptorWithName);
50 GPBFieldDescriptor *fieldDescriptorWithNumber =
51 [descriptor fieldWithNumber:21];
52 XCTAssertNotNil(fieldDescriptorWithNumber);
53 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
54 XCTAssertNotNil(fieldDescriptorWithNumber.enumDescriptor);
55 XCTAssertEqualObjects(fieldDescriptorWithNumber.enumDescriptor.name,
56 @"TestAllTypes_NestedEnum");
57
58 // Foreign Enum
59 fieldDescriptorWithName = [descriptor fieldWithName:@"optionalForeignEnum"];
60 XCTAssertNotNil(fieldDescriptorWithName);
61 fieldDescriptorWithNumber = [descriptor fieldWithNumber:22];
62 XCTAssertNotNil(fieldDescriptorWithNumber);
63 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
64 XCTAssertNotNil(fieldDescriptorWithNumber.enumDescriptor);
65 XCTAssertEqualObjects(fieldDescriptorWithNumber.enumDescriptor.name,
66 @"ForeignEnum");
67
68 // Import Enum
69 fieldDescriptorWithName = [descriptor fieldWithName:@"optionalImportEnum"];
70 XCTAssertNotNil(fieldDescriptorWithName);
71 fieldDescriptorWithNumber = [descriptor fieldWithNumber:23];
72 XCTAssertNotNil(fieldDescriptorWithNumber);
73 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
74 XCTAssertNotNil(fieldDescriptorWithNumber.enumDescriptor);
75 XCTAssertEqualObjects(fieldDescriptorWithNumber.enumDescriptor.name,
76 @"ImportEnum");
77
78 // Nested Message
79 fieldDescriptorWithName = [descriptor fieldWithName:@"optionalNestedMessage"];
80 XCTAssertNotNil(fieldDescriptorWithName);
81 fieldDescriptorWithNumber = [descriptor fieldWithNumber:18];
82 XCTAssertNotNil(fieldDescriptorWithNumber);
83 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
84 XCTAssertNil(fieldDescriptorWithNumber.enumDescriptor);
85
86 // Foreign Message
87 fieldDescriptorWithName =
88 [descriptor fieldWithName:@"optionalForeignMessage"];
89 XCTAssertNotNil(fieldDescriptorWithName);
90 fieldDescriptorWithNumber = [descriptor fieldWithNumber:19];
91 XCTAssertNotNil(fieldDescriptorWithNumber);
92 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
93 XCTAssertNil(fieldDescriptorWithNumber.enumDescriptor);
94
95 // Import Message
96 fieldDescriptorWithName = [descriptor fieldWithName:@"optionalImportMessage"];
97 XCTAssertNotNil(fieldDescriptorWithName);
98 fieldDescriptorWithNumber = [descriptor fieldWithNumber:20];
99 XCTAssertNotNil(fieldDescriptorWithNumber);
100 XCTAssertEqual(fieldDescriptorWithName, fieldDescriptorWithNumber);
101 XCTAssertNil(fieldDescriptorWithNumber.enumDescriptor);
102 }
103
104 - (void)testEnumDescriptor {
105 GPBEnumDescriptor *descriptor = TestAllTypes_NestedEnum_EnumDescriptor();
106
107 NSString *enumName = [descriptor enumNameForValue:1];
108 XCTAssertNotNil(enumName);
109 int32_t value;
110 XCTAssertTrue(
111 [descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Foo"]);
112 XCTAssertTrue(
113 [descriptor getValue:NULL forEnumName:@"TestAllTypes_NestedEnum_Foo"]);
114 XCTAssertEqual(value, TestAllTypes_NestedEnum_Foo);
115
116 enumName = [descriptor enumNameForValue:2];
117 XCTAssertNotNil(enumName);
118 XCTAssertTrue(
119 [descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Bar"]);
120 XCTAssertEqual(value, TestAllTypes_NestedEnum_Bar);
121
122 enumName = [descriptor enumNameForValue:3];
123 XCTAssertNotNil(enumName);
124 XCTAssertTrue(
125 [descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Baz"]);
126 XCTAssertEqual(value, TestAllTypes_NestedEnum_Baz);
127
128 // Bad values
129 enumName = [descriptor enumNameForValue:0];
130 XCTAssertNil(enumName);
131 XCTAssertFalse([descriptor getValue:&value forEnumName:@"Unknown"]);
132 XCTAssertFalse([descriptor getValue:NULL forEnumName:@"Unknown"]);
133 XCTAssertFalse([descriptor getValue:&value
134 forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
135 XCTAssertFalse([descriptor getValue:NULL
136 forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
137 }
138
139 - (void)testEnumValueValidator {
140 GPBDescriptor *descriptor = [TestAllTypes descriptor];
141 GPBFieldDescriptor *fieldDescriptor =
142 [descriptor fieldWithName:@"optionalNestedEnum"];
143
144 // Valid values
145 XCTAssertTrue([fieldDescriptor isValidEnumValue:1]);
146 XCTAssertTrue([fieldDescriptor isValidEnumValue:2]);
147 XCTAssertTrue([fieldDescriptor isValidEnumValue:3]);
148 XCTAssertTrue([fieldDescriptor isValidEnumValue:-1]);
149
150 // Invalid values
151 XCTAssertFalse([fieldDescriptor isValidEnumValue:4]);
152 XCTAssertFalse([fieldDescriptor isValidEnumValue:0]);
153 XCTAssertFalse([fieldDescriptor isValidEnumValue:-2]);
154 }
155
156 - (void)testEnumDescriptorLookup {
157 GPBDescriptor *descriptor = [TestAllTypes descriptor];
158 GPBEnumDescriptor *enumDescriptor =
159 [descriptor enumWithName:@"TestAllTypes_NestedEnum"];
160 XCTAssertNotNil(enumDescriptor);
161
162 // Descriptor cannot find foreign or imported enums.
163 enumDescriptor = [descriptor enumWithName:@"ForeignEnumEnum"];
164 XCTAssertNil(enumDescriptor);
165 enumDescriptor = [descriptor enumWithName:@"ImportEnumEnum"];
166 XCTAssertNil(enumDescriptor);
167 }
168
169 - (void)testOneofDescriptor {
170 GPBDescriptor *descriptor = [TestOneof2 descriptor];
171
172 // All fields should be listed.
173 XCTAssertEqual(descriptor.fields.count, 17U);
174
175 // There are two oneofs in there.
176 XCTAssertEqual(descriptor.oneofs.count, 2U);
177
178 GPBFieldDescriptor *fooStringField =
179 [descriptor fieldWithNumber:TestOneof2_FieldNumber_FooString];
180 XCTAssertNotNil(fooStringField);
181 GPBFieldDescriptor *barStringField =
182 [descriptor fieldWithNumber:TestOneof2_FieldNumber_BarString];
183 XCTAssertNotNil(barStringField);
184
185 // Check the oneofs to have what is expected.
186
187 GPBOneofDescriptor *oneofFoo = [descriptor oneofWithName:@"foo"];
188 XCTAssertNotNil(oneofFoo);
189 XCTAssertEqual(oneofFoo.fields.count, 9U);
190
191 // Pointer comparisons.
192 XCTAssertEqual([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_FooString],
193 fooStringField);
194 XCTAssertEqual([oneofFoo fieldWithName:@"fooString"], fooStringField);
195
196 GPBOneofDescriptor *oneofBar = [descriptor oneofWithName:@"bar"];
197 XCTAssertNotNil(oneofBar);
198 XCTAssertEqual(oneofBar.fields.count, 6U);
199
200 // Pointer comparisons.
201 XCTAssertEqual([oneofBar fieldWithNumber:TestOneof2_FieldNumber_BarString],
202 barStringField);
203 XCTAssertEqual([oneofBar fieldWithName:@"barString"], barStringField);
204
205 // Unknown oneof not found.
206
207 XCTAssertNil([descriptor oneofWithName:@"mumble"]);
208 XCTAssertNil([descriptor oneofWithName:@"Foo"]);
209
210 // Unknown oneof item.
211
212 XCTAssertNil([oneofFoo fieldWithName:@"mumble"]);
213 XCTAssertNil([oneofFoo fieldWithNumber:666]);
214
215 // Field exists, but not in this oneof.
216
217 XCTAssertNil([oneofFoo fieldWithName:@"barString"]);
218 XCTAssertNil([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_BarString]);
219 XCTAssertNil([oneofBar fieldWithName:@"fooString"]);
220 XCTAssertNil([oneofBar fieldWithNumber:TestOneof2_FieldNumber_FooString]);
221
222 // Check pointers back to the enclosing oneofs.
223 // (pointer comparisions)
224 XCTAssertEqual(fooStringField.containingOneof, oneofFoo);
225 XCTAssertEqual(barStringField.containingOneof, oneofBar);
226 GPBFieldDescriptor *bazString =
227 [descriptor fieldWithNumber:TestOneof2_FieldNumber_BazString];
228 XCTAssertNotNil(bazString);
229 XCTAssertNil(bazString.containingOneof);
230 }
231
232 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698