GPBFilteredMessageTests.m 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2013 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. // Tests our filter system for ObjC.
  31. // The proto being filtered is unittest_filter.proto.
  32. // The filter file is Filter.txt.
  33. #import "GPBTestUtilities.h"
  34. #import "google/protobuf/UnittestFilter.pbobjc.h"
  35. // If we get an error about this already being defined, it is most likely
  36. // because of an error in protoc which is supposed to be filtering
  37. // the Remove message.
  38. enum { Other_FieldNumber_B = 0 };
  39. @interface FilteredMessageTests : GPBTestCase
  40. @end
  41. @implementation FilteredMessageTests
  42. - (void)testEnumFiltering {
  43. // If compile fails here it is because protoc did not generate KeepEnum.
  44. XCTAssertTrue(KeepEnum_IsValidValue(KeepEnum_KeepValue));
  45. XCTAssertNotNil(KeepEnum_EnumDescriptor());
  46. // If compile fails here it is because protoc did not generate
  47. // KeepEnumInsideEnum and is probably due to nested enum handling being
  48. // broken.
  49. XCTAssertTrue(RemoveEnumMessage_KeepEnumInside_IsValidValue(
  50. RemoveEnumMessage_KeepEnumInside_KeepValue));
  51. XCTAssertNotNil(RemoveEnumMessage_KeepEnumInside_EnumDescriptor());
  52. }
  53. - (void)testMessageFiltering {
  54. // Messages that should be generated.
  55. XCTAssertNil([UnittestFilterRoot extensionRegistry]);
  56. XCTAssertNotNil([[[Keep alloc] init] autorelease]);
  57. XCTAssertNotNil([[[Other alloc] init] autorelease]);
  58. XCTAssertNotNil([[[RemoveJustKidding alloc] init] autorelease]);
  59. XCTAssertNotNil(
  60. [[[RemoveEnumMessage_KeepNestedInside alloc] init] autorelease]);
  61. // Messages that should not be generated
  62. XCTAssertNil(NSClassFromString(@"Remove"));
  63. XCTAssertNil(NSClassFromString(@"RemoveEnumMessage"));
  64. XCTAssertNil(NSClassFromString(@"RemoveEnumMessage_RemoveNestedInside"));
  65. // These should all fail compile if protoc is bad.
  66. XCTAssertTrue([Other instancesRespondToSelector:@selector(hasA)]);
  67. XCTAssertTrue([Other instancesRespondToSelector:@selector(setHasA:)]);
  68. XCTAssertTrue([Other instancesRespondToSelector:@selector(a)]);
  69. XCTAssertTrue([Other instancesRespondToSelector:@selector(setA:)]);
  70. // These the compiler should not generate.
  71. XCTAssertFalse(
  72. [Other instancesRespondToSelector:NSSelectorFromString(@"hasB")]);
  73. XCTAssertFalse(
  74. [Other instancesRespondToSelector:NSSelectorFromString(@"setHasB:")]);
  75. XCTAssertFalse([Other instancesRespondToSelector:NSSelectorFromString(@"b")]);
  76. XCTAssertFalse(
  77. [Other instancesRespondToSelector:NSSelectorFromString(@"setB:")]);
  78. // This should fail if protoc filters it.
  79. XCTAssertEqual(Other_FieldNumber_A, 1);
  80. // Make sure the definition at the top of the file is providing the value.
  81. XCTAssertEqual(Other_FieldNumber_B, 0);
  82. }
  83. @end