YYTestCopyingAndCoding.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // YYTestCopyingAndCoding.m
  3. // YYModel <https://github.com/ibireme/YYModel>
  4. //
  5. // Created by ibireme on 15/11/29.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <XCTest/XCTest.h>
  12. #import "YYModel.h"
  13. typedef struct my_struct {
  14. int a;
  15. double b;
  16. long double c;
  17. } my_struct;
  18. @interface YYTestModelHashModel : NSObject <NSCopying, NSCoding>
  19. @property bool boolValue;
  20. @property BOOL BOOLValue;
  21. @property char charValue;
  22. @property unsigned char unsignedCharValue;
  23. @property short shortValue;
  24. @property unsigned short unsignedShortValue;
  25. @property int intValue;
  26. @property unsigned int unsignedIntValue;
  27. @property long longValue;
  28. @property unsigned long unsignedLongValue;
  29. @property long long longLongValue;
  30. @property unsigned long long unsignedLongLongValue;
  31. @property float floatValue;
  32. @property double doubleValue;
  33. @property long double longDoubleValue;
  34. @property (strong) Class classValue;
  35. @property SEL selectorValue;
  36. @property (copy) void (^blockValue)();
  37. @property void *pointerValue;
  38. @property CFArrayRef cfArrayValue;
  39. @property NSValue *valueValue;
  40. @property CGSize sizeValue;
  41. @property CGPoint pointValue;
  42. @property CGRect rectValue;
  43. @property CGAffineTransform transformValue;
  44. @property UIEdgeInsets insetsValue;
  45. @property UIOffset offsetValue;
  46. @property CATransform3D transform3DValue; // invalid for NSKeyedArchiver/Unarchiver
  47. @property my_struct myStructValue; // invalid for NSKeyedArchiver/Unarchiver
  48. @property (nonatomic, strong) NSObject *object;
  49. @property (nonatomic, strong) NSNumber *number;
  50. @property (nonatomic, strong) NSDecimalNumber *decimal;
  51. @property (nonatomic, strong) NSString *string;
  52. @property (nonatomic, strong) NSMutableString *mString;
  53. @property (nonatomic, strong) NSData *data;
  54. @property (nonatomic, strong) NSMutableData *mData;
  55. @property (nonatomic, strong) NSDate *date;
  56. @property (nonatomic, strong) NSValue *value;
  57. @property (nonatomic, strong) NSURL *url;
  58. @property (nonatomic, strong) NSArray *array;
  59. @property (nonatomic, strong) NSMutableArray *mArray;
  60. @property (nonatomic, strong) NSDictionary *dict;
  61. @property (nonatomic, strong) NSMutableDictionary *mDict;
  62. @property (nonatomic, strong) NSSet *set;
  63. @property (nonatomic, strong) NSMutableSet *mSet;
  64. @end
  65. @implementation YYTestModelHashModel
  66. - (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
  67. - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
  68. - (id)copyWithZone:(NSZone *)zone { return [self yy_modelCopy]; }
  69. - (NSUInteger)hash { return [self yy_modelHash]; }
  70. - (BOOL)isEqual:(id)object { return [self yy_modelIsEqual:object]; }
  71. @end
  72. @interface YYTestEqualAndHash : XCTestCase
  73. @end
  74. @implementation YYTestEqualAndHash
  75. - (void)testHash {
  76. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  77. YYTestModelHashModel *model2 = [YYTestModelHashModel new];
  78. XCTAssertTrue([model1 isEqual:model2]);
  79. model1.intValue = 1;
  80. XCTAssertFalse([model1 isEqual:model2]);
  81. model2.intValue = 1;
  82. XCTAssertTrue([model1 isEqual:model2]);
  83. model1.string = @"Apple";
  84. XCTAssertFalse([model1 isEqual:model2]);
  85. model2.string = @"Apple";
  86. XCTAssertTrue([model1 isEqual:model2]);
  87. my_struct my = {0};
  88. my.b = 12.34;
  89. model1.myStructValue = my;
  90. XCTAssertFalse([model1 isEqual:model2]);
  91. model2.myStructValue = my;
  92. XCTAssertTrue([model1 isEqual:model2]);
  93. }
  94. - (void)testCopying {
  95. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  96. YYTestModelHashModel *model2 = nil;
  97. model1.intValue = 1;
  98. model1.floatValue = 12.34;
  99. model1.myStructValue = (my_struct){.b = 56.78};
  100. model1.string = @"Apple";
  101. model2 = model1.copy;
  102. XCTAssertEqual(model1.intValue, model2.intValue);
  103. XCTAssertEqual(model1.floatValue, model2.floatValue);
  104. XCTAssertEqual(model1.myStructValue.b, model2.myStructValue.b);
  105. XCTAssertTrue([model1.string isEqualToString:model2.string]);
  106. }
  107. - (void)testCoding {
  108. NSData *data = nil;
  109. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  110. YYTestModelHashModel *model2 = nil;
  111. model1.intValue = 1;
  112. model1.floatValue = 12.34;
  113. model1.number = @(1234);
  114. model1.string = @"Apple";
  115. model1.sizeValue = CGSizeMake(12, 34);
  116. model1.selectorValue = @selector(stringWithFormat:);
  117. model1.myStructValue = (my_struct){.b = 56.78};
  118. model1.valueValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  119. data = [NSKeyedArchiver archivedDataWithRootObject:model1];
  120. model2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  121. XCTAssertEqual(model1.intValue, model2.intValue);
  122. XCTAssertEqual(model1.floatValue, model2.floatValue);
  123. XCTAssertTrue([model1.number isEqual:model2.number]);
  124. XCTAssertTrue([model1.string isEqualToString:model2.string]);
  125. XCTAssertTrue(CGSizeEqualToSize(model1.sizeValue, model2.sizeValue));
  126. XCTAssertTrue(model2.selectorValue == @selector(stringWithFormat:));
  127. XCTAssertTrue(model2.myStructValue.b == 0); // ignore in NSKeyedArchiver
  128. XCTAssertTrue(model2.valueValue == nil); // ignore in NSKeyedArchiver
  129. // for code coverage
  130. NSArray *array = @[model1, model2];
  131. NSMutableData *mutableData = [NSMutableData new];
  132. NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  133. [array yy_modelEncodeWithCoder:coder];
  134. [coder finishEncoding];
  135. XCTAssertTrue(mutableData.length > 0);
  136. mutableData = [NSMutableData new];
  137. coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  138. [[NSNull null] yy_modelEncodeWithCoder:coder];
  139. [coder finishEncoding];
  140. XCTAssertTrue(mutableData.length > 0);
  141. }
  142. @end