[ObjC] Add test around enum unknown values.

PiperOrigin-RevId: 654858735
pull/17587/head
Thomas Van Lenten 4 months ago
parent cf7abf6c6b
commit fad7b78e1a
  1. 52
      objectivec/Tests/GPBMessageTests.m
  2. 31
      objectivec/Tests/unittest_objc.proto

@ -5,14 +5,14 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#import "GPBTestUtilities.h"
#import <objc/runtime.h>
#import "GPBArray.h"
#import "GPBArray_PackagePrivate.h"
#import "GPBDescriptor.h"
#import "GPBDictionary_PackagePrivate.h"
#import "GPBMessage_PackagePrivate.h"
#import "GPBTestUtilities.h"
#import "GPBUnknownFieldSet_PackagePrivate.h"
#import "GPBUnknownField_PackagePrivate.h"
#import "objectivec/Tests/Unittest.pbobjc.h"
@ -2022,6 +2022,54 @@
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo);
}
- (void)testCloseEnumsValuesOutOfRange {
// The unknown values should all make it into the unknown fields.
EnumTestMsg *msg1 = [EnumTestMsg message];
msg1.bar = EnumTestMsg_MyEnum_NegTwo;
msg1.baz = EnumTestMsg_MyEnum_Two;
[msg1.mumbleArray addValue:EnumTestMsg_MyEnum_Two];
[msg1.mumbleArray addValue:EnumTestMsg_MyEnum_NegTwo];
NSData *data = [msg1 data];
XCTAssertNotNil(data);
EnumTestMsgPrime *msg2 = [EnumTestMsgPrime parseFromData:data error:NULL];
XCTAssertNotNil(msg2);
XCTAssertEqualObjects(data, [msg2 data]);
XCTAssertFalse(msg2.hasBar);
XCTAssertFalse(msg2.hasBaz);
XCTAssertEqual(msg2.mumbleArray_Count, 0U);
GPBUnknownFields *ufs = [[[GPBUnknownFields alloc] initFromMessage:msg2] autorelease];
XCTAssertEqual(ufs.count, 4U);
uint64_t varint;
XCTAssertTrue([ufs getFirst:EnumTestMsg_FieldNumber_Bar varint:&varint]);
XCTAssertEqual(varint, (uint64_t)EnumTestMsg_MyEnum_NegTwo);
XCTAssertTrue([ufs getFirst:EnumTestMsg_FieldNumber_Baz varint:&varint]);
XCTAssertEqual(varint, (uint64_t)EnumTestMsg_MyEnum_Two);
NSArray<GPBUnknownField *> *fields = [ufs fields:EnumTestMsg_FieldNumber_MumbleArray];
XCTAssertEqual(fields.count, 2U);
XCTAssertEqual(fields[0].varint, (uint64_t)EnumTestMsg_MyEnum_Two);
XCTAssertEqual(fields[1].varint, (uint64_t)EnumTestMsg_MyEnum_NegTwo);
GPBUnknownFieldSet *unknownFields = msg2.unknownFields;
XCTAssertNotNil(unknownFields);
XCTAssertEqual(unknownFields.countOfFields, 3U);
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_Bar]);
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_Baz]);
XCTAssertTrue([unknownFields hasField:EnumTestMsg_FieldNumber_MumbleArray]);
GPBUnknownField *field = [unknownFields getField:EnumTestMsg_FieldNumber_Bar];
XCTAssertEqual(field.varintList.count, 1U);
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_NegTwo);
field = [unknownFields getField:EnumTestMsg_FieldNumber_Baz];
XCTAssertEqual(field.varintList.count, 1U);
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_Two);
field = [unknownFields getField:EnumTestMsg_FieldNumber_MumbleArray];
XCTAssertEqual(field.varintList.count, 2U);
XCTAssertEqual([field.varintList valueAtIndex:0], (uint64_t)EnumTestMsg_MyEnum_Two);
XCTAssertEqual([field.varintList valueAtIndex:1], (uint64_t)EnumTestMsg_MyEnum_NegTwo);
}
- (void)testReservedWordNaming {
// names.cc has some special handing to make sure that some "reserved" objc
// names get renamed in a way so they don't conflict.

@ -7,11 +7,11 @@
syntax = "proto2";
package objc.protobuf.tests;
import "google/protobuf/any.proto";
import "objectivec/Tests/unittest.proto";
package objc.protobuf.tests;
// Explicit empty prefix, tests some validations code paths also.
option objc_class_prefix = "";
@ -167,7 +167,6 @@ message self {
}
extensions 3000 to 3999;
}
enum retain {
@ -700,13 +699,17 @@ message JustToScopeExtensions {
repeated string mutableCopy_val_lower_complex_repeated = 2711;
repeated string mutableCopy_Val_upper_complex_repeated = 2712;
repeated string mutableCopyvalue_lower_no_underscore_complex_repeated = 2713;
repeated string mutableCopyValue_upper_no_underscore_complex_repeated = 2714;
repeated string mutableCopyvalue_lower_no_underscore_complex_repeated =
2713;
repeated string mutableCopyValue_upper_no_underscore_complex_repeated =
2714;
repeated int32 mutableCopy_val_lower_primitive_repeated = 2715;
repeated int32 mutableCopy_Val_upper_primitive_repeated = 2716;
repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated = 2717;
repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated = 2718;
repeated int32 mutableCopyvalue_lower_no_underscore_primitive_repeated =
2717;
repeated int32 mutableCopyValue_upper_no_underscore_primitive_repeated =
2718;
repeated self mutableCopy_val_lower_message_repeated = 2719;
repeated self mutableCopy_Val_upper_message_repeated = 2720;
@ -794,6 +797,20 @@ message EnumTestMsg {
repeated MyEnum mumble = 4;
}
message EnumTestMsgPrime {
enum MyEnumPrime {
ZERO = 0;
ONE = 1;
NEG_ONE = -1;
// Lacks 2, -2.
}
optional MyEnumPrime foo = 1;
optional MyEnumPrime bar = 2 [default = ONE];
optional MyEnumPrime baz = 3 [default = NEG_ONE];
repeated MyEnumPrime mumble = 4;
}
// Test case for https://github.com/protocolbuffers/protobuf/issues/1453
// Message with no explicit defaults, but a non zero default for an enum.
message MessageWithOneBasedEnum {

Loading…
Cancel
Save