FLEXTestsMethodsList.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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)testExample {
  20. NSArray<Class> *classesToTest = @[
  21. [NSObject class],
  22. [NSArray class],
  23. [UIApplication class],
  24. [UIView class],
  25. [NSThread class],
  26. [CALayer class],
  27. [NSDictionary class],
  28. [NSProxy class],
  29. [NSData class],
  30. [FLEXManager class],
  31. [FLEXWindow class],
  32. [FLEXMultiColumnTableView class],
  33. [NSString class],
  34. [NSSet class],
  35. [NSUndoManager class],
  36. [NSMutableArray class],
  37. [NSMutableDictionary class],
  38. [NSException class],
  39. [UIImage class],
  40. [UIViewController class],
  41. [UIScreen class],
  42. [UIResponder class],
  43. [NSNumber class],
  44. [NSValue class],
  45. [NSError class],
  46. [NSNotificationCenter class],
  47. [NSUserActivity class],
  48. [NSUserDefaults class],
  49. [NSExpression class],
  50. [NSBundle class]
  51. ];
  52. for (Class cls in classesToTest) {
  53. [self testMethodListForClass:cls];
  54. }
  55. }
  56. - (void)testMethodListForClass:(Class)class {
  57. NSLog(@"class: %@", NSStringFromClass(class));
  58. unsigned int methodCount = 0;
  59. Method *methods = class_copyMethodList(class, &methodCount);
  60. for (unsigned int i = 0; i < methodCount; ++i) {
  61. Method method = methods[i];
  62. NSArray *prevWay = [self prettyArgumentComponentsForMethod:method];
  63. NSArray *newWay = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:method];
  64. XCTAssertEqualObjects(prevWay, newWay);
  65. }
  66. free(methods);
  67. }
  68. #pragma mark - Method to test with
  69. - (NSArray *)prettyArgumentComponentsForMethod:(Method)method {
  70. NSMutableArray *components = [NSMutableArray new];
  71. NSString *selectorName = NSStringFromSelector(method_getName(method));
  72. NSArray *selectorComponents = [selectorName componentsSeparatedByString:@":"];
  73. unsigned int numberOfArguments = method_getNumberOfArguments(method);
  74. for (unsigned int argIndex = kFLEXNumberOfImplicitArgs; argIndex < numberOfArguments; argIndex++) {
  75. char *argType = method_copyArgumentType(method, argIndex);
  76. NSString *readableArgType = [FLEXRuntimeUtility readableTypeForEncoding:@(argType)];
  77. free(argType);
  78. NSString *prettyComponent = [NSString stringWithFormat:@"%@:(%@) ", [selectorComponents objectAtIndex:argIndex - kFLEXNumberOfImplicitArgs], readableArgType];
  79. [components addObject:prettyComponent];
  80. }
  81. return components;
  82. }
  83. @end