GULRuntimeDiff.m 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "GULRuntimeDiff.h"
  15. #import "GULRuntimeClassDiff.h"
  16. /** Computes the equality of possibly nil or empty NSSets.
  17. *
  18. * @param firstSet The first set of strings.
  19. * @param secondSet The second set of strings.
  20. * @return YES if both sets are zero length or nil, or the result of `isEqualToSet:`.
  21. */
  22. FOUNDATION_STATIC_INLINE
  23. BOOL IsEqual(NSSet *firstSet, NSSet *secondSet) {
  24. return ((!firstSet || firstSet.count == 0) && (!secondSet || secondSet.count == 0)) ||
  25. [firstSet isEqualToSet:secondSet];
  26. }
  27. @implementation GULRuntimeDiff
  28. - (NSUInteger)hash {
  29. return [_addedClasses hash] ^ [_removedClasses hash] ^ [_classDiffs hash];
  30. }
  31. - (BOOL)isEqual:(id)object {
  32. GULRuntimeDiff *otherObject = (GULRuntimeDiff *)object;
  33. return IsEqual(_addedClasses, otherObject->_addedClasses) &&
  34. IsEqual(_removedClasses, otherObject->_removedClasses) &&
  35. IsEqual(_classDiffs, otherObject->_classDiffs);
  36. }
  37. - (NSString *)description {
  38. NSMutableString *description = [[NSMutableString alloc] init];
  39. if (_addedClasses.count) {
  40. [description appendString:@"Added classes:\n"];
  41. for (NSString *classString in _addedClasses) {
  42. [description appendFormat:@"\t%@\n", classString];
  43. }
  44. }
  45. if (_removedClasses.count) {
  46. [description appendString:@"\nRemoved classes:\n"];
  47. for (NSString *classString in _removedClasses) {
  48. [description appendFormat:@"\t%@\n", classString];
  49. }
  50. }
  51. if (_classDiffs.count) {
  52. [description appendString:@"\nClass diffs:\n"];
  53. for (GULRuntimeClassDiff *classDiff in _classDiffs) {
  54. NSString *classDiffDescription =
  55. [[classDiff description] stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
  56. [description appendFormat:@"\t%@\n", classDiffDescription];
  57. }
  58. }
  59. return description;
  60. }
  61. @end