YYTestCustomTransform.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
  20. NSNumber *time = dic[@"time"];
  21. if ([time isKindOfClass:[NSNumber class]] && time.unsignedLongLongValue != 0) {
  22. _time = [NSDate dateWithTimeIntervalSince1970:time.unsignedLongLongValue / 1000.0];
  23. return YES;
  24. } else {
  25. return NO;
  26. }
  27. }
  28. - (BOOL)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
  29. if (_time) {
  30. dic[@"time"] = @((uint64_t)(_time.timeIntervalSince1970 * 1000));
  31. return YES;
  32. } else {
  33. return NO;
  34. }
  35. }
  36. @end
  37. @interface YYTestCustomTransform : XCTestCase
  38. @end
  39. @implementation YYTestCustomTransform
  40. - (void)test {
  41. NSString *json;
  42. YYTestCustomTransformModel *model;
  43. NSDictionary *jsonObject;
  44. json = @"{\"id\":5472746497,\"content\":\"Hello\",\"time\":1401234567000}";
  45. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  46. XCTAssert(model.time != nil);
  47. json = @"{\"id\":5472746497,\"content\":\"Hello\"}";
  48. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  49. XCTAssert(model == nil);
  50. model = [YYTestCustomTransformModel yy_modelWithDictionary:@{@"id":@5472746497,@"content":@"Hello"}];
  51. XCTAssert(model == nil);
  52. json = @"{\"id\":5472746497,\"content\":\"Hello\",\"time\":1401234567000}";
  53. model = [YYTestCustomTransformModel yy_modelWithJSON:json];
  54. jsonObject = [model yy_modelToJSONObject];
  55. XCTAssert([jsonObject[@"time"] isKindOfClass:[NSNumber class]]);
  56. model.time = nil;
  57. jsonObject = [model yy_modelToJSONObject];
  58. XCTAssert(jsonObject == nil);
  59. }
  60. @end