OCMConstraint.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2007-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 <OCMock/OCMConstraint.h>
  17. @implementation OCMConstraint
  18. + (instancetype)constraint
  19. {
  20. return [[[self alloc] init] autorelease];
  21. }
  22. - (BOOL)evaluate:(id)value
  23. {
  24. return NO;
  25. }
  26. - (id)copyWithZone:(struct _NSZone *)zone
  27. {
  28. return [self retain];
  29. }
  30. + (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject
  31. {
  32. OCMInvocationConstraint *constraint = [OCMInvocationConstraint constraint];
  33. NSMethodSignature *signature = [anObject methodSignatureForSelector:aSelector];
  34. if(signature == nil)
  35. [NSException raise:NSInvalidArgumentException format:@"Unkown selector %@ used in constraint.", NSStringFromSelector(aSelector)];
  36. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  37. [invocation setTarget:anObject];
  38. [invocation setSelector:aSelector];
  39. constraint->invocation = invocation;
  40. return constraint;
  41. }
  42. + (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue
  43. {
  44. OCMInvocationConstraint *constraint = [self constraintWithSelector:aSelector onObject:anObject];
  45. if([[constraint->invocation methodSignature] numberOfArguments] < 4)
  46. [NSException raise:NSInvalidArgumentException format:@"Constraint with value requires selector with two arguments."];
  47. [constraint->invocation setArgument:&aValue atIndex:3];
  48. return constraint;
  49. }
  50. @end
  51. #pragma mark -
  52. @implementation OCMAnyConstraint
  53. - (BOOL)evaluate:(id)value
  54. {
  55. return YES;
  56. }
  57. @end
  58. #pragma mark -
  59. @implementation OCMIsNilConstraint
  60. - (BOOL)evaluate:(id)value
  61. {
  62. return value == nil;
  63. }
  64. @end
  65. #pragma mark -
  66. @implementation OCMIsNotNilConstraint
  67. - (BOOL)evaluate:(id)value
  68. {
  69. return value != nil;
  70. }
  71. @end
  72. #pragma mark -
  73. @implementation OCMIsNotEqualConstraint
  74. - (BOOL)evaluate:(id)value
  75. {
  76. return ![value isEqual:testValue];
  77. }
  78. @end
  79. #pragma mark -
  80. @implementation OCMInvocationConstraint
  81. - (BOOL)evaluate:(id)value
  82. {
  83. [invocation setArgument:&value atIndex:2]; // should test if constraint takes arg
  84. [invocation invoke];
  85. BOOL returnValue;
  86. [invocation getReturnValue:&returnValue];
  87. return returnValue;
  88. }
  89. @end
  90. #pragma mark -
  91. @implementation OCMBlockConstraint
  92. - (instancetype)initWithConstraintBlock:(BOOL (^)(id))aBlock
  93. {
  94. if ((self = [super init]))
  95. {
  96. block = [aBlock copy];
  97. }
  98. return self;
  99. }
  100. - (void)dealloc {
  101. [block release];
  102. [super dealloc];
  103. }
  104. - (BOOL)evaluate:(id)value
  105. {
  106. return block ? block(value) : NO;
  107. }
  108. @end