diff options
author | Thomas Van Lenten <thomasvl@google.com> | 2016-08-12 11:46:55 -0400 |
---|---|---|
committer | Thomas Van Lenten <thomasvl@google.com> | 2016-08-12 11:46:55 -0400 |
commit | 17d601a2b64035399645ef5c7067ee967d9f34d0 (patch) | |
tree | a9de108db704f8ed497b0e0af2073391072c1340 /objectivec/Tests/GPBMessageTests+Runtime.m | |
parent | 91b6d04e2da90b66c038eee19855729ba32d493f (diff) | |
download | protobuf-17d601a2b64035399645ef5c7067ee967d9f34d0.tar.gz protobuf-17d601a2b64035399645ef5c7067ee967d9f34d0.tar.bz2 protobuf-17d601a2b64035399645ef5c7067ee967d9f34d0.zip |
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.
Diffstat (limited to 'objectivec/Tests/GPBMessageTests+Runtime.m')
-rw-r--r-- | objectivec/Tests/GPBMessageTests+Runtime.m | 123 |
1 files changed, 119 insertions, 4 deletions
diff --git a/objectivec/Tests/GPBMessageTests+Runtime.m b/objectivec/Tests/GPBMessageTests+Runtime.m index e963d180..c7ceb539 100644 --- a/objectivec/Tests/GPBMessageTests+Runtime.m +++ b/objectivec/Tests/GPBMessageTests+Runtime.m @@ -326,6 +326,17 @@ //% [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() //%PROTO2_TEST_HAS_FIELD(Int32, 1, 0) //%PROTO2_TEST_HAS_FIELD(Int64, 1, 0) @@ -347,6 +358,14 @@ //% // //% //%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() // This block of code is generated, do not edit it directly. @@ -658,13 +677,57 @@ [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() } - (void)testProto3SingleFieldHasBehavior { // - // Setting to any value including the default value (0) should result has* - // being true. + // Setting to any value but the default value (0) should result has* + // being true. When set to the default, shouldn't be true. // //%PDDM-DEFINE PROTO3_TEST_HAS_FIELD(FIELD, NON_ZERO_VALUE, ZERO_VALUE) @@ -678,6 +741,17 @@ //% [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() //%PROTO3_TEST_HAS_FIELD(Int32, 1, 0) //%PROTO3_TEST_HAS_FIELD(Int64, 1, 0) @@ -695,10 +769,17 @@ //%PROTO3_TEST_HAS_FIELD(String, @"foo", @"") //%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) +//% // +//% // 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() // This block of code is generated, do not edit it directly. @@ -853,7 +934,7 @@ } // - // Test doesn't apply to optionalGroup/optionalMessage. + // Test doesn't apply to optionalMessage (no groups in proto3). // { // optionalEnum @@ -866,6 +947,40 @@ [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() } |