Commit 17d601a2 authored by Thomas Van Lenten's avatar Thomas Van Lenten

More explicit tests for nil behaviors on fields.

Code already works, but make a bank of tests that call it out so things are
very clean in the future just to be safe.
parent 91b6d04e
...@@ -326,6 +326,17 @@ ...@@ -326,6 +326,17 @@
//% [msg release]; //% [msg release];
//% } //% }
//% //%
//%PDDM-DEFINE PROTO2_TEST_CLEAR_FIELD_WITH_NIL(FIELD, VALUE)
//% { // optional##FIELD
//% Message2 *msg = [[Message2 alloc] init];
//% XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
//% msg.optional##FIELD = VALUE;
//% XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
//% msg.optional##FIELD = nil;
//% XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_Optional##FIELD));
//% [msg release];
//% }
//%
//%PDDM-DEFINE PROTO2_TEST_HAS_FIELDS() //%PDDM-DEFINE PROTO2_TEST_HAS_FIELDS()
//%PROTO2_TEST_HAS_FIELD(Int32, 1, 0) //%PROTO2_TEST_HAS_FIELD(Int32, 1, 0)
//%PROTO2_TEST_HAS_FIELD(Int64, 1, 0) //%PROTO2_TEST_HAS_FIELD(Int64, 1, 0)
...@@ -347,6 +358,14 @@ ...@@ -347,6 +358,14 @@
//% // //% //
//% //%
//%PROTO2_TEST_HAS_FIELD(Enum, Message2_Enum_Bar, Message2_Enum_Foo) //%PROTO2_TEST_HAS_FIELD(Enum, Message2_Enum_Bar, Message2_Enum_Foo)
//% //
//% // Nil can also be used to clear strings, bytes, groups, and messages.
//% //
//%
//%PROTO2_TEST_CLEAR_FIELD_WITH_NIL(String, @"foo")
//%PROTO2_TEST_CLEAR_FIELD_WITH_NIL(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding])
//%PROTO2_TEST_CLEAR_FIELD_WITH_NIL(Group, [Message2_OptionalGroup message])
//%PROTO2_TEST_CLEAR_FIELD_WITH_NIL(Message, [Message2 message])
//%PDDM-EXPAND PROTO2_TEST_HAS_FIELDS() //%PDDM-EXPAND PROTO2_TEST_HAS_FIELDS()
// This block of code is generated, do not edit it directly. // This block of code is generated, do not edit it directly.
...@@ -658,13 +677,57 @@ ...@@ -658,13 +677,57 @@
[msg release]; [msg release];
} }
//
// Nil can also be used to clear strings, bytes, groups, and messages.
//
{ // optionalString
Message2 *msg = [[Message2 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
msg.optionalString = @"foo";
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
msg.optionalString = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalString));
[msg release];
}
{ // optionalBytes
Message2 *msg = [[Message2 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
msg.optionalBytes = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
msg.optionalBytes = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalBytes));
[msg release];
}
{ // optionalGroup
Message2 *msg = [[Message2 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalGroup));
msg.optionalGroup = [Message2_OptionalGroup message];
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalGroup));
msg.optionalGroup = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalGroup));
[msg release];
}
{ // optionalMessage
Message2 *msg = [[Message2 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalMessage));
msg.optionalMessage = [Message2 message];
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalMessage));
msg.optionalMessage = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message2_FieldNumber_OptionalMessage));
[msg release];
}
//%PDDM-EXPAND-END PROTO2_TEST_HAS_FIELDS() //%PDDM-EXPAND-END PROTO2_TEST_HAS_FIELDS()
} }
- (void)testProto3SingleFieldHasBehavior { - (void)testProto3SingleFieldHasBehavior {
// //
// Setting to any value including the default value (0) should result has* // Setting to any value but the default value (0) should result has*
// being true. // being true. When set to the default, shouldn't be true.
// //
//%PDDM-DEFINE PROTO3_TEST_HAS_FIELD(FIELD, NON_ZERO_VALUE, ZERO_VALUE) //%PDDM-DEFINE PROTO3_TEST_HAS_FIELD(FIELD, NON_ZERO_VALUE, ZERO_VALUE)
...@@ -678,6 +741,17 @@ ...@@ -678,6 +741,17 @@
//% [msg release]; //% [msg release];
//% } //% }
//% //%
//%PDDM-DEFINE PROTO3_TEST_CLEAR_FIELD_WITH_NIL(FIELD, VALUE)
//% { // optional##FIELD
//% Message3 *msg = [[Message3 alloc] init];
//% XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
//% msg.optional##FIELD = VALUE;
//% XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
//% msg.optional##FIELD = nil;
//% XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_Optional##FIELD));
//% [msg release];
//% }
//%
//%PDDM-DEFINE PROTO3_TEST_HAS_FIELDS() //%PDDM-DEFINE PROTO3_TEST_HAS_FIELDS()
//%PROTO3_TEST_HAS_FIELD(Int32, 1, 0) //%PROTO3_TEST_HAS_FIELD(Int32, 1, 0)
//%PROTO3_TEST_HAS_FIELD(Int64, 1, 0) //%PROTO3_TEST_HAS_FIELD(Int64, 1, 0)
...@@ -695,10 +769,17 @@ ...@@ -695,10 +769,17 @@
//%PROTO3_TEST_HAS_FIELD(String, @"foo", @"") //%PROTO3_TEST_HAS_FIELD(String, @"foo", @"")
//%PROTO3_TEST_HAS_FIELD(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding], [NSData data]) //%PROTO3_TEST_HAS_FIELD(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding], [NSData data])
//% // //% //
//% // Test doesn't apply to optionalGroup/optionalMessage. //% // Test doesn't apply to optionalMessage (no groups in proto3).
//% // //% //
//% //%
//%PROTO3_TEST_HAS_FIELD(Enum, Message3_Enum_Bar, Message3_Enum_Foo) //%PROTO3_TEST_HAS_FIELD(Enum, Message3_Enum_Bar, Message3_Enum_Foo)
//% //
//% // Nil can also be used to clear strings, bytes, and messages (no groups in proto3).
//% //
//%
//%PROTO3_TEST_CLEAR_FIELD_WITH_NIL(String, @"foo")
//%PROTO3_TEST_CLEAR_FIELD_WITH_NIL(Bytes, [@"foo" dataUsingEncoding:NSUTF8StringEncoding])
//%PROTO3_TEST_CLEAR_FIELD_WITH_NIL(Message, [Message3 message])
//%PDDM-EXPAND PROTO3_TEST_HAS_FIELDS() //%PDDM-EXPAND PROTO3_TEST_HAS_FIELDS()
// This block of code is generated, do not edit it directly. // This block of code is generated, do not edit it directly.
...@@ -853,7 +934,7 @@ ...@@ -853,7 +934,7 @@
} }
// //
// Test doesn't apply to optionalGroup/optionalMessage. // Test doesn't apply to optionalMessage (no groups in proto3).
// //
{ // optionalEnum { // optionalEnum
...@@ -866,6 +947,40 @@ ...@@ -866,6 +947,40 @@
[msg release]; [msg release];
} }
//
// Nil can also be used to clear strings, bytes, and messages (no groups in proto3).
//
{ // optionalString
Message3 *msg = [[Message3 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
msg.optionalString = @"foo";
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
msg.optionalString = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalString));
[msg release];
}
{ // optionalBytes
Message3 *msg = [[Message3 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
msg.optionalBytes = [@"foo" dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
msg.optionalBytes = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalBytes));
[msg release];
}
{ // optionalMessage
Message3 *msg = [[Message3 alloc] init];
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalMessage));
msg.optionalMessage = [Message3 message];
XCTAssertTrue(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalMessage));
msg.optionalMessage = nil;
XCTAssertFalse(GPBMessageHasFieldNumberSet(msg, Message3_FieldNumber_OptionalMessage));
[msg release];
}
//%PDDM-EXPAND-END PROTO3_TEST_HAS_FIELDS() //%PDDM-EXPAND-END PROTO3_TEST_HAS_FIELDS()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment