• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  // TextFormat
129  enumName = [descriptor textFormatNameForValue:1];
130  XCTAssertNotNil(enumName);
131  XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:@"FOO"]);
132  XCTAssertEqual(value, TestAllTypes_NestedEnum_Foo);
133
134  // Bad values
135  enumName = [descriptor enumNameForValue:0];
136  XCTAssertNil(enumName);
137  XCTAssertFalse([descriptor getValue:&value forEnumName:@"Unknown"]);
138  XCTAssertFalse([descriptor getValue:NULL forEnumName:@"Unknown"]);
139  XCTAssertFalse([descriptor getValue:&value
140                          forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
141  XCTAssertFalse([descriptor getValue:NULL
142                          forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
143  XCTAssertFalse([descriptor getValue:NULL forEnumTextFormatName:@"Unknown"]);
144  XCTAssertFalse([descriptor getValue:&value forEnumTextFormatName:@"Unknown"]);
145}
146
147- (void)testEnumValueValidator {
148  GPBDescriptor *descriptor = [TestAllTypes descriptor];
149  GPBFieldDescriptor *fieldDescriptor =
150      [descriptor fieldWithName:@"optionalNestedEnum"];
151
152  // Valid values
153  XCTAssertTrue([fieldDescriptor isValidEnumValue:1]);
154  XCTAssertTrue([fieldDescriptor isValidEnumValue:2]);
155  XCTAssertTrue([fieldDescriptor isValidEnumValue:3]);
156  XCTAssertTrue([fieldDescriptor isValidEnumValue:-1]);
157
158  // Invalid values
159  XCTAssertFalse([fieldDescriptor isValidEnumValue:4]);
160  XCTAssertFalse([fieldDescriptor isValidEnumValue:0]);
161  XCTAssertFalse([fieldDescriptor isValidEnumValue:-2]);
162}
163
164- (void)testOneofDescriptor {
165  GPBDescriptor *descriptor = [TestOneof2 descriptor];
166
167  // All fields should be listed.
168  XCTAssertEqual(descriptor.fields.count, 17U);
169
170  // There are two oneofs in there.
171  XCTAssertEqual(descriptor.oneofs.count, 2U);
172
173  GPBFieldDescriptor *fooStringField =
174      [descriptor fieldWithNumber:TestOneof2_FieldNumber_FooString];
175  XCTAssertNotNil(fooStringField);
176  GPBFieldDescriptor *barStringField =
177      [descriptor fieldWithNumber:TestOneof2_FieldNumber_BarString];
178  XCTAssertNotNil(barStringField);
179
180  // Check the oneofs to have what is expected.
181
182  GPBOneofDescriptor *oneofFoo = [descriptor oneofWithName:@"foo"];
183  XCTAssertNotNil(oneofFoo);
184  XCTAssertEqual(oneofFoo.fields.count, 9U);
185
186  // Pointer comparisons.
187  XCTAssertEqual([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_FooString],
188                 fooStringField);
189  XCTAssertEqual([oneofFoo fieldWithName:@"fooString"], fooStringField);
190
191  GPBOneofDescriptor *oneofBar = [descriptor oneofWithName:@"bar"];
192  XCTAssertNotNil(oneofBar);
193  XCTAssertEqual(oneofBar.fields.count, 6U);
194
195  // Pointer comparisons.
196  XCTAssertEqual([oneofBar fieldWithNumber:TestOneof2_FieldNumber_BarString],
197                 barStringField);
198  XCTAssertEqual([oneofBar fieldWithName:@"barString"], barStringField);
199
200  // Unknown oneof not found.
201
202  XCTAssertNil([descriptor oneofWithName:@"mumble"]);
203  XCTAssertNil([descriptor oneofWithName:@"Foo"]);
204
205  // Unknown oneof item.
206
207  XCTAssertNil([oneofFoo fieldWithName:@"mumble"]);
208  XCTAssertNil([oneofFoo fieldWithNumber:666]);
209
210  // Field exists, but not in this oneof.
211
212  XCTAssertNil([oneofFoo fieldWithName:@"barString"]);
213  XCTAssertNil([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_BarString]);
214  XCTAssertNil([oneofBar fieldWithName:@"fooString"]);
215  XCTAssertNil([oneofBar fieldWithNumber:TestOneof2_FieldNumber_FooString]);
216
217  // Check pointers back to the enclosing oneofs.
218  // (pointer comparisions)
219  XCTAssertEqual(fooStringField.containingOneof, oneofFoo);
220  XCTAssertEqual(barStringField.containingOneof, oneofBar);
221  GPBFieldDescriptor *bazString =
222      [descriptor fieldWithNumber:TestOneof2_FieldNumber_BazString];
223  XCTAssertNotNil(bazString);
224  XCTAssertNil(bazString.containingOneof);
225}
226
227@end
228