aboutsummaryrefslogtreecommitdiff
path: root/objectivec/Tests
diff options
context:
space:
mode:
authorTeBoring <teboring@google.com>2015-07-15 16:16:08 -0700
committerTeBoring <teboring@google.com>2015-07-21 15:45:02 -0700
commit7366efd81e7f36108aa35e66fca61da8a65762c2 (patch)
treec3b05157928bf27405115aea5835055066cb7022 /objectivec/Tests
parentfde6e89f99eda04a4f1b8677bcea07e6c2040405 (diff)
downloadprotobuf-7366efd81e7f36108aa35e66fca61da8a65762c2.tar.gz
protobuf-7366efd81e7f36108aa35e66fca61da8a65762c2.tar.bz2
protobuf-7366efd81e7f36108aa35e66fca61da8a65762c2.zip
Add packFrom, unpackTo and is in google.protobuf.Any.
The previous two methods make it easy to transform between any and normal message. unPackeTo will throw error if the type url in any doesn't match the type of the message to be transformed to. is checks any's type url matches the give GPBMessage type.
Diffstat (limited to 'objectivec/Tests')
-rw-r--r--objectivec/Tests/GPBWellKnownTypesTest.m53
1 files changed, 52 insertions, 1 deletions
diff --git a/objectivec/Tests/GPBWellKnownTypesTest.m b/objectivec/Tests/GPBWellKnownTypesTest.m
index 78f4e637..48c875aa 100644
--- a/objectivec/Tests/GPBWellKnownTypesTest.m
+++ b/objectivec/Tests/GPBWellKnownTypesTest.m
@@ -28,7 +28,9 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#import "google/protobuf/Unittest.pbobjc.h"
#import "GPBWellKnownTypes.h"
+#import "GPBTestUtilities.h"
#import <XCTest/XCTest.h>
@@ -38,7 +40,7 @@ static const NSTimeInterval kFutureOffsetInterval = 15000;
// Nanosecond time accuracy
static const NSTimeInterval kTimeAccuracy = 1e-9;
-@interface WellKnownTypesTest : XCTestCase
+@interface WellKnownTypesTest : GPBTestCase
@end
@implementation WellKnownTypesTest
@@ -99,4 +101,53 @@ static const NSTimeInterval kTimeAccuracy = 1e-9;
[duration2 release];
}
+- (void)testAnyPackingAndUnpacking {
+ TestAllTypes *from = [TestAllTypes message];
+ [self setAllFields:from repeatedCount:1];
+ NSData *data = from.data;
+
+ // Test initWithMessage
+ GPBAny *anyInited = [[GPBAny alloc] initWithMessage:from];
+ XCTAssertEqualObjects(
+ [GPBTypeGoogleApisComPrefix stringByAppendingString:from.descriptor.name],
+ anyInited.typeURL);
+ XCTAssertEqualObjects(data, anyInited.value);
+ [anyInited release];
+
+ // Test setMessage.
+ GPBAny *any = [GPBAny message];
+ [any setMessage:from];
+ XCTAssertEqualObjects(
+ [GPBTypeGoogleApisComPrefix stringByAppendingString:from.descriptor.name],
+ any.typeURL);
+ XCTAssertEqualObjects(data, any.value);
+
+ // Test messageOfClass
+ TestAllTypes *to = (TestAllTypes*)[any messageOfClass:[TestAllTypes class]];
+ XCTAssertEqualObjects(from, to);
+ XCTAssertEqual([any messageOfClass:[ForeignMessage class]], nil);
+ XCTAssertEqual([[GPBAny message] messageOfClass:[TestAllTypes class]], nil);
+
+ // Test setMessage with another type.
+ ForeignMessage *from2 = [ForeignMessage message];
+ [any setMessage:from2];
+ XCTAssertEqualObjects(
+ [GPBTypeGoogleApisComPrefix stringByAppendingString:from2.descriptor.name],
+ any.typeURL);
+ XCTAssertEqual(0UL, [any.value length]);
+
+ // Test wrapsMessageOfClass
+ XCTAssertTrue([any wrapsMessageOfClass:[from2 class]]);
+ XCTAssertFalse([any wrapsMessageOfClass:[from class]]);
+#if !defined(NS_BLOCK_ASSERTIONS)
+ // If assert is enabled, throw exception when the passed message class to
+ // wrapsMessageOfClass is not a child of GPBMessage.
+ XCTAssertThrows([any wrapsMessageOfClass:[NSString class]]);
+#else
+ // If assert is disabled, return false when the passed message class to
+ // wrapsMessageOfClass is not a child of GPBMessage.
+ XCTAssertFalse([any wrapsMessageOfClass:[NSString class]]);
+#endif
+}
+
@end