FIRTestsAssertionHandler.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2020 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRTestsAssertionHandler.h"
  17. @interface FIRTestsAssertionHandler ()
  18. @property(nonatomic) NSMutableDictionary<NSString *, FIRTestsAssertionHandlerBlock> *methodFailureHandlers;
  19. @end
  20. @implementation FIRTestsAssertionHandler
  21. - (instancetype)init
  22. {
  23. self = [super init];
  24. if (self) {
  25. _methodFailureHandlers = [NSMutableDictionary dictionary];
  26. }
  27. return self;
  28. }
  29. - (void)handleFailureInMethod:(SEL)selector
  30. object:(id<NSObject>)object
  31. file:(NSString *)fileName
  32. lineNumber:(NSInteger)line
  33. description:(NSString *)format, ... {
  34. FIRTestsAssertionHandlerBlock handler;
  35. @synchronized (self) {
  36. handler = self.methodFailureHandlers[NSStringFromClass([object class])];
  37. }
  38. if (handler) {
  39. handler(object, fileName, line);
  40. } else {
  41. [super handleFailureInMethod:selector object:object file:fileName lineNumber:line description:@"%@", format];
  42. }
  43. }
  44. - (void)setMethodFailureHandlerForClass:(Class)aClass handler:(FIRTestsAssertionHandlerBlock)handler {
  45. @synchronized (self) {
  46. self.methodFailureHandlers[NSStringFromClass(aClass)] = [handler copy];
  47. }
  48. }
  49. @end