YYTestCopyingAndCoding.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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) NSString *string2;
  53. @property (nonatomic, strong) NSMutableString *mString;
  54. @property (nonatomic, strong) NSData *data;
  55. @property (nonatomic, strong) NSMutableData *mData;
  56. @property (nonatomic, strong) NSDate *date;
  57. @property (nonatomic, strong) NSValue *value;
  58. @property (nonatomic, strong) NSURL *url;
  59. @property (nonatomic, strong) NSArray *array;
  60. @property (nonatomic, strong) NSMutableArray *mArray;
  61. @property (nonatomic, strong) NSDictionary *dict;
  62. @property (nonatomic, strong) NSMutableDictionary *mDict;
  63. @property (nonatomic, strong) NSSet *set;
  64. @property (nonatomic, strong) NSMutableSet *mSet;
  65. @end
  66. @implementation YYTestModelHashModel
  67. - (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
  68. - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
  69. - (id)copyWithZone:(NSZone *)zone { return [self yy_modelCopy]; }
  70. - (NSUInteger)hash { return [self yy_modelHash]; }
  71. - (BOOL)isEqual:(id)object { return [self yy_modelIsEqual:object]; }
  72. @end
  73. @interface YYTestEqualAndHash : XCTestCase
  74. @end
  75. @implementation YYTestEqualAndHash
  76. - (void)testHash {
  77. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  78. YYTestModelHashModel *model2 = [YYTestModelHashModel new];
  79. XCTAssertTrue([model1 isEqual:model2]);
  80. model1.intValue = 1;
  81. XCTAssertFalse([model1 isEqual:model2]);
  82. model2.intValue = 1;
  83. XCTAssertTrue([model1 isEqual:model2]);
  84. model1.string = @"Apple";
  85. XCTAssertFalse([model1 isEqual:model2]);
  86. model2.string = @"Apple";
  87. XCTAssertTrue([model1 isEqual:model2]);
  88. my_struct my = {0};
  89. my.b = 12.34;
  90. model1.myStructValue = my;
  91. XCTAssertFalse([model1 isEqual:model2]);
  92. model2.myStructValue = my;
  93. XCTAssertTrue([model1 isEqual:model2]);
  94. model1.string = @"Apple";
  95. model1.string2 = @"Apple";
  96. model2.string = @"Steve Jobs";
  97. model2.string2 = @"Steve Jobs";
  98. XCTAssertTrue(model1.hash == model2.hash);
  99. XCTAssertFalse([model1 isEqual:model2]);
  100. }
  101. - (void)testCopying {
  102. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  103. YYTestModelHashModel *model2 = nil;
  104. model1.intValue = 1;
  105. model1.floatValue = 12.34;
  106. model1.myStructValue = (my_struct){.b = 56.78};
  107. model1.string = @"Apple";
  108. model2 = model1.copy;
  109. XCTAssertEqual(model1.intValue, model2.intValue);
  110. XCTAssertEqual(model1.floatValue, model2.floatValue);
  111. XCTAssertEqual(model1.myStructValue.b, model2.myStructValue.b);
  112. XCTAssertTrue([model1.string isEqualToString:model2.string]);
  113. }
  114. - (void)testCoding {
  115. NSData *data = nil;
  116. YYTestModelHashModel *model1 = [YYTestModelHashModel new];
  117. YYTestModelHashModel *model2 = nil;
  118. model1.intValue = 1;
  119. model1.floatValue = 12.34;
  120. model1.number = @(1234);
  121. model1.string = @"Apple";
  122. model1.sizeValue = CGSizeMake(12, 34);
  123. model1.selectorValue = @selector(stringWithFormat:);
  124. model1.myStructValue = (my_struct){.b = 56.78};
  125. model1.valueValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  126. data = [NSKeyedArchiver archivedDataWithRootObject:model1];
  127. model2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  128. XCTAssertEqual(model1.intValue, model2.intValue);
  129. XCTAssertEqual(model1.floatValue, model2.floatValue);
  130. XCTAssertTrue([model1.number isEqual:model2.number]);
  131. XCTAssertTrue([model1.string isEqualToString:model2.string]);
  132. XCTAssertTrue(CGSizeEqualToSize(model1.sizeValue, model2.sizeValue));
  133. XCTAssertTrue(model2.selectorValue == @selector(stringWithFormat:));
  134. XCTAssertTrue(model2.myStructValue.b == 0); // ignore in NSKeyedArchiver
  135. XCTAssertTrue(model2.valueValue == nil); // ignore in NSKeyedArchiver
  136. // for code coverage
  137. NSArray *array = @[model1, model2];
  138. NSMutableData *mutableData = [NSMutableData new];
  139. NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  140. [array yy_modelEncodeWithCoder:coder];
  141. [coder finishEncoding];
  142. XCTAssertTrue(mutableData.length > 0);
  143. mutableData = [NSMutableData new];
  144. coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutableData];
  145. [[NSNull null] yy_modelEncodeWithCoder:coder];
  146. [coder finishEncoding];
  147. XCTAssertTrue(mutableData.length > 0);
  148. }
  149. @end