• 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  // 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)testOneofDescriptor {
157  GPBDescriptor *descriptor = [TestOneof2 descriptor];
158
159  // All fields should be listed.
160  XCTAssertEqual(descriptor.fields.count, 17U);
161
162  // There are two oneofs in there.
163  XCTAssertEqual(descriptor.oneofs.count, 2U);
164
165  GPBFieldDescriptor *fooStringField =
166      [descriptor fieldWithNumber:TestOneof2_FieldNumber_FooString];
167  XCTAssertNotNil(fooStringField);
168  GPBFieldDescriptor *barStringField =
169      [descriptor fieldWithNumber:TestOneof2_FieldNumber_BarString];
170  XCTAssertNotNil(barStringField);
171
172  // Check the oneofs to have what is expected.
173
174  GPBOneofDescriptor *oneofFoo = [descriptor oneofWithName:@"foo"];
175  XCTAssertNotNil(oneofFoo);
176  XCTAssertEqual(oneofFoo.fields.count, 9U);
177
178  // Pointer comparisons.
179  XCTAssertEqual([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_FooString],
180                 fooStringField);
181  XCTAssertEqual([oneofFoo fieldWithName:@"fooString"], fooStringField);
182
183  GPBOneofDescriptor *oneofBar = [descriptor oneofWithName:@"bar"];
184  XCTAssertNotNil(oneofBar);
185  XCTAssertEqual(oneofBar.fields.count, 6U);
186
187  // Pointer comparisons.
188  XCTAssertEqual([oneofBar fieldWithNumber:TestOneof2_FieldNumber_BarString],
189                 barStringField);
190  XCTAssertEqual([oneofBar fieldWithName:@"barString"], barStringField);
191
192  // Unknown oneof not found.
193
194  XCTAssertNil([descriptor oneofWithName:@"mumble"]);
195  XCTAssertNil([descriptor oneofWithName:@"Foo"]);
196
197  // Unknown oneof item.
198
199  XCTAssertNil([oneofFoo fieldWithName:@"mumble"]);
200  XCTAssertNil([oneofFoo fieldWithNumber:666]);
201
202  // Field exists, but not in this oneof.
203
204  XCTAssertNil([oneofFoo fieldWithName:@"barString"]);
205  XCTAssertNil([oneofFoo fieldWithNumber:TestOneof2_FieldNumber_BarString]);
206  XCTAssertNil([oneofBar fieldWithName:@"fooString"]);
207  XCTAssertNil([oneofBar fieldWithNumber:TestOneof2_FieldNumber_FooString]);
208
209  // Check pointers back to the enclosing oneofs.
210  // (pointer comparisions)
211  XCTAssertEqual(fooStringField.containingOneof, oneofFoo);
212  XCTAssertEqual(barStringField.containingOneof, oneofBar);
213  GPBFieldDescriptor *bazString =
214      [descriptor fieldWithNumber:TestOneof2_FieldNumber_BazString];
215  XCTAssertNotNil(bazString);
216  XCTAssertNil(bazString.containingOneof);
217}
218
219@end
220