FIRTestsAssertionHandler.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseMessaging/Tests/UnitTests/FIRTestsAssertionHandler.h"
  17. @interface FIRTestsAssertionHandler ()
  18. @property(nonatomic)
  19. NSMutableDictionary<NSString *, FIRTestsAssertionHandlerBlock> *methodFailureHandlers;
  20. @end
  21. @implementation FIRTestsAssertionHandler
  22. - (instancetype)init {
  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
  42. object:object
  43. file:fileName
  44. lineNumber:line
  45. description:@"%@", format];
  46. }
  47. }
  48. - (void)setMethodFailureHandlerForClass:(Class)aClass
  49. handler:(FIRTestsAssertionHandlerBlock)handler {
  50. @synchronized(self) {
  51. self.methodFailureHandlers[NSStringFromClass(aClass)] = [handler copy];
  52. }
  53. }
  54. @end