Skip to content

Commit fad7b78

Browse files
committed
[ObjC] Add test around enum unknown values.
PiperOrigin-RevId: 654858735
1 parent cf7abf6 commit fad7b78

File tree

2 files changed

+152
-87
lines changed

2 files changed

+152
-87
lines changed

objectivec/Tests/GPBMessageTests.m

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
// license that can be found in the LICENSE file or at
66
// https://developers.google.com/open-source/licenses/bsd
77

8-
#import "GPBTestUtilities.h"
9-
108
#import <objc/runtime.h>
119

10+
#import "GPBArray.h"
1211
#import "GPBArray_PackagePrivate.h"
1312
#import "GPBDescriptor.h"
1413
#import "GPBDictionary_PackagePrivate.h"
1514
#import "GPBMessage_PackagePrivate.h"
15+
#import "GPBTestUtilities.h"
1616
#import "GPBUnknownFieldSet_PackagePrivate.h"
1717
#import "GPBUnknownField_PackagePrivate.h"
1818
#import "objectivec/Tests/Unittest.pbobjc.h"
@@ -2022,6 +2022,54 @@ - (void)testNegativeEnums {
20222022
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo);
20232023
}
20242024

2025+
- (void)testCloseEnumsValuesOutOfRange {
2026+
// The unknown values should all make it into the unknown fields.
2027+
EnumTestMsg *msg1 = [EnumTestMsg message];
2028+
msg1.bar = EnumTestMsg_MyEnum_NegTwo;
2029+
msg1.baz = EnumTestMsg_MyEnum_Two;
2030+
[msg1.mumbleArray addValue:EnumTestMsg_MyEnum_Two];
2031+
[msg1.mumbleArray addValue:EnumTestMsg_MyEnum_NegTwo];
2032+
2033+
NSData *data = [msg1 data];
2034+
XCTAssertNotNil(data);
2035+
2036+
EnumTestMsgPrime *msg2 = [EnumTestMsgPrime parseFromData:data error:NULL];
2037+
XCTAssertNotNil(msg2);
2038+
XCTAssertEqualObjects(data, [msg2 data]);
2039+
XCTAssertFalse(msg2.hasBar);
2040+
XCTAssertFalse(msg2.hasBaz);
2041+
XCTAssertEqual(msg2.mumbleArray_Count, 0U);
2042+
2043+
GPBUnknownFields *ufs = [[[GPBUnknownFields alloc] initFromMessage:msg2] autorelease];
2044+
XCTAssertEqual(ufs.count, 4U);
2045+
uint64_t varint;
2046+
XCTAssertTrue([ufs getFirst:EnumTestMsg_FieldNumber_Bar varint:&varint]);
2047+
XCTAssertEqual(varint, (uint64_t)EnumTestMsg_MyEnum_NegTwo);
2048+
XCTAssertTrue([ufs getFirst:EnumTestMsg_FieldNumber_Baz varint:&varint]);
2049+
XCTAssertEqual(varint, (uint64_t)EnumTestMsg_MyEnum_Two);
2050+
NSArray<GPBUnknownField *> *fields = [ufs fields:EnumTestMsg_FieldNumber_MumbleArray];
2051+
XCTAssertEqual(fields.count, 2U);
2052+
XCTAssertEqual(fields[0].varint, (uint64_t)EnumTestMsg_MyEnum_Two);
2053+
XCTAssertEqual(fields[1].varint, (uint64_t)EnumTestMsg_MyEnum_NegTwo);
2054+
2055+
GPBUnknownFieldSet *unknownFields = msg2.unknownFields;
2056+
XCTAssertNotNil(unknownFields);
2057+
XCTAssertEqual(unknownFields.countOfFields, 3U);
2058+
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_Bar]);
2059+
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_Baz]);
2060+
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_MumbleArray]);
2061+
GPBUnknownField *field = [unknownFields getField:EnumTestMsg_FieldNumber_Bar];
2062+
XCTAssertEqual(field.varintList.count, 1U);
2063+
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_NegTwo);
2064+
field = [unknownFields getField:EnumTestMsg_FieldNumber_Baz];
2065+
XCTAssertEqual(field.varintList.count, 1U);
2066+
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_Two);
2067+
field = [unknownFields getField:EnumTestMsg_FieldNumber_MumbleArray];
2068+
XCTAssertEqual(field.varintList.count, 2U);
2069+
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_Two);
2070+
XCTAssertEqual([field.varintList valueAtIndex:1], (uint64_t)EnumTestMsg_MyEnum_NegTwo);
2071+
}
2072+
20252073
- (void)testReservedWordNaming {
20262074
// names.cc has some special handing to make sure that some "reserved" objc
20272075
// names get renamed in a way so they don't conflict.

objectivec/Tests/unittest_objc.proto

Lines changed: 102 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
syntax = "proto2";
99

10+
package objc.protobuf.tests;
11+
1012
import "google/protobuf/any.proto";
1113
import "objectivec/Tests/unittest.proto";
1214

13-
package objc.protobuf.tests;
14-
1515
// Explicit empty prefix, tests some validations code paths also.
1616
option objc_class_prefix = "";
1717

@@ -78,143 +78,142 @@ message self {
7878
}
7979

8080
enum autorelease {
81-
retain = 1;
82-
release = 2;
81+
retain = 1;
82+
release = 2;
8383
retainCount = 3;
8484
}
8585

8686
// Singular
8787
// Objective C Keywords
88-
optional bool id = 1;
89-
optional bool _cmd = 2;
88+
optional bool id = 1;
89+
optional bool _cmd = 2;
9090
// super is used as submessage above
91-
optional bool in = 4;
92-
optional bool out = 5;
93-
optional bool inout = 6;
94-
optional bool bycopy = 7;
95-
optional bool byref = 8;
96-
optional bool oneway = 9;
97-
optional bool self = 10;
98-
optional bool instancetype = 11;
99-
optional bool nullable = 12;
100-
optional bool nonnull = 13;
101-
optional bool nil = 14;
91+
optional bool in = 4;
92+
optional bool out = 5;
93+
optional bool inout = 6;
94+
optional bool bycopy = 7;
95+
optional bool byref = 8;
96+
optional bool oneway = 9;
97+
optional bool self = 10;
98+
optional bool instancetype = 11;
99+
optional bool nullable = 12;
100+
optional bool nonnull = 13;
101+
optional bool nil = 14;
102102
// Nil and nil can't be in the same message
103-
optional bool YES = 16;
104-
optional bool NO = 17;
105-
optional bool weak = 18;
103+
optional bool YES = 16;
104+
optional bool NO = 17;
105+
optional bool weak = 18;
106106

107107
// Some C/C++ Keywords
108-
optional bool case = 30;
109-
optional bool if = 31;
110-
optional bool and_eq = 32;
111-
optional bool public = 33;
112-
optional bool private = 34;
113-
optional bool typename = 35;
114-
optional bool static_cast = 36;
115-
optional bool typeof = 37;
116-
optional bool restrict = 38;
117-
optional bool NULL = 39;
108+
optional bool case = 30;
109+
optional bool if = 31;
110+
optional bool and_eq = 32;
111+
optional bool public = 33;
112+
optional bool private = 34;
113+
optional bool typename = 35;
114+
optional bool static_cast = 36;
115+
optional bool typeof = 37;
116+
optional bool restrict = 38;
117+
optional bool NULL = 39;
118118

119119
// Some NSObject Methods
120-
optional bool dealloc = 110;
121-
optional bool isProxy = 111;
122-
optional bool copy = 112;
123-
optional bool description = 113;
124-
optional bool zone = 114;
125-
optional bool className = 115;
126-
optional bool __retain_OA = 116;
127-
optional bool CAMLType = 117;
128-
optional bool isNSDictionary__ = 118;
120+
optional bool dealloc = 110;
121+
optional bool isProxy = 111;
122+
optional bool copy = 112;
123+
optional bool description = 113;
124+
optional bool zone = 114;
125+
optional bool className = 115;
126+
optional bool __retain_OA = 116;
127+
optional bool CAMLType = 117;
128+
optional bool isNSDictionary__ = 118;
129129
optional bool accessibilityLabel = 119;
130130

131131
// Some Objc "keywords" that we shouldn't
132132
// have to worry about because they
133133
// can only appear in specialized areas.
134-
optional bool assign = 200;
135-
optional bool getter = 201;
136-
optional bool setter = 202;
137-
optional bool atomic = 203;
138-
optional bool nonatomic = 204;
139-
optional bool strong = 205;
140-
optional bool null_resettable = 206;
141-
optional bool readonly = 207;
134+
optional bool assign = 200;
135+
optional bool getter = 201;
136+
optional bool setter = 202;
137+
optional bool atomic = 203;
138+
optional bool nonatomic = 204;
139+
optional bool strong = 205;
140+
optional bool null_resettable = 206;
141+
optional bool readonly = 207;
142142

143143
// Some GPBMessage methods
144-
optional bool clear = 300;
145-
optional bool data = 301;
146-
optional bool descriptor = 302;
147-
optional bool delimitedData = 303;
144+
optional bool clear = 300;
145+
optional bool data = 301;
146+
optional bool descriptor = 302;
147+
optional bool delimitedData = 303;
148148

149149
// Some MacTypes
150-
optional bool Fixed = 400;
151-
optional bool Point = 401;
152-
optional bool FixedPoint = 402;
153-
optional bool Style = 403;
150+
optional bool Fixed = 400;
151+
optional bool Point = 401;
152+
optional bool FixedPoint = 402;
153+
optional bool Style = 403;
154154

155155
// C/C++ reserved identifiers
156-
optional bool _Generic = 500;
157-
optional bool __block = 501;
156+
optional bool _Generic = 500;
157+
optional bool __block = 501;
158158

159159
// Try a keyword as a type
160-
optional autorelease SubEnum = 1000;
160+
optional autorelease SubEnum = 1000;
161161

162162
optional group New = 2000 {
163-
optional string copy = 1;
163+
optional string copy = 1;
164164
}
165165
optional group MutableCopy = 2001 {
166166
optional int32 extensionRegistry = 1;
167167
}
168168

169169
extensions 3000 to 3999;
170-
171170
}
172171

