1// Protocol Buffers - Google's data interchange format 2// Copyright 2017 Google Inc. All rights reserved. 3// https://developers.google.com/protocol-buffers/ 4// 5// Redistribution and use in source and binary forms, with or without 6// modification, are permitted provided that the following conditions are 7// met: 8// 9// * Redistributions of source code must retain the above copyright 10// notice, this list of conditions and the following disclaimer. 11// * Redistributions in binary form must reproduce the above 12// copyright notice, this list of conditions and the following disclaimer 13// in the documentation and/or other materials provided with the 14// distribution. 15// * Neither the name of Google Inc. nor the names of its 16// contributors may be used to endorse or promote products derived from 17// this software without specific prior written permission. 18// 19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31#import "GPBTestUtilities.h" 32 33#import "GPBExtensionRegistry.h" 34#import "google/protobuf/Unittest.pbobjc.h" 35 36@interface GPBExtensionRegistryTest : GPBTestCase 37@end 38 39@implementation GPBExtensionRegistryTest 40 41- (void)testBasics { 42 GPBExtensionRegistry *reg = [[[GPBExtensionRegistry alloc] init] autorelease]; 43 XCTAssertNotNil(reg); 44 45 XCTAssertNil([reg extensionForDescriptor:[TestAllExtensions descriptor] 46 fieldNumber:1]); 47 XCTAssertNil([reg extensionForDescriptor:[TestAllTypes descriptor] 48 fieldNumber:1]); 49 50 [reg addExtension:[UnittestRoot optionalInt32Extension]]; 51 [reg addExtension:[UnittestRoot packedInt64Extension]]; 52 53 XCTAssertTrue([reg extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] == 54 [UnittestRoot optionalInt32Extension]); // ptr equality 55 XCTAssertNil([reg extensionForDescriptor:[TestAllTypes descriptor] 56 fieldNumber:1]); 57 XCTAssertTrue([reg extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] == 58 [UnittestRoot packedInt64Extension]); // ptr equality 59} 60 61- (void)testCopy { 62 GPBExtensionRegistry *reg1 = [[[GPBExtensionRegistry alloc] init] autorelease]; 63 [reg1 addExtension:[UnittestRoot optionalInt32Extension]]; 64 65 GPBExtensionRegistry *reg2 = [[reg1 copy] autorelease]; 66 XCTAssertNotNil(reg2); 67 68 XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] == 69 [UnittestRoot optionalInt32Extension]); // ptr equality 70 XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] == 71 [UnittestRoot optionalInt32Extension]); // ptr equality 72 73 // Message class that had registered extension(s) at the -copy time. 74 75 [reg1 addExtension:[UnittestRoot optionalBoolExtension]]; 76 [reg2 addExtension:[UnittestRoot optionalStringExtension]]; 77 78 XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13] == 79 [UnittestRoot optionalBoolExtension]); // ptr equality 80 XCTAssertNil([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14]); 81 XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13]); 82 XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14] == 83 [UnittestRoot optionalStringExtension]); // ptr equality 84 85 // Message class that did not have any registered extensions at the -copy time. 86 87 [reg1 addExtension:[UnittestRoot packedInt64Extension]]; 88 [reg2 addExtension:[UnittestRoot packedSint32Extension]]; 89 90 XCTAssertTrue([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] == 91 [UnittestRoot packedInt64Extension]); // ptr equality 92 XCTAssertNil([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94]); 93 XCTAssertNil([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91]); 94 XCTAssertTrue([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94] == 95 [UnittestRoot packedSint32Extension]); // ptr equality 96 97} 98 99- (void)testAddExtensions { 100 GPBExtensionRegistry *reg1 = [[[GPBExtensionRegistry alloc] init] autorelease]; 101 [reg1 addExtension:[UnittestRoot optionalInt32Extension]]; 102 103 GPBExtensionRegistry *reg2 = [[[GPBExtensionRegistry alloc] init] autorelease]; 104 105 XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor] 106 fieldNumber:1]); 107 108 [reg2 addExtensions:reg1]; 109 110 XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] == 111 [UnittestRoot optionalInt32Extension]); // ptr equality 112 113 // Confirm adding to the first doesn't add to the second. 114 115 [reg1 addExtension:[UnittestRoot optionalBoolExtension]]; 116 [reg1 addExtension:[UnittestRoot packedInt64Extension]]; 117 118 XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13] == 119 [UnittestRoot optionalBoolExtension]); // ptr equality 120 XCTAssertTrue([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] == 121 [UnittestRoot packedInt64Extension]); // ptr equality 122 XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13]); 123 XCTAssertNil([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91]); 124 125 // Confirm adding to the second doesn't add to the first. 126 127 [reg2 addExtension:[UnittestRoot optionalStringExtension]]; 128 [reg2 addExtension:[UnittestRoot packedSint32Extension]]; 129 130 XCTAssertNil([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14]); 131 XCTAssertNil([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94]); 132 XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14] == 133 [UnittestRoot optionalStringExtension]); // ptr equality 134 XCTAssertTrue([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94] == 135 [UnittestRoot packedSint32Extension]); // ptr equality 136} 137 138@end 139