EnumeratorTests.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 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. @interface EnumeratorTests : RLMTestCase
  20. @end
  21. @implementation EnumeratorTests
  22. - (void)testEnum
  23. {
  24. RLMRealm *realm = [RLMRealm defaultRealm];
  25. RLMResults *emptyPeople = [EmployeeObject allObjects];
  26. // Enum for zero rows added
  27. for (EmployeeObject *row in emptyPeople) {
  28. XCTFail(@"No objects should have been added %@", row);
  29. }
  30. NSArray *rowsArray = @[@{@"name": @"John", @"age": @20, @"hired": @YES},
  31. @{@"name": @"Mary", @"age": @21, @"hired": @NO},
  32. @{@"name": @"Lars", @"age": @21, @"hired": @YES},
  33. @{@"name": @"Phil", @"age": @43, @"hired": @NO},
  34. @{@"name": @"Anni", @"age": @54, @"hired": @YES}];
  35. // Add objects
  36. [realm beginWriteTransaction];
  37. for (NSArray *rowArray in rowsArray) {
  38. [EmployeeObject createInRealm:realm withValue:rowArray];
  39. }
  40. [realm commitWriteTransaction];
  41. // Get all objects
  42. RLMResults *people = [EmployeeObject allObjects];
  43. // Iterate using for...in
  44. NSUInteger index = 0;
  45. for (EmployeeObject *row in people) {
  46. XCTAssertEqualObjects(row.name, rowsArray[index][@"name"], @"Name in iteration should be equal to what was set.");
  47. XCTAssertEqualObjects(@(row.age), rowsArray[index][@"age"], @"Age in iteration should be equal to what was set.");
  48. XCTAssertEqualObjects(@(row.hired), rowsArray[index][@"hired"], @"Hired in iteration should be equal to what was set.");
  49. index++;
  50. }
  51. NSPredicate *pred = [NSPredicate predicateWithFormat:@"hired = YES && age BETWEEN {20, 30}"];
  52. NSArray *filteredArray = [rowsArray filteredArrayUsingPredicate:pred];
  53. // Do a query, and get all matches as RLMResults
  54. RLMResults *res = [EmployeeObject objectsWithPredicate:pred];
  55. // Iterate over the resulting RLMResults
  56. index = 0;
  57. for (EmployeeObject *row in res) {
  58. XCTAssertEqualObjects(row.name, filteredArray[index][@"name"], @"Name in iteration should be equal to what was set.");
  59. XCTAssertEqualObjects(@(row.age), filteredArray[index][@"age"], @"Age in iteration should be equal to what was set.");
  60. XCTAssertEqualObjects(@(row.hired), filteredArray[index][@"hired"], @"Hired in iteration should be equal to what was set.");
  61. index++;
  62. }
  63. }
  64. @end