OCMArg.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2009-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 <objc/runtime.h>
  17. #import <OCMock/OCMArg.h>
  18. #import <OCMock/OCMConstraint.h>
  19. #import "OCMPassByRefSetter.h"
  20. @implementation OCMArg
  21. + (id)any
  22. {
  23. return [OCMAnyConstraint constraint];
  24. }
  25. + (void *)anyPointer
  26. {
  27. return (void *)0x01234567;
  28. }
  29. + (id __autoreleasing *)anyObjectRef
  30. {
  31. return (id *)[self anyPointer];
  32. }
  33. + (SEL)anySelector
  34. {
  35. return NSSelectorFromString(@"aSelectorThatMatchesAnySelector");
  36. }
  37. + (id)isNil
  38. {
  39. return [OCMIsNilConstraint constraint];
  40. }
  41. + (id)isNotNil
  42. {
  43. return [OCMIsNotNilConstraint constraint];
  44. }
  45. + (id)isEqual:(id)value
  46. {
  47. return value;
  48. }
  49. + (id)isNotEqual:(id)value
  50. {
  51. OCMIsNotEqualConstraint *constraint = [OCMIsNotEqualConstraint constraint];
  52. constraint->testValue = value;
  53. return constraint;
  54. }
  55. + (id)isKindOfClass:(Class)cls
  56. {
  57. return [[[OCMBlockConstraint alloc] initWithConstraintBlock:^BOOL(id obj) {
  58. return [obj isKindOfClass:cls];
  59. }] autorelease];
  60. }
  61. + (id)checkWithSelector:(SEL)selector onObject:(id)anObject
  62. {
  63. return [OCMConstraint constraintWithSelector:selector onObject:anObject];
  64. }
  65. + (id)checkWithBlock:(BOOL (^)(id))block
  66. {
  67. return [[[OCMBlockConstraint alloc] initWithConstraintBlock:block] autorelease];
  68. }
  69. + (id *)setTo:(id)value
  70. {
  71. return (id *)[[[OCMPassByRefSetter alloc] initWithValue:value] autorelease];
  72. }
  73. + (void *)setToValue:(NSValue *)value
  74. {
  75. return (id *)[[[OCMPassByRefSetter alloc] initWithValue:value] autorelease];
  76. }
  77. + (id)resolveSpecialValues:(NSValue *)value
  78. {
  79. const char *type = [value objCType];
  80. if(type[0] == '^')
  81. {
  82. void *pointer = [value pointerValue];
  83. if(pointer == [self anyPointer])
  84. return [OCMArg any];
  85. if((pointer != NULL) && [OCMPassByRefSetter ptrIsPassByRefSetter:pointer])
  86. return (id)pointer;
  87. }
  88. else if(type[0] == ':')
  89. {
  90. SEL selector;
  91. [value getValue:&selector];
  92. if(selector == NSSelectorFromString(@"aSelectorThatMatchesAnySelector"))
  93. return [OCMArg any];
  94. }
  95. return value;
  96. }
  97. @end