YYTestAutoTypeConvert.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // YYTestAutoTypeConvert.m
  3. // YYModel <https://github.com/ibireme/YYModel>
  4. //
  5. // Created by ibireme on 15/11/28.
  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 YYTestAutoTypeModel : 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 YYTestAutoTypeModel
  54. + (NSDictionary *)modelCustomPropertyMapper {
  55. return @{ @"boolValue" : @"v",
  56. @"BOOLValue" : @"v",
  57. @"charValue" : @"v",
  58. @"unsignedCharValue" : @"v",
  59. @"shortValue" : @"v",
  60. @"unsignedShortValue" : @"v",
  61. @"intValue" : @"v",
  62. @"unsignedIntValue" : @"v",
  63. @"longValue" : @"v",
  64. @"unsignedLongValue" : @"v",
  65. @"longLongValue" : @"v",
  66. @"unsignedLongLongValue" : @"v",
  67. @"floatValue" : @"v",
  68. @"doubleValue" : @"v",
  69. @"longDoubleValue" : @"v",
  70. @"classValue" : @"v",
  71. @"selectorValue" : @"v",
  72. @"blockValue" : @"v",
  73. @"pointerValue" : @"v",
  74. @"structValue" : @"v",
  75. @"pointValue" : @"v",
  76. @"object" : @"v",
  77. @"number" : @"v",
  78. @"decimal" : @"v",
  79. @"string" : @"v",
  80. @"mString" : @"v",
  81. @"data" : @"v",
  82. @"mData" : @"v",
  83. @"date" : @"v",
  84. @"value" : @"v",
  85. @"url" : @"v",
  86. @"array" : @"v",
  87. @"mArray" : @"v",
  88. @"dict" : @"v",
  89. @"mDict" : @"v",
  90. @"set" : @"v",
  91. @"mSet" : @"v"
  92. };
  93. }
  94. @end
  95. @interface YYTestAutoTypeConvert : XCTestCase
  96. @end
  97. @implementation YYTestAutoTypeConvert
  98. - (void)testNumber {
  99. NSString *json;
  100. YYTestAutoTypeModel *model;
  101. json = @"{\"v\" : 1}";
  102. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  103. XCTAssert(model.boolValue);
  104. XCTAssert(model.BOOLValue);
  105. XCTAssert(model.charValue == 1);
  106. XCTAssert(model.unsignedCharValue == 1);
  107. XCTAssert(model.shortValue == 1);
  108. XCTAssert(model.unsignedShortValue == 1);
  109. XCTAssert(model.intValue == 1);
  110. XCTAssert(model.unsignedIntValue == 1);
  111. XCTAssert(model.longValue == 1);
  112. XCTAssert(model.unsignedLongValue == 1);
  113. XCTAssert(model.longLongValue == 1);
  114. XCTAssert(model.unsignedLongLongValue == 1);
  115. XCTAssert(model.floatValue == 1);
  116. XCTAssert(model.doubleValue == 1);
  117. XCTAssert(model.longDoubleValue == 1);
  118. XCTAssert([model.object isEqual:@(1)]);
  119. XCTAssert([model.number isEqual:@(1)]);
  120. XCTAssert([model.decimal isEqual:@(1)]);
  121. XCTAssert([model.string isEqualToString:@"1"]);
  122. XCTAssert([model.mString isEqualToString:@"1"]);
  123. XCTAssert([model.mString isKindOfClass:[NSMutableString class]]);
  124. json = @"{\"v\" : 1.5}";
  125. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  126. XCTAssert(model.boolValue);
  127. XCTAssert(model.BOOLValue);
  128. XCTAssert(model.charValue == 1);
  129. XCTAssert(model.unsignedCharValue == 1);
  130. XCTAssert(model.shortValue == 1);
  131. XCTAssert(model.unsignedShortValue == 1);
  132. XCTAssert(model.intValue == 1);
  133. XCTAssert(model.unsignedIntValue == 1);
  134. XCTAssert(model.longValue == 1);
  135. XCTAssert(model.unsignedLongValue == 1);
  136. XCTAssert(model.longLongValue == 1);
  137. XCTAssert(model.unsignedLongLongValue == 1);
  138. XCTAssert(model.floatValue == 1.5);
  139. XCTAssert(model.doubleValue == 1.5);
  140. XCTAssert(model.longDoubleValue == 1.5);
  141. XCTAssert([model.object isEqual:@(1.5)]);
  142. XCTAssert([model.number isEqual:@(1.5)]);
  143. XCTAssert([model.decimal isEqual:@(1.5)]);
  144. XCTAssert([model.string isEqualToString:@"1.5"]);
  145. XCTAssert([model.mString isEqualToString:@"1.5"]);
  146. XCTAssert([model.mString isKindOfClass:[NSMutableString class]]);
  147. json = @"{\"v\" : -1}";
  148. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  149. XCTAssert(model.boolValue);
  150. XCTAssert(model.BOOLValue);
  151. XCTAssert(model.charValue == -1);
  152. XCTAssert(model.unsignedCharValue == (unsigned char)-1);
  153. XCTAssert(model.shortValue == -1);
  154. XCTAssert(model.unsignedShortValue == (unsigned short)-1);
  155. XCTAssert(model.intValue == -1);
  156. XCTAssert(model.unsignedIntValue == (unsigned int)-1);
  157. XCTAssert(model.longValue == -1);
  158. XCTAssert(model.unsignedLongValue == (unsigned long)-1);
  159. XCTAssert(model.longLongValue == -1);
  160. XCTAssert(model.unsignedLongLongValue == (unsigned long long)-1);
  161. XCTAssert(model.floatValue == -1);
  162. XCTAssert(model.doubleValue == -1);
  163. XCTAssert(model.longDoubleValue == -1);
  164. XCTAssert([model.object isEqual:@(-1)]);
  165. XCTAssert([model.number isEqual:@(-1)]);
  166. XCTAssert([model.decimal isEqual:@(-1)]);
  167. XCTAssert([model.string isEqualToString:@"-1"]);
  168. XCTAssert([model.mString isEqualToString:@"-1"]);
  169. XCTAssert([model.mString isKindOfClass:[NSMutableString class]]);
  170. json = @"{\"v\" : \"2\"}";
  171. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  172. XCTAssert(model.boolValue);
  173. XCTAssert(model.BOOLValue);
  174. XCTAssert(model.charValue == 2);
  175. XCTAssert(model.unsignedCharValue == 2);
  176. XCTAssert(model.shortValue == 2);
  177. XCTAssert(model.unsignedShortValue == 2);
  178. XCTAssert(model.intValue == 2);
  179. XCTAssert(model.unsignedIntValue == 2);
  180. XCTAssert(model.longValue == 2);
  181. XCTAssert(model.unsignedLongValue == 2);
  182. XCTAssert(model.longLongValue == 2);
  183. XCTAssert(model.unsignedLongLongValue == 2);
  184. XCTAssert(model.floatValue == 2);
  185. XCTAssert(model.doubleValue == 2);
  186. XCTAssert(model.longDoubleValue == 2);
  187. XCTAssert([model.object isEqual:@"2"]);
  188. XCTAssert([model.number isEqual:@(2)]);
  189. XCTAssert([model.decimal isEqual:@(2)]);
  190. XCTAssert([model.string isEqualToString:@"2"]);
  191. XCTAssert([model.mString isEqualToString:@"2"]);
  192. XCTAssert([model.mString isKindOfClass:[NSMutableString class]]);
  193. model.intValue = 12;
  194. [model yy_modelSetWithJSON:json];
  195. XCTAssert(model.intValue == 2);
  196. json = @"{\"v\" : \"-3.2\"}";
  197. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  198. XCTAssert(fabs(model.floatValue + 3.2) < 0.0001);
  199. XCTAssert(fabs(model.doubleValue + 3.2) < 0.0001);
  200. XCTAssert(fabsl(model.longDoubleValue + 3.2) < 0.0001);
  201. XCTAssert([model.object isEqual:@"-3.2"]);
  202. XCTAssert([model.number isEqual:@(-3.2)]);
  203. XCTAssert([model.decimal isEqual:@(-3.2)]);
  204. XCTAssert([model.string isEqualToString:@"-3.2"]);
  205. XCTAssert([model.mString isEqualToString:@"-3.2"]);
  206. XCTAssert([model.mString isKindOfClass:[NSMutableString class]]);
  207. json = @"{\"v\" : \"true\"}";
  208. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  209. XCTAssert(model.boolValue);
  210. XCTAssert(model.intValue == 1);
  211. json = @"{\"v\" : \"false\"}";
  212. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  213. XCTAssert(model.boolValue == 0);
  214. XCTAssert(model.intValue == 0);
  215. json = @"{\"v\" : \"YES\"}";
  216. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  217. XCTAssert(model.boolValue);
  218. XCTAssert(model.intValue == 1);
  219. json = @"{\"v\" : \"NO\"}";
  220. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  221. XCTAssert(model.boolValue == 0);
  222. XCTAssert(model.intValue == 0);
  223. json = @"{\"v\" : \"nil\"}";
  224. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  225. XCTAssert(model.boolValue == 0);
  226. XCTAssert(model.intValue == 0);
  227. XCTAssert([model.string isEqual:@"nil"]);
  228. XCTAssert(model.number == nil);
  229. json = @"{\"v\" : {}}";
  230. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  231. XCTAssert(model.boolValue == 0);
  232. XCTAssert(model.intValue == 0);
  233. XCTAssert(model.string == nil);
  234. XCTAssert(model.number == nil);
  235. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSDecimalNumber decimalNumberWithString:@"9876543210"]}];
  236. XCTAssert(model.unsignedLongLongValue == 9876543210LLU);
  237. XCTAssert(model.longLongValue == 9876543210LL);
  238. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSValue valueWithPointer:CFArrayCreate]}];
  239. XCTAssert(model.pointerValue == CFArrayCreate);
  240. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSURL class]}];
  241. XCTAssert(model.classValue == [NSURL class]);
  242. __block int i = 0;
  243. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : ^{i = 1;}}];
  244. model.blockValue();
  245. XCTAssert(i == 1);
  246. }
  247. - (void)testDate {
  248. NSString *json;
  249. YYTestAutoTypeModel *model;
  250. json = @"{\"v\" : \"2014-05-06\"}";
  251. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  252. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  253. json = @"{\"v\" : \"2014-05-06 07:08:09\"}";
  254. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  255. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  256. json = @"{\"v\" : \"2014-05-06T07:08:09\"}";
  257. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  258. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  259. json = @"{\"v\" : \"2014-01-20T12:24:48Z\"}";
  260. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  261. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  262. json = @"{\"v\" : \"2014-01-20T12:24:48+0800\"}";
  263. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  264. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  265. json = @"{\"v\" : \"2014-01-20T12:24:48+12:00\"}";
  266. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  267. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  268. json = @"{\"v\" : \"Fri Sep 04 00:12:21 +0800 2015\"}";
  269. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  270. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  271. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSDate new]}];
  272. XCTAssert([model.date isKindOfClass:[NSDate class]]);
  273. }
  274. - (void)testString {
  275. NSDictionary *json;
  276. YYTestAutoTypeModel *model;
  277. json = @{@"v" : @"Apple"};
  278. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  279. XCTAssertTrue([model.string isEqualToString:@"Apple"]);
  280. json = @{@"v" : @" github.com"};
  281. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  282. XCTAssertTrue([model.url isEqual:[NSURL URLWithString:@"github.com"]]);
  283. json = @{@"v" : @"stringWithFormat:"};
  284. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  285. XCTAssertTrue(model.selectorValue == @selector(stringWithFormat:));
  286. json = @{@"v" : @"UILabel"};
  287. model = [YYTestAutoTypeModel yy_modelWithJSON:json];
  288. XCTAssertTrue(model.classValue == UILabel.class);
  289. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [@"haha" dataUsingEncoding:NSUTF8StringEncoding]}];
  290. XCTAssert([model.string isEqualToString:@"haha"]);
  291. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSURL URLWithString:@"https://github.com"]}];
  292. XCTAssert([model.string isEqualToString:@"https://github.com"]);
  293. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : @" "}];
  294. XCTAssert(model.url == nil);
  295. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [[NSAttributedString alloc] initWithString:@"test"]}];
  296. XCTAssert([model.string isEqualToString:@"test"]);
  297. }
  298. - (void)testValue {
  299. NSValue *value;
  300. YYTestAutoTypeModel *model;
  301. value = [NSValue valueWithCGRect:CGRectMake(1, 2, 3, 4)];
  302. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : value}];
  303. XCTAssertTrue(CGRectEqualToRect(model.structValue, CGRectMake(1, 2, 3, 4)));
  304. XCTAssertTrue(CGPointEqualToPoint(model.pointValue, CGPointZero));
  305. value = [NSValue valueWithCGPoint:CGPointMake(1, 2)];
  306. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : value}];
  307. XCTAssertTrue(CGRectEqualToRect(model.structValue, CGRectZero));
  308. XCTAssertTrue(CGPointEqualToPoint(model.pointValue, CGPointMake(1, 2)));
  309. }
  310. - (void)testNull {
  311. YYTestAutoTypeModel *model;
  312. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSNull null]}];
  313. XCTAssertTrue(model.boolValue == false);
  314. XCTAssertTrue(model.object == nil);
  315. }
  316. - (void)testArrayAndDic {
  317. NSString *json;
  318. json = @"[{\"v\":1},{\"v\":2},{\"v\":3}]";
  319. NSArray *array = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:json];
  320. XCTAssertTrue(array.count == 3);
  321. XCTAssertTrue([array.firstObject isKindOfClass:[YYTestAutoTypeModel class]]);
  322. array = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonDataFromString:json]];
  323. XCTAssertTrue(array.count == 3);
  324. XCTAssertTrue([array.firstObject isKindOfClass:[YYTestAutoTypeModel class]]);
  325. array = [NSArray yy_modelArrayWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonObjectFromString:json]];
  326. XCTAssertTrue(array.count == 3);
  327. XCTAssertTrue([array.firstObject isKindOfClass:[YYTestAutoTypeModel class]]);
  328. json = @"{\"a\":{\"v\":1},\"b\":{\"v\":2},\"c\":{\"v\":3}}";
  329. NSDictionary *dict = [NSDictionary yy_modelDictionaryWithClass:YYTestAutoTypeModel.class json:json];
  330. XCTAssertTrue(dict.count == 3);
  331. XCTAssertTrue([dict[@"a"] isKindOfClass:[YYTestAutoTypeModel class]]);
  332. json = @"{\"a\":{\"v\":1},\"b\":{\"v\":2},\"c\":{\"v\":3}}";
  333. dict = [NSDictionary yy_modelDictionaryWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonDataFromString:json]];
  334. XCTAssertTrue(dict.count == 3);
  335. XCTAssertTrue([dict[@"a"] isKindOfClass:[YYTestAutoTypeModel class]]);
  336. json = @"{\"a\":{\"v\":1},\"b\":{\"v\":2},\"c\":{\"v\":3}}";
  337. dict = [NSDictionary yy_modelDictionaryWithClass:YYTestAutoTypeModel.class json:[YYTestHelper jsonObjectFromString:json]];
  338. XCTAssertTrue(dict.count == 3);
  339. XCTAssertTrue([dict[@"a"] isKindOfClass:[YYTestAutoTypeModel class]]);
  340. YYTestAutoTypeModel *model;
  341. model = [YYTestAutoTypeModel yy_modelWithJSON:@{@"v" : [NSSet setWithArray:@[@1,@2,@3]]}];
  342. XCTAssertTrue([model.array isKindOfClass:[NSArray class]]);
  343. XCTAssertTrue(model.array.count == 3);
  344. }
  345. @end