PropertyTests.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 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 "RLMObjectSchema_Private.h"
  20. #import "RLMProperty_Private.h"
  21. #import "RLMRealm_Dynamic.h"
  22. #import <objc/runtime.h>
  23. @interface PropertyTests : RLMTestCase
  24. @end
  25. @implementation PropertyTests
  26. - (void)testDescription {
  27. AllTypesObject *object = [[AllTypesObject alloc] init];
  28. RLMProperty *property = object.objectSchema[@"objectCol"];
  29. XCTAssertEqualObjects(property.description, @"objectCol {\n"
  30. @"\ttype = object;\n"
  31. @"\tobjectClassName = StringObject;\n"
  32. @"\tlinkOriginPropertyName = (null);\n"
  33. @"\tindexed = NO;\n"
  34. @"\tisPrimary = NO;\n"
  35. @"\tarray = NO;\n"
  36. @"\toptional = YES;\n"
  37. @"}");
  38. }
  39. - (void)testEqualityFromObjectSchema {
  40. { // Test non-optional property types
  41. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[AllTypesObject class]];
  42. NSDictionary *expectedProperties = @{
  43. @"boolCol": [[RLMProperty alloc] initWithName:@"boolCol" type:RLMPropertyTypeBool objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  44. @"intCol": [[RLMProperty alloc] initWithName:@"intCol" type:RLMPropertyTypeInt objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  45. @"floatCol": [[RLMProperty alloc] initWithName:@"floatCol" type:RLMPropertyTypeFloat objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  46. @"doubleCol": [[RLMProperty alloc] initWithName:@"doubleCol" type:RLMPropertyTypeDouble objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  47. @"stringCol": [[RLMProperty alloc] initWithName:@"stringCol" type:RLMPropertyTypeString objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  48. @"binaryCol": [[RLMProperty alloc] initWithName:@"binaryCol" type:RLMPropertyTypeData objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  49. @"dateCol": [[RLMProperty alloc] initWithName:@"dateCol" type:RLMPropertyTypeDate objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  50. @"cBoolCol": [[RLMProperty alloc] initWithName:@"cBoolCol" type:RLMPropertyTypeBool objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  51. @"longCol": [[RLMProperty alloc] initWithName:@"longCol" type:RLMPropertyTypeInt objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:NO],
  52. @"objectCol": [[RLMProperty alloc] initWithName:@"objectCol" type:RLMPropertyTypeObject objectClassName:@"StringObject" linkOriginPropertyName:nil indexed:NO optional:YES],
  53. };
  54. XCTAssertEqual(objectSchema.properties.count, expectedProperties.allKeys.count);
  55. for (NSString *propertyName in expectedProperties) {
  56. XCTAssertEqualObjects(objectSchema[propertyName], expectedProperties[propertyName]);
  57. }
  58. }
  59. { // Test optional property types
  60. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[AllOptionalTypes class]];
  61. NSDictionary *expectedProperties = @{
  62. @"intObj": [[RLMProperty alloc] initWithName:@"intObj" type:RLMPropertyTypeInt objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  63. @"floatObj": [[RLMProperty alloc] initWithName:@"floatObj" type:RLMPropertyTypeFloat objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  64. @"doubleObj": [[RLMProperty alloc] initWithName:@"doubleObj" type:RLMPropertyTypeDouble objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  65. @"boolObj": [[RLMProperty alloc] initWithName:@"boolObj" type:RLMPropertyTypeBool objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  66. @"string": [[RLMProperty alloc] initWithName:@"string" type:RLMPropertyTypeString objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  67. @"data": [[RLMProperty alloc] initWithName:@"data" type:RLMPropertyTypeData objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  68. @"date": [[RLMProperty alloc] initWithName:@"date" type:RLMPropertyTypeDate objectClassName:nil linkOriginPropertyName:nil indexed:NO optional:YES],
  69. };
  70. XCTAssertEqual(objectSchema.properties.count, expectedProperties.allKeys.count);
  71. for (NSString *propertyName in expectedProperties) {
  72. XCTAssertEqualObjects(objectSchema[propertyName], expectedProperties[propertyName]);
  73. }
  74. }
  75. { // Test indexed property
  76. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[IndexedStringObject class]];
  77. RLMProperty *stringProperty = objectSchema[@"stringCol"];
  78. RLMProperty *expectedProperty = [[RLMProperty alloc] initWithName:@"stringCol" type:RLMPropertyTypeString objectClassName:nil linkOriginPropertyName:nil indexed:YES optional:YES];
  79. XCTAssertEqualObjects(stringProperty, expectedProperty);
  80. }
  81. { // Test primary key property
  82. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[PrimaryStringObject class]];
  83. RLMProperty *stringProperty = objectSchema[@"stringCol"];
  84. RLMProperty *expectedProperty = [[RLMProperty alloc] initWithName:@"stringCol"
  85. type:RLMPropertyTypeString
  86. objectClassName:nil
  87. linkOriginPropertyName:nil
  88. indexed:YES
  89. optional:NO];
  90. expectedProperty.isPrimary = YES;
  91. XCTAssertEqualObjects(stringProperty, expectedProperty);
  92. }
  93. }
  94. - (void)testTwoPropertiesAreEqual {
  95. const char *name = "intCol";
  96. objc_property_t objcProperty1 = class_getProperty(AllTypesObject.class, name);
  97. RLMProperty *property1 = [[RLMProperty alloc] initWithName:@(name) indexed:YES linkPropertyDescriptor:nil property:objcProperty1];
  98. objc_property_t objcProperty2 = class_getProperty(IntObject.class, name);
  99. RLMProperty *property2 = [[RLMProperty alloc] initWithName:@(name) indexed:YES linkPropertyDescriptor:nil property:objcProperty2];
  100. XCTAssertEqualObjects(property1, property2);
  101. }
  102. - (void)testTwoPropertiesAreUnequal {
  103. const char *name = "stringCol";
  104. objc_property_t objcProperty1 = class_getProperty(AllTypesObject.class, name);
  105. RLMProperty *property1 = [[RLMProperty alloc] initWithName:@(name) indexed:YES linkPropertyDescriptor:nil property:objcProperty1];
  106. name = "intCol";
  107. objc_property_t objcProperty2 = class_getProperty(IntObject.class, name);
  108. RLMProperty *property2 = [[RLMProperty alloc] initWithName:@(name) indexed:YES linkPropertyDescriptor:nil property:objcProperty2];
  109. XCTAssertNotEqualObjects(property1, property2);
  110. }
  111. - (void)testSwiftPropertyNameValidation {
  112. RLMAssertThrows(RLMValidateSwiftPropertyName(@"alloc"));
  113. RLMAssertThrows(RLMValidateSwiftPropertyName(@"_alloc"));
  114. RLMAssertThrows(RLMValidateSwiftPropertyName(@"allocOject"));
  115. RLMAssertThrows(RLMValidateSwiftPropertyName(@"_allocOject"));
  116. RLMAssertThrows(RLMValidateSwiftPropertyName(@"alloc_object"));
  117. RLMAssertThrows(RLMValidateSwiftPropertyName(@"_alloc_object"));
  118. RLMAssertThrows(RLMValidateSwiftPropertyName(@"new"));
  119. RLMAssertThrows(RLMValidateSwiftPropertyName(@"copy"));
  120. RLMAssertThrows(RLMValidateSwiftPropertyName(@"mutableCopy"));
  121. // Swift doesn't infer family from `init`
  122. XCTAssertNoThrow(RLMValidateSwiftPropertyName(@"init"));
  123. XCTAssertNoThrow(RLMValidateSwiftPropertyName(@"_init"));
  124. XCTAssertNoThrow(RLMValidateSwiftPropertyName(@"initWithValue"));
  125. // Lowercase letter after family name
  126. XCTAssertNoThrow(RLMValidateSwiftPropertyName(@"allocate"));
  127. XCTAssertNoThrow(RLMValidateSwiftPropertyName(@"__alloc"));
  128. }
  129. - (void)testTypeToString {
  130. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeString), @"string");
  131. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeInt), @"int");
  132. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeBool), @"bool");
  133. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeDate), @"date");
  134. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeData), @"data");
  135. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeDouble), @"double");
  136. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeFloat), @"float");
  137. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeObject), @"object");
  138. XCTAssertEqualObjects(RLMTypeToString(RLMPropertyTypeLinkingObjects), @"linking objects");
  139. XCTAssertEqualObjects(RLMTypeToString((RLMPropertyType)-1), @"Unknown");
  140. }
  141. @end