YYTestAutoTypeConvert.m 17 KB

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