OCMStubRecorder+FIRAuthUnitTests.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2017 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 "FirebaseAuth/Sources/Auth/FIRAuthGlobalWorkQueue.h"
  17. #import "FirebaseAuth/Tests/Unit/OCMStubRecorder+FIRAuthUnitTests.h"
  18. /** @fn argumentOf
  19. @brief Retrieves a specific argument from a method invocation.
  20. @param invocation The Objective-C method invocation.
  21. @param position The position of the argument to retrieve, starting from 0.
  22. @return The argument at the given position that the method has been invoked with.
  23. @remarks The argument type must be compatible with @c id .
  24. */
  25. static id argumentOf(NSInvocation *invocation, int position) {
  26. __unsafe_unretained id unretainedArgument;
  27. // Indices 0 and 1 indicate the hidden arguments self and _cmd. Actual arguments starts from 2.
  28. [invocation getArgument:&unretainedArgument atIndex:position + 2];
  29. // The argument needs to be retained, or it will be released along with the invocation object.
  30. id argument = unretainedArgument;
  31. return argument;
  32. }
  33. /** @fn doubleArgumentOf
  34. @brief Retrieves a specific argument of type 'double' from a method invocation.
  35. @param invocation The Objective-C method invocation.
  36. @param position The position of the argument to retrieve, starting from 0.
  37. @return The argument at the given position that the method has been invoked with.
  38. @remarks The argument type must be @c double .
  39. */
  40. static double doubleArgumentOf(NSInvocation *invocation, int position) {
  41. double argument;
  42. // Indices 0 and 1 indicate the hidden arguments self and _cmd. Actual arguments starts from 2.
  43. [invocation getArgument:&argument atIndex:position + 2];
  44. return argument;
  45. }
  46. @implementation OCMStubRecorder (FIRAuthUnitTests)
  47. - (id)andCallBlock1:(FIRAuthGeneralBlock1)block1 {
  48. return [self andDo:^(NSInvocation *invocation) {
  49. block1(argumentOf(invocation, 0));
  50. }];
  51. }
  52. - (id)andCallBlock2:(FIRAuthGeneralBlock2)block2 {
  53. return [self andDo:^(NSInvocation *invocation) {
  54. block2(argumentOf(invocation, 0), argumentOf(invocation, 1));
  55. }];
  56. }
  57. - (id)andDispatchError2:(NSError *)error {
  58. return [self andCallBlock2:^(id request, FIRAuthGeneralBlock2 callback) {
  59. dispatch_async(FIRAuthGlobalWorkQueue(), ^() {
  60. callback(nil, error);
  61. });
  62. }];
  63. }
  64. - (id)andCallIdDoubleIdBlock:(FIRAuthIdDoubleIdBlock)block {
  65. return [self andDo:^(NSInvocation *invocation) {
  66. block(argumentOf(invocation, 0), doubleArgumentOf(invocation, 2), argumentOf(invocation, 2));
  67. }];
  68. }
  69. - (OCMStubRecorder * (^)(FIRAuthGeneralBlock1))_andCallBlock1 {
  70. return ^(FIRAuthGeneralBlock1 block1) {
  71. return [self andCallBlock1:block1];
  72. };
  73. }
  74. - (OCMStubRecorder * (^)(FIRAuthGeneralBlock2))_andCallBlock2 {
  75. return ^(FIRAuthGeneralBlock2 block2) {
  76. return [self andCallBlock2:block2];
  77. };
  78. }
  79. - (OCMStubRecorder * (^)(NSError *))_andDispatchError2 {
  80. return ^(NSError *error) {
  81. return [self andDispatchError2:error];
  82. };
  83. }
  84. - (OCMStubRecorder * (^)(FIRAuthIdDoubleIdBlock))_andCallIdDoubleIdBlock {
  85. return ^(FIRAuthIdDoubleIdBlock block) {
  86. return [self andCallIdDoubleIdBlock:block];
  87. };
  88. }
  89. @end