YYTestModelToJSON.m 5.2 KB

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