YYTestModelToJSON.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 YYTestModelToJSON : XCTestCase
  64. @end
  65. @implementation YYTestModelToJSON
  66. - (void)testToJSON {
  67. YYTestModelToJSONModel *model = [YYTestModelToJSONModel new];
  68. model.intValue = 1;
  69. model.longValue = 2;
  70. model.unsignedLongLongValue = 3;
  71. model.shortValue = 4;
  72. model.array = @[@1,@"2",[NSURL URLWithString:@"https://github.com"]];
  73. model.set = [NSSet setWithArray:model.array];
  74. NSDictionary *jsonObject = [model yy_modelToJSONObject];
  75. XCTAssert([jsonObject isKindOfClass:[NSDictionary class]]);
  76. XCTAssert([jsonObject[@"int"] isEqual:@(1)]);
  77. XCTAssert([jsonObject[@"long"] isEqual:@(2)] || [jsonObject[@"long"] isEqual:@(3)]);
  78. XCTAssert([ ((NSDictionary *)jsonObject[@"ext"])[@"short"] isEqual:@(4)]);
  79. NSString *jsonString = [model yy_modelToJSONString];
  80. XCTAssert([[YYTestHelper jsonObjectFromString:jsonString] isKindOfClass:[NSDictionary class]]);
  81. NSData *jsonData = [model yy_modelToJSONData];
  82. XCTAssert([[YYTestHelper jsonObjectFromData:jsonData] isKindOfClass:[NSDictionary class]]);
  83. model = [YYTestModelToJSONModel yy_modelWithJSON:jsonData];
  84. XCTAssert(model.intValue == 1);
  85. }
  86. - (void)testDate {
  87. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  88. formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
  89. formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
  90. NSDate *date = [NSDate dateWithTimeIntervalSince1970:100000000];
  91. NSString *dateString = [formatter stringFromDate:date];
  92. YYTestModelToJSONModel *model = [YYTestModelToJSONModel new];
  93. model.date = date;
  94. NSDictionary *jsonObject = [model yy_modelToJSONObject];
  95. XCTAssert([jsonObject[@"date"] isEqual:dateString]);
  96. NSString *jsonString = [model yy_modelToJSONString];
  97. YYTestModelToJSONModel *newModel = [YYTestModelToJSONModel yy_modelWithJSON:jsonString];
  98. XCTAssert([newModel.date isEqualToDate:date]);
  99. }
  100. @end