YYTestModelToJSON.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // YYTestModelToJSON.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 <UIKit/UIKit.h>
  13. #import "YYModel.h"
  14. #import "YYTestHelper.h"
  15. @interface YYTestModelToJSONModel : NSObject
  16. @property bool boolValue;
  17. @property BOOL BOOLValue;
  18. @property char charValue;
  19. @property unsigned char unsignedCharValue;
  20. @property short shortValue;
  21. @property unsigned short unsignedShortValue;
  22. @property int intValue;
  23. @property unsigned int unsignedIntValue;
  24. @property long longValue;
  25. @property unsigned long unsignedLongValue;
  26. @property long long longLongValue;
  27. @property unsigned long long unsignedLongLongValue;
  28. @property float floatValue;
  29. @property double doubleValue;
  30. @property long double longDoubleValue;
  31. @property (strong) Class classValue;
  32. @property SEL selectorValue;
  33. @property (copy) void (^blockValue)();
  34. @property void *pointerValue;
  35. @property CGRect structValue;
  36. @property CGPoint pointValue;
  37. @property (nonatomic, strong) NSObject *object;
  38. @property (nonatomic, strong) NSNumber *number;
  39. @property (nonatomic, strong) NSDecimalNumber *decimal;
  40. @property (nonatomic, strong) NSString *string;
  41. @property (nonatomic, strong) NSMutableString *mString;
  42. @property (nonatomic, strong) NSData *data;
  43. @property (nonatomic, strong) NSMutableData *mData;
  44. @property (nonatomic, strong) NSDate *date;
  45. @property (nonatomic, strong) NSValue *value;
  46. @property (nonatomic, strong) NSURL *url;
  47. @property (nonatomic, strong) NSArray *array;
  48. @property (nonatomic, strong) NSMutableArray *mArray;
  49. @property (nonatomic, strong) NSDictionary *dict;
  50. @property (nonatomic, strong) NSMutableDictionary *mDict;
  51. @property (nonatomic, strong) NSSet *set;
  52. @property (nonatomic, strong) NSMutableSet *mSet;
  53. @property (nonatomic, strong) UIColor *color;
  54. @end
  55. @implementation YYTestModelToJSONModel
  56. + (NSDictionary *)modelCustomPropertyMapper {
  57. return @{
  58. @"intValue" : @"int",
  59. @"longValue" : @"long", // mapped to same key
  60. @"unsignedLongLongValue" : @"long", // mapped to same key
  61. @"shortValue" : @"ext.short" // mapped to key path
  62. };
  63. }
  64. @end
  65. @interface YYTestKeyPathModelToJSONModel : NSObject
  66. @property (nonatomic, strong) NSString *a;
  67. @property (nonatomic, strong) NSString *b;
  68. @property (nonatomic, strong) NSString *c;
  69. @property (nonatomic, strong) NSString *d;
  70. @property (nonatomic, strong) NSString *e;
  71. @property (nonatomic, strong) NSDictionary *f;
  72. @property (nonatomic, strong) NSString *g;
  73. @end
  74. @implementation YYTestKeyPathModelToJSONModel
  75. + (NSDictionary *)modelCustomPropertyMapper {
  76. return @{
  77. @"a" : @"ext.a",
  78. @"b" : @"ext.b",
  79. @"c" : @"ext.a",
  80. @"e" : @"d.e",
  81. @"g" : @"f.g.g"
  82. };
  83. }
  84. @end
  85. @interface YYTestModelToJSON : XCTestCase
  86. @end
  87. @implementation YYTestModelToJSON
  88. - (void)testToJSON {
  89. YYTestModelToJSONModel *model = [YYTestModelToJSONModel new];
  90. model.intValue = 1;
  91. model.longValue = 2;
  92. model.unsignedLongLongValue = 3;
  93. model.shortValue = 4;
  94. model.array = @[@1,@"2",[NSURL URLWithString:@"https://github.com"]];
  95. model.set = [NSSet setWithArray:model.array];
  96. model.color = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
  97. NSDictionary *jsonObject = [model yy_modelToJSONObject];
  98. XCTAssert([jsonObject isKindOfClass:[NSDictionary class]]);
  99. XCTAssert([jsonObject[@"int"] isEqual:@(1)]);
  100. XCTAssert([jsonObject[@"long"] isEqual:@(2)] || [jsonObject[@"long"] isEqual:@(3)]);
  101. XCTAssert([ ((NSDictionary *)jsonObject[@"ext"])[@"short"] isEqual:@(4)]);
  102. XCTAssert(jsonObject[@"color"] != nil);
  103. NSString *jsonString = [model yy_modelToJSONString];
  104. XCTAssert([[YYTestHelper jsonObjectFromString:jsonString] isKindOfClass:[NSDictionary class]]);
  105. NSData *jsonData = [model yy_modelToJSONData];
  106. XCTAssert([[YYTestHelper jsonObjectFromData:jsonData] isKindOfClass:[NSDictionary class]]);
  107. model = [YYTestModelToJSONModel yy_modelWithJSON:jsonData];
  108. XCTAssert(model.intValue == 1);
  109. }
  110. - (void)testKeyPath {
  111. YYTestKeyPathModelToJSONModel *model = [YYTestKeyPathModelToJSONModel new];
  112. model.a = @"a";
  113. model.b = @"b";
  114. model.c = @"c";
  115. model.d = @"d";
  116. model.e = @"e";
  117. model.f = @{};
  118. NSDictionary *dic = [model yy_modelToJSONObject];
  119. NSDictionary *ext = dic[@"ext"];
  120. XCTAssert([ext[@"b"] isEqualToString:@"b"]);
  121. XCTAssert([ext[@"a"] isEqualToString:@"a"] || [ext[@"a"] isEqualToString:@"c"]);
  122. model.f = @{@"g" : @""};
  123. dic = [model yy_modelToJSONObject];
  124. }
  125. - (void)testDate {
  126. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  127. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  128. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
  129. NSDate *date = [NSDate dateWithTimeIntervalSince1970:100000000];
  130. NSString *dateString = [formatter stringFromDate:date];
  131. YYTestModelToJSONModel *model = [YYTestModelToJSONModel new];
  132. model.date = date;
  133. NSDictionary *jsonObject = [model yy_modelToJSONObject];
  134. XCTAssert([jsonObject[@"date"] isEqual:dateString]);
  135. NSString *jsonString = [model yy_modelToJSONString];
  136. YYTestModelToJSONModel *newModel = [YYTestModelToJSONModel yy_modelWithJSON:jsonString];
  137. XCTAssert([newModel.date isEqualToDate:date]);
  138. }
  139. @end