GPBExtensionRegistryTest.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #import "GPBTestUtilities.h"
  31. #import "GPBExtensionRegistry.h"
  32. #import "google/protobuf/Unittest.pbobjc.h"
  33. @interface GPBExtensionRegistryTest : GPBTestCase
  34. @end
  35. @implementation GPBExtensionRegistryTest
  36. - (void)testBasics {
  37. GPBExtensionRegistry *reg = [[[GPBExtensionRegistry alloc] init] autorelease];
  38. XCTAssertNotNil(reg);
  39. XCTAssertNil([reg extensionForDescriptor:[TestAllExtensions descriptor]
  40. fieldNumber:1]);
  41. XCTAssertNil([reg extensionForDescriptor:[TestAllTypes descriptor]
  42. fieldNumber:1]);
  43. [reg addExtension:[UnittestRoot optionalInt32Extension]];
  44. [reg addExtension:[UnittestRoot packedInt64Extension]];
  45. XCTAssertTrue([reg extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] ==
  46. [UnittestRoot optionalInt32Extension]); // ptr equality
  47. XCTAssertNil([reg extensionForDescriptor:[TestAllTypes descriptor]
  48. fieldNumber:1]);
  49. XCTAssertTrue([reg extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] ==
  50. [UnittestRoot packedInt64Extension]); // ptr equality
  51. }
  52. - (void)testCopy {
  53. GPBExtensionRegistry *reg1 = [[[GPBExtensionRegistry alloc] init] autorelease];
  54. [reg1 addExtension:[UnittestRoot optionalInt32Extension]];
  55. GPBExtensionRegistry *reg2 = [[reg1 copy] autorelease];
  56. XCTAssertNotNil(reg2);
  57. XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] ==
  58. [UnittestRoot optionalInt32Extension]); // ptr equality
  59. XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] ==
  60. [UnittestRoot optionalInt32Extension]); // ptr equality
  61. // Message class that had registered extension(s) at the -copy time.
  62. [reg1 addExtension:[UnittestRoot optionalBoolExtension]];
  63. [reg2 addExtension:[UnittestRoot optionalStringExtension]];
  64. XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13] ==
  65. [UnittestRoot optionalBoolExtension]); // ptr equality
  66. XCTAssertNil([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14]);
  67. XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13]);
  68. XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14] ==
  69. [UnittestRoot optionalStringExtension]); // ptr equality
  70. // Message class that did not have any registered extensions at the -copy time.
  71. [reg1 addExtension:[UnittestRoot packedInt64Extension]];
  72. [reg2 addExtension:[UnittestRoot packedSint32Extension]];
  73. XCTAssertTrue([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] ==
  74. [UnittestRoot packedInt64Extension]); // ptr equality
  75. XCTAssertNil([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94]);
  76. XCTAssertNil([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91]);
  77. XCTAssertTrue([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94] ==
  78. [UnittestRoot packedSint32Extension]); // ptr equality
  79. }
  80. - (void)testAddExtensions {
  81. GPBExtensionRegistry *reg1 = [[[GPBExtensionRegistry alloc] init] autorelease];
  82. [reg1 addExtension:[UnittestRoot optionalInt32Extension]];
  83. GPBExtensionRegistry *reg2 = [[[GPBExtensionRegistry alloc] init] autorelease];
  84. XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor]
  85. fieldNumber:1]);
  86. [reg2 addExtensions:reg1];
  87. XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:1] ==
  88. [UnittestRoot optionalInt32Extension]); // ptr equality
  89. // Confirm adding to the first doesn't add to the second.
  90. [reg1 addExtension:[UnittestRoot optionalBoolExtension]];
  91. [reg1 addExtension:[UnittestRoot packedInt64Extension]];
  92. XCTAssertTrue([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13] ==
  93. [UnittestRoot optionalBoolExtension]); // ptr equality
  94. XCTAssertTrue([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91] ==
  95. [UnittestRoot packedInt64Extension]); // ptr equality
  96. XCTAssertNil([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:13]);
  97. XCTAssertNil([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:91]);
  98. // Confirm adding to the second doesn't add to the first.
  99. [reg2 addExtension:[UnittestRoot optionalStringExtension]];
  100. [reg2 addExtension:[UnittestRoot packedSint32Extension]];
  101. XCTAssertNil([reg1 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14]);
  102. XCTAssertNil([reg1 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94]);
  103. XCTAssertTrue([reg2 extensionForDescriptor:[TestAllExtensions descriptor] fieldNumber:14] ==
  104. [UnittestRoot optionalStringExtension]); // ptr equality
  105. XCTAssertTrue([reg2 extensionForDescriptor:[TestPackedExtensions descriptor] fieldNumber:94] ==
  106. [UnittestRoot packedSint32Extension]); // ptr equality
  107. }
  108. @end