173172
enum retain {
174-
count = 4;
175-
initialized = 5;
173+
count = 4;
174+
initialized = 5;
176175
serializedSize = 6;
177176
}
178177

179178
message ObjCPropertyNaming {
180179
// Test that the properties properly get things all caps.
181-
optional string url = 1;
180+
optional string url = 1;
182181
optional string thumbnail_url = 2;
183-
optional string url_foo = 3;
182+
optional string url_foo = 3;
184183
optional string some_url_blah = 4;
185-
optional string http = 5;
186-
optional string https = 6;
184+
optional string http = 5;
185+
optional string https = 6;
187186
// This one doesn't.
188-
repeated string urls = 7;
187+
repeated string urls = 7;
189188
}
190189

191190
// EnumValueShortName: The short names shouldn't get suffixes/prefixes.
192191
enum Foo {
193192
SERIALIZED_SIZE = 1;
194-
SIZE = 2;
195-
OTHER = 3;
193+
SIZE = 2;
194+
OTHER = 3;
196195
}
197196

198197
// EnumValueShortName: The enum name gets a prefix.
199198
enum Category {
200-
RED = 1;
199+
RED = 1;
201200
BLUE = 2;
202201
}
203202

204203
// EnumValueShortName: Twist case, full name gets PB, but the short names
205204
// should still end up correct.
206205
enum Time {
207-
BASE = 1;
208-
RECORD = 2;
209-
SOMETHING_ELSE = 3;
206+
BASE = 1;
207+
RECORD = 2;
208+
SOMETHING_ELSE = 3;
210209
}
211210

212211
extend self {
213-
repeated int32 debugDescription = 3000 [packed = true];
214-
repeated int64 finalize = 3001 [packed = true];
215-
repeated uint32 hash = 3002 [packed = true];
216-
repeated uint64 classForCoder = 3003 [packed = true];
217-
repeated sint32 byref = 3004 [packed = true];
212+
repeated int32 debugDescription = 3000 [packed = true];
213+
repeated int64 finalize = 3001 [packed = true];
214+
repeated uint32 hash = 3002 [packed = true];
215+
repeated uint64 classForCoder = 3003 [packed = true];
216+
repeated sint32 byref = 3004 [packed = true];
218217
}
219218

220219
// Test handing of fields that start with init*.
@@ -700,13 +699,17 @@ message JustToScopeExtensions {
700699

701700
repeated string mutableCopy_val_lower_complex_repeated = 2711;
702701
repeated string mutableCopy_Val_upper_complex_repeated = 2712;
703-
repeated string mutableCopyvalue_lower_no_underscore_complex_repeated = 2713;
704-
repeated string mutableCopyValue_upper_no_underscore_complex_repeated = 2714;
702+
repeated string mutableCopyvalue_lower_no_underscore_complex_repeated =
703+
2713;
704+
repeated string mutableCopyValue_upper_no_underscore_complex_repeated =
705+
2714;
705706

706707
repeated int32 mutableCopy_val_lower_primitive_repeated = 2715;
707708
repeated int32 mutableCopy_Val_upper_primitive_repeated = 2716;
708-
repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated = 2717;
709-
repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated = 2718;
709+
repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated =
710+
2717;
711+
repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated =
712+
2718;
710713

711714
repeated self mutableCopy_val_lower_message_repeated = 2719;
712715
repeated self mutableCopy_Val_upper_message_repeated = 2720;
@@ -781,9 +784,9 @@ message ObjcWeirdDefaults {
781784
// Used to confirm negative enum values work as expected.
782785
message EnumTestMsg {
783786
enum MyEnum {
784-
ZERO = 0;
785-
ONE = 1;
786-
TWO = 2;
787+
ZERO = 0;
788+
ONE = 1;
789+
TWO = 2;
787790
NEG_ONE = -1;
788791
NEG_TWO = -2;
789792
}
@@ -794,6 +797,20 @@ message EnumTestMsg {
794797
repeated MyEnum mumble = 4;
795798
}
796799

800+
message EnumTestMsgPrime {
801+
enum MyEnumPrime {
802+
ZERO = 0;
803+
ONE = 1;
804+
NEG_ONE = -1;
805+
// Lacks 2, -2.
806+
}
807+
optional MyEnumPrime foo = 1;
808+
optional MyEnumPrime bar = 2 [default = ONE];
809+
optional MyEnumPrime baz = 3 [default = NEG_ONE];
810+
811+
repeated MyEnumPrime mumble = 4;
812+
}
813+
797814
// Test case for https://github.com/protocolbuffers/protobuf/issues/1453
798815
// Message with no explicit defaults, but a non zero default for an enum.
799816
message MessageWithOneBasedEnum {

0 commit comments

Comments
 (0)