GULRuntimeSnapshotTests.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import <objc/runtime.h>
  16. #import <GoogleUtilities/GULRuntimeDiff.h>
  17. #import <GoogleUtilities/GULRuntimeSnapshot.h>
  18. @interface GULRuntimeSnapshotTestsTestClass : NSObject
  19. @end
  20. @implementation GULRuntimeSnapshotTestsTestClass
  21. - (NSString *)description {
  22. return [super description];
  23. }
  24. @end
  25. @interface GULRuntimeSnapshotTests : XCTestCase
  26. @end
  27. @implementation GULRuntimeSnapshotTests
  28. /** Tests default init. */
  29. - (void)testInitDoesntThrow {
  30. XCTAssertNoThrow([[GULRuntimeSnapshot alloc] init]);
  31. }
  32. /** Tests the designated initializer. */
  33. - (void)testDesignatedInitializer {
  34. XCTAssertNoThrow([[GULRuntimeSnapshot alloc] initWithClasses:nil]);
  35. NSSet *classes = [NSSet setWithObjects:[NSString class], [NSObject class], [self class], nil];
  36. XCTAssertNoThrow([[GULRuntimeSnapshot alloc] initWithClasses:classes]);
  37. }
  38. /** Tests equality of snapshots. */
  39. - (void)testEquality {
  40. GULRuntimeSnapshot *snapshot1 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  41. GULRuntimeSnapshot *snapshot2 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  42. XCTAssertEqualObjects(snapshot1, snapshot2);
  43. snapshot1 = nil;
  44. snapshot2 = nil;
  45. NSSet *classSet = [NSSet setWithObject:[GULRuntimeSnapshotTestsTestClass class]];
  46. snapshot1 = [[GULRuntimeSnapshot alloc] initWithClasses:classSet];
  47. snapshot2 = [[GULRuntimeSnapshot alloc] initWithClasses:classSet];
  48. XCTAssertEqualObjects(snapshot1, snapshot2);
  49. [snapshot1 capture];
  50. [snapshot2 capture];
  51. XCTAssertEqualObjects(snapshot1, snapshot2);
  52. SEL selector = @selector(description);
  53. Method description = class_getInstanceMethod([GULRuntimeSnapshotTestsTestClass class], selector);
  54. IMP newDescriptionIMP = imp_implementationWithBlock(^(id _self) {
  55. return @"swizzled description";
  56. });
  57. IMP originalDescriptionIMP = method_getImplementation(description);
  58. IMP probableOriginalDescriptionIMP = method_setImplementation(description, newDescriptionIMP);
  59. XCTAssertEqual(probableOriginalDescriptionIMP, originalDescriptionIMP);
  60. [snapshot1 capture];
  61. [snapshot2 capture];
  62. XCTAssertEqualObjects(snapshot1, snapshot2);
  63. method_setImplementation(description, originalDescriptionIMP);
  64. [snapshot2 capture];
  65. XCTAssertNotEqualObjects(snapshot2, snapshot1);
  66. [snapshot1 capture];
  67. XCTAssertEqualObjects(snapshot1, snapshot2);
  68. }
  69. /** Tests capturing snapshots doesn't throw. */
  70. - (void)testCapture {
  71. GULRuntimeSnapshot *snapshot1 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  72. XCTAssertNoThrow([snapshot1 capture]);
  73. GULRuntimeSnapshot *snapshot2 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  74. XCTAssertNoThrow([snapshot2 capture]);
  75. }
  76. /** Tests detecting a new class works. */
  77. - (void)testNewClassDetected {
  78. GULRuntimeSnapshot *snapshot1 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  79. [snapshot1 capture];
  80. Class newClass = objc_allocateClassPair([NSObject class], "GULNewClass", 0);
  81. objc_registerClassPair(newClass);
  82. GULRuntimeSnapshot *snapshot2 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  83. [snapshot2 capture];
  84. GULRuntimeDiff *diff = [snapshot1 diff:snapshot2];
  85. XCTAssertGreaterThan(diff.addedClasses.count, 0);
  86. BOOL found = NO;
  87. for (NSString *class in diff.addedClasses) {
  88. if ([class isEqualToString:@"GULNewClass"]) {
  89. found = YES;
  90. break;
  91. }
  92. }
  93. XCTAssertTrue(found);
  94. }
  95. /** Tests detecting a class deletion works. */
  96. - (void)testClassRemovedDetected {
  97. Class newClass = objc_allocateClassPair([NSObject class], "GULNewClass2", 0);
  98. objc_registerClassPair(newClass);
  99. GULRuntimeSnapshot *snapshot1 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  100. [snapshot1 capture];
  101. objc_disposeClassPair(NSClassFromString(@"GULNewClass2"));
  102. GULRuntimeSnapshot *snapshot2 = [[GULRuntimeSnapshot alloc] initWithClasses:nil];
  103. [snapshot2 capture];
  104. GULRuntimeDiff *diff = [snapshot1 diff:snapshot2];
  105. XCTAssertGreaterThan(diff.removedClasses.count, 0);
  106. BOOL found = NO;
  107. for (NSString *class in diff.removedClasses) {
  108. if ([class isEqualToString:@"GULNewClass2"]) {
  109. found = YES;
  110. break;
  111. }
  112. }
  113. XCTAssertTrue(found);
  114. }
  115. @end