PredicateUtilTests.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "RLMTestCase.h"
  19. #import "RLMPredicateUtil.hpp"
  20. @interface PredicateUtilTests : RLMTestCase
  21. @end
  22. @implementation PredicateUtilTests
  23. - (void)testVisitingAllExpressionTypes {
  24. auto testPredicate = [&](NSPredicate *predicate, size_t expectedExpressionCount) {
  25. size_t visitCount = 0;
  26. auto visitExpression = [&](NSExpression *expression) {
  27. visitCount++;
  28. return expression;
  29. };
  30. NSPredicate *transformedPredicate = transformPredicate(predicate, visitExpression);
  31. XCTAssertEqualObjects(predicate, transformedPredicate);
  32. XCTAssertEqual(visitCount, expectedExpressionCount);
  33. };
  34. auto testPredicateString = [=](NSString *predicateString, size_t expectedExpressionCount) {
  35. return testPredicate([NSPredicate predicateWithFormat:predicateString], expectedExpressionCount);
  36. };
  37. testPredicateString(@"TRUEPREDICATE", 0);
  38. testPredicateString(@"A == B", 2);
  39. testPredicateString(@"A == B AND C == 1", 4);
  40. testPredicateString(@"A.@count == 2", 2);
  41. testPredicateString(@"SUBQUERY(collection, $variable, $variable.property == 1).@count > 2", 9);
  42. testPredicateString(@"A IN {1, 2, 3}", 5);
  43. testPredicateString(@"A IN B UNION C", 4);
  44. testPredicateString(@"A IN B INTERSECT C", 4);
  45. testPredicateString(@"A IN B MINUS C", 4);
  46. if ([NSExpression respondsToSelector:@selector(expressionForConditional:trueExpression:falseExpression:)]) {
  47. // Only test conditional predicates on platforms that support them (i.e. iOS 9 and later).
  48. testPredicateString(@"TERNARY(TRUEPREDICATE, A, B) == 1", 4);
  49. }
  50. testPredicateString(@"ANYKEY == 1", 2);
  51. testPredicateString(@"SELF == 1", 2);
  52. testPredicate([NSPredicate predicateWithBlock:^(id, NSDictionary*) { return NO; }], 0);
  53. auto block = ^(id, NSArray *, NSMutableDictionary *) {
  54. return @"Hello";
  55. };
  56. testPredicate([NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForBlock:block arguments:nil]
  57. rightExpression:[NSExpression expressionForConstantValue:@"hello"]
  58. modifier:NSDirectPredicateModifier
  59. type:NSEqualToPredicateOperatorType
  60. options:0], 2);
  61. }
  62. @end