OCMMacroState.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2014-2015 Erik Doernenburg and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use these files except in compliance with the License. You may obtain
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. #import "OCMMacroState.h"
  17. #import "OCMStubRecorder.h"
  18. #import "OCMockObject.h"
  19. #import "OCMExpectationRecorder.h"
  20. #import "OCMVerifier.h"
  21. #import "OCMInvocationMatcher.h"
  22. @implementation OCMMacroState
  23. static OCMMacroState *globalState;
  24. #pragma mark Methods to begin/end macros
  25. + (void)beginStubMacro
  26. {
  27. OCMStubRecorder *recorder = [[[OCMStubRecorder alloc] init] autorelease];
  28. globalState = [[[OCMMacroState alloc] initWithRecorder:recorder] autorelease];
  29. }
  30. + (OCMStubRecorder *)endStubMacro
  31. {
  32. OCMStubRecorder *recorder = (OCMStubRecorder *)[globalState recorder];
  33. globalState = nil;
  34. return recorder;
  35. }
  36. + (void)beginExpectMacro
  37. {
  38. OCMExpectationRecorder *recorder = [[[OCMExpectationRecorder alloc] init] autorelease];
  39. globalState = [[[OCMMacroState alloc] initWithRecorder:recorder] autorelease];
  40. }
  41. + (OCMStubRecorder *)endExpectMacro
  42. {
  43. return [self endStubMacro];
  44. }
  45. + (void)beginVerifyMacroAtLocation:(OCMLocation *)aLocation
  46. {
  47. OCMVerifier *recorder = [[[OCMVerifier alloc] init] autorelease];
  48. [recorder setLocation:aLocation];
  49. globalState = [[[OCMMacroState alloc] initWithRecorder:recorder] autorelease];
  50. }
  51. + (void)endVerifyMacro
  52. {
  53. globalState = nil;
  54. }
  55. #pragma mark Accessing global state
  56. + (OCMMacroState *)globalState
  57. {
  58. return globalState;
  59. }
  60. #pragma mark Init, dealloc, accessors
  61. - (id)initWithRecorder:(OCMRecorder *)aRecorder
  62. {
  63. if ((self = [super init]))
  64. {
  65. recorder = [aRecorder retain];
  66. }
  67. return self;
  68. }
  69. - (void)dealloc
  70. {
  71. [recorder release];
  72. if(globalState == self)
  73. globalState = nil;
  74. [super dealloc];
  75. }
  76. - (OCMRecorder *)recorder
  77. {
  78. return recorder;
  79. }
  80. #pragma mark Changing the recorder
  81. - (void)switchToClassMethod
  82. {
  83. [recorder classMethod];
  84. }
  85. @end