YYTestCustomTransform.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // YYTestCustomTransform.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. @interface YYTestCustomTransformModel : NSObject
  14. @property uint64_t id;
  15. @property NSString *content;
  16. @property NSDate *time;
  17. @end
  18. @implementation YYTestCustomTransformModel
  19. -(NSDictionary *)modelCustomWillTransformFromDictionary:(NSDictionary *)dic{
  20. if (dic) {
  21. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
  22. if (dict[@"date"]) {
  23. dict[@"time"] = dict[@"date"];
  24. }
  25. return dict;
  26. }
  27. return dic;
  28. }
  29. - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
  30. NSNumber *time = dic[@"time"];
  31. if ([time isKindOfClass:[NSNumber class]] && time.unsignedLongLongValue != 0) {
  32. _time = [NSDate dateWithTimeIntervalSince1970:time.unsignedLongLongValue / 1000.0];
  33. return YES;
  34. } else {
  35. return NO;
  36. }
  37. }
  38. - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
  39. if (_time) {
  40. dic[@"time"] = @((uint64_t)(_time.timeIntervalSince1970 * 1000));
  41. return YES;
  42. } else {
  43. return NO;
  44. }
  45. }
  46. @end
  47. @interface YYTestCustomTransform : XCTestCase
  48. @end
  49. @implementation YYTestCustomTransform
  50. - (void)test {
  51. NSString *json;
  52. YYTestCustomTransformModel *model;
  53. NSDictionary *jsonObject;
  54. json = @"{\"id\":5472746497,\"content\":\"Hello\",\"time\":1401234567000}";
  55. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  56. XCTAssert(model.time != nil);
  57. json = @"{\"id\":5472746497,\"content\":\"Hello\"}";
  58. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  59. XCTAssert(model == nil);
  60. model = [YYTestCustomTransformModel yy_modelWithDictionary:@{@"id":@5472746497,@"content":@"Hello"}];
  61. XCTAssert(model == nil);
  62. json = @"{\"id\":5472746497,\"content\":\"Hello\",\"time\":1401234567000}";
  63. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  64. jsonObject = [model yy_modelToJSONObject];
  65. XCTAssert([jsonObject[@"time"] isKindOfClass:[NSNumber class]]);
  66. model.time = nil;
  67. jsonObject = [model yy_modelToJSONObject];
  68. XCTAssert(jsonObject == nil);
  69. json = @"{\"id\":5472746497,\"content\":\"Hello\",\"date\":1401234567000}";
  70. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  71. XCTAssert(model.time != nil);
  72. }
  73. @end