GULRuntimeClassDiff.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "GULRuntimeClassDiff.h"
  15. /** Computes the equality of possibly nil or empty NSSets.
  16. *
  17. * @param firstSet The first set of strings.
  18. * @param secondSet The second set of strings.
  19. * @return YES if both sets are zero length or nil, or the result of `isEqualToSet:`.
  20. */
  21. FOUNDATION_STATIC_INLINE
  22. BOOL IsEqual(NSSet *firstSet, NSSet *secondSet) {
  23. return ((!firstSet || firstSet.count == 0) && (!secondSet || secondSet.count == 0)) ||
  24. [firstSet isEqualToSet:secondSet];
  25. }
  26. @implementation GULRuntimeClassDiff
  27. - (NSUInteger)hash {
  28. return [_aClass hash] ^ [_addedClassProperties hash] ^ [_addedInstanceProperties hash] ^
  29. [_addedClassSelectors hash] ^ [_addedInstanceSelectors hash] ^ [_modifiedImps hash];
  30. }
  31. - (BOOL)isEqual:(id)object {
  32. GULRuntimeClassDiff *otherObject = (GULRuntimeClassDiff *)object;
  33. return _aClass == otherObject->_aClass &&
  34. IsEqual(_addedClassProperties, otherObject->_addedClassProperties) &&
  35. IsEqual(_addedInstanceProperties, otherObject->_addedInstanceProperties) &&
  36. IsEqual(_addedClassSelectors, otherObject->_addedClassSelectors) &&
  37. IsEqual(_addedInstanceSelectors, otherObject->_addedInstanceSelectors) &&
  38. IsEqual(_modifiedImps, otherObject->_modifiedImps);
  39. }
  40. - (NSString *)description {
  41. NSMutableString *description = [[NSMutableString alloc] init];
  42. [description appendFormat:@"%@:\n", NSStringFromClass(self.aClass)];
  43. if (_addedClassProperties.count) {
  44. [description appendString:@"\tAdded class properties:\n"];
  45. for (NSString *addedClassProperty in _addedClassProperties) {
  46. [description appendFormat:@"\t\t%@\n", addedClassProperty];
  47. }
  48. }
  49. if (_addedInstanceProperties.count) {
  50. [description appendString:@"\tAdded instance properties:\n"];
  51. for (NSString *addedInstanceProperty in _addedInstanceProperties) {
  52. [description appendFormat:@"\t\t%@\n", addedInstanceProperty];
  53. }
  54. }
  55. if (_addedClassSelectors.count) {
  56. [description appendString:@"\tAdded class selectors:\n"];
  57. for (NSString *addedClassSelector in _addedClassSelectors) {
  58. [description appendFormat:@"\t\t%@\n", addedClassSelector];
  59. }
  60. }
  61. if (_addedInstanceSelectors.count) {
  62. [description appendString:@"\tAdded instance selectors:\n"];
  63. for (NSString *addedInstanceSelector in _addedInstanceSelectors) {
  64. [description appendFormat:@"\t\t%@\n", addedInstanceSelector];
  65. }
  66. }
  67. if (_modifiedImps.count) {
  68. [description appendString:@"\tModified IMPs:\n"];
  69. for (NSString *modifiedImp in _modifiedImps) {
  70. [description appendFormat:@"\t\t%@\n", modifiedImp];
  71. }
  72. }
  73. return description;
  74. }
  75. @end