FLEXTestsMethodsList.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // FLEXTestMethodList.m
  3. // FLEXTestMethodList
  4. //
  5. // Created by Tigran Yesayan on 7/6/17.
  6. // Copyright © 2017 Flipboard. All rights reserved.
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXManager.h"
  11. #import "FLEXWindow.h"
  12. #import "FLEXMultiColumnTableView.h"
  13. @interface FLEXRuntimeUtility (Testing)
  14. + (NSString *)readableTypeForEncoding:(NSString *)encodingString;
  15. @end
  16. @interface FLEXTestMethodList : XCTestCase
  17. @end
  18. @implementation FLEXTestMethodList
  19. - (void)setUp {
  20. [super setUp];
  21. // Put setup code here. This method is called before the invocation of each test method in the class.
  22. }
  23. - (void)tearDown {
  24. // Put teardown code here. This method is called after the invocation of each test method in the class.
  25. [super tearDown];
  26. }
  27. - (void)testExample {
  28. // This is an example of a functional test case.
  29. // Use XCTAssert and related functions to verify your tests produce the correct results.
  30. [self testMethodListForClass:[NSObject class]];
  31. [self testMethodListForClass:[NSArray class]];
  32. [self testMethodListForClass:[UIApplication class]];
  33. [self testMethodListForClass:[UIView class]];
  34. [self testMethodListForClass:[NSThread class]];
  35. [self testMethodListForClass:[CALayer class]];
  36. [self testMethodListForClass:[NSDictionary class]];
  37. [self testMethodListForClass:[NSProxy class]];
  38. [self testMethodListForClass:[NSData class]];
  39. [self testMethodListForClass:[FLEXManager class]];
  40. [self testMethodListForClass:[FLEXWindow class]];
  41. [self testMethodListForClass:[FLEXMultiColumnTableView class]];
  42. [self testMethodListForClass:[NSString class]];
  43. [self testMethodListForClass:[NSSet class]];
  44. [self testMethodListForClass:[NSUndoManager class]];
  45. [self testMethodListForClass:[NSMutableArray class]];
  46. [self testMethodListForClass:[NSMutableDictionary class]];
  47. [self testMethodListForClass:[NSException class]];
  48. [self testMethodListForClass:[UIImage class]];
  49. [self testMethodListForClass:[UIViewController class]];
  50. [self testMethodListForClass:[UIScreen class]];
  51. [self testMethodListForClass:[UIResponder class]];
  52. [self testMethodListForClass:[NSNumber class]];
  53. [self testMethodListForClass:[NSValue class]];
  54. [self testMethodListForClass:[NSError class]];
  55. [self testMethodListForClass:[NSNotificationCenter class]];
  56. [self testMethodListForClass:[NSUserActivity class]];
  57. [self testMethodListForClass:[NSUserDefaults class]];
  58. [self testMethodListForClass:[NSExpression class]];
  59. [self testMethodListForClass:[NSBundle class]];
  60. }
  61. - (void)testMethodListForClass:(Class)class {
  62. NSLog(@"class: %@", NSStringFromClass(class));
  63. unsigned int methodCount = 0;
  64. Method *methods = class_copyMethodList(class, &methodCount);
  65. for (unsigned int i = 0; i < methodCount; ++i) {
  66. Method method = methods[i];
  67. NSString *selectorName = NSStringFromSelector(method_getName(method));
  68. NSArray *prevWay = [self prettyArgumentComponentsForMethod:method];
  69. if (![prevWay count]) {
  70. prevWay = @[ selectorName ];
  71. }
  72. NSArray *newWay = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:method];
  73. XCTAssert([newWay isEqual:prevWay]);
  74. }
  75. free(methods);
  76. }
  77. - (void)testPerformanceExample {
  78. // This is an example of a performance test case.
  79. [self measureBlock:^{
  80. // Put the code you want to measure the time of here.
  81. }];
  82. }
  83. #pragma mark - Method to test with
  84. - (NSArray *)prettyArgumentComponentsForMethod:(Method)method {
  85. NSMutableArray *components = [NSMutableArray array];
  86. NSString *selectorName = NSStringFromSelector(method_getName(method));
  87. NSArray *selectorComponents = [selectorName componentsSeparatedByString:@":"];
  88. unsigned int numberOfArguments = method_getNumberOfArguments(method);
  89. for (unsigned int argIndex = kFLEXNumberOfImplicitArgs; argIndex < numberOfArguments; argIndex++) {
  90. char *argType = method_copyArgumentType(method, argIndex);
  91. NSString *readableArgType = [FLEXRuntimeUtility readableTypeForEncoding:@(argType)];
  92. free(argType);
  93. NSString *prettyComponent = [NSString stringWithFormat:@"%@:(%@) ", [selectorComponents objectAtIndex:argIndex - kFLEXNumberOfImplicitArgs], readableArgType];
  94. [components addObject:prettyComponent];
  95. }
  96. return components;
  97. }
  98. @end