Skip to content

Commit

Permalink
增加 NSNull 的单元测试案例 (#777)
Browse files Browse the repository at this point in the history
增加模型转字典 Date -> String 的示例代码
  • Loading branch information
wolfcon authored Jun 22, 2020
1 parent b50e9b5 commit af1c935
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 18 deletions.
42 changes: 24 additions & 18 deletions MJExtensionDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 0130EE7F233C56D8008D2386 /* MJFrenchUser.m */; };
01F28187248F64ED008775BA /* MJCat.m in Sources */ = {isa = PBXBuildFile; fileRef = 01F28186248F64ED008775BA /* MJCat.m */; };
2D2DB9CD2317DA07005A689E /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D2DB9CC2317DA07005A689E /* AppDelegate.m */; };
2D2DB9D02317DA07005A689E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D2DB9CF2317DA07005A689E /* ViewController.m */; };
2D2DB9D32317DA07005A689E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2D2DB9D12317DA07005A689E /* Main.storyboard */; };
Expand Down Expand Up @@ -86,6 +87,8 @@
/* Begin PBXFileReference section */
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJFrenchUser.h; sourceTree = "<group>"; };
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJFrenchUser.m; sourceTree = "<group>"; };
01F28185248F64ED008775BA /* MJCat.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MJCat.h; sourceTree = "<group>"; };
01F28186248F64ED008775BA /* MJCat.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJCat.m; sourceTree = "<group>"; };
2D2DB9C82317DA07005A689E /* MJExtensionDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MJExtensionDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2D2DB9CB2317DA07005A689E /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
2D2DB9CC2317DA07005A689E /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,30 +257,32 @@
2D2DBA5F2317DBDF005A689E /* Model */ = {
isa = PBXGroup;
children = (
2D2DBA602317DBDF005A689E /* MJDog.m */,
2D2DBA612317DBDF005A689E /* MJBaseObject.h */,
2D2DBA622317DBDF005A689E /* MJStatus.h */,
2D2DBA632317DBDF005A689E /* MJAd.h */,
2D2DBA642317DBDF005A689E /* MJStatusResult.h */,
2D2DBA652317DBDF005A689E /* MJBox.m */,
2D2DBA662317DBDF005A689E /* MJStudent.m */,
2D2DBA672317DBDF005A689E /* MJUser.h */,
2D2DBA682317DBDF005A689E /* MJPerson.h */,
2D2DBA692317DBDF005A689E /* MJBook.m */,
2D2DBA732317DBDF005A689E /* MJAd.m */,
2D2DBA742317DBDF005A689E /* MJBag.h */,
2D2DBA6A2317DBDF005A689E /* MJBag.m */,
2D2DBA6B2317DBDF005A689E /* MJExtensionConfig.h */,
2D2DBA612317DBDF005A689E /* MJBaseObject.h */,
2D2DBA6C2317DBDF005A689E /* MJBaseObject.m */,
2D2DBA6D2317DBDF005A689E /* MJDog.h */,
2D2DBA6E2317DBDF005A689E /* MJUser.m */,
2D2DBA6F2317DBDF005A689E /* MJStudent.h */,
2D2DBA762317DBDF005A689E /* MJBook.h */,
2D2DBA692317DBDF005A689E /* MJBook.m */,
2D2DBA702317DBDF005A689E /* MJBox.h */,
2D2DBA712317DBDF005A689E /* MJStatusResult.m */,
2D2DBA722317DBDF005A689E /* MJStatus.m */,
2D2DBA732317DBDF005A689E /* MJAd.m */,
2D2DBA742317DBDF005A689E /* MJBag.h */,
2D2DBA652317DBDF005A689E /* MJBox.m */,
01F28185248F64ED008775BA /* MJCat.h */,
01F28186248F64ED008775BA /* MJCat.m */,
2D2DBA6D2317DBDF005A689E /* MJDog.h */,
2D2DBA602317DBDF005A689E /* MJDog.m */,
2D2DBA6B2317DBDF005A689E /* MJExtensionConfig.h */,
2D2DBA752317DBDF005A689E /* MJExtensionConfig.m */,
2D2DBA762317DBDF005A689E /* MJBook.h */,
2D2DBA682317DBDF005A689E /* MJPerson.h */,
2D2DBA772317DBDF005A689E /* MJPerson.m */,
2D2DBA622317DBDF005A689E /* MJStatus.h */,
2D2DBA722317DBDF005A689E /* MJStatus.m */,
2D2DBA642317DBDF005A689E /* MJStatusResult.h */,
2D2DBA712317DBDF005A689E /* MJStatusResult.m */,
2D2DBA662317DBDF005A689E /* MJStudent.m */,
2D2DBA6F2317DBDF005A689E /* MJStudent.h */,
2D2DBA672317DBDF005A689E /* MJUser.h */,
2D2DBA6E2317DBDF005A689E /* MJUser.m */,
0130EE7E233C56D8008D2386 /* MJFrenchUser.h */,
0130EE7F233C56D8008D2386 /* MJFrenchUser.m */,
);
Expand Down Expand Up @@ -469,6 +474,7 @@
2D2DBA832317DBE0005A689E /* MJAd.m in Sources */,
2D2DBA802317DBE0005A689E /* MJUser.m in Sources */,
0130EE80233C56D8008D2386 /* MJFrenchUser.m in Sources */,
01F28187248F64ED008775BA /* MJCat.m in Sources */,
2D2DBA852317DBE0005A689E /* MJPerson.m in Sources */,
2D2DBA7B2317DBE0005A689E /* MJBox.m in Sources */,
2D2DBA582317DBB9005A689E /* MJExtensionTests.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions MJExtensionTests/MJExtensionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "MJBox.h"
#import <CoreData/CoreData.h>
#import "MJFrenchUser.h"
#import "MJCat.h"

@interface MJExtensionTests : XCTestCase

Expand Down Expand Up @@ -440,6 +441,19 @@ - (void)testNull {
};
MJUser *testNull = [MJUser mj_objectWithKeyValues:dic];
MJExtensionLog(@"%@", testNull);


NSDictionary *catDict = @{
@"name": @"Tom",
@"address": [NSNull null],
@"nicknames": @[
@"Jerry's Heart",
[NSNull null],
@"Cowboy Tom",
]
};
MJCat *cat = [MJCat mj_objectWithKeyValues:catDict];
MJExtensionLog(@"%@", cat);
}

#pragma mark NSCoding示例
Expand Down Expand Up @@ -478,6 +492,7 @@ - (void)testReplacedKeyFromPropertyName121 {

#pragma mark 过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@"")
- (void)testNewValueFromOldValue {
// JSON -> Object
// 1.定义一个字典
NSDictionary *dict = @{
@"name" : @"5分钟突破iOS开发",
Expand All @@ -492,6 +507,12 @@ - (void)testNewValueFromOldValue {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
XCTAssert([[fmt stringFromDate:book.publishedTime] isEqual:@"2011-09-10"]);

//Object -> JSON
NSDictionary *bookDict = [book mj_keyValues];

XCTAssert([bookDict[@"name"] isEqualToString:@"5分钟突破iOS开发"]);
XCTAssert([bookDict[@"publishedTime"] isEqualToString:@"2011-09-10"]);
}

#pragma mark 使用MJExtensionLog打印模型的所有属性
Expand Down
7 changes: 7 additions & 0 deletions MJExtensionTests/Model/MJBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ @implementation MJBook
//
// return oldValue;
//}

- (void)mj_objectDidConvertToKeyValues:(NSMutableDictionary *)keyValues {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyy-MM-dd";
// should use sharedFormatter for better performance
keyValues[@"publishedTime"] = [fmt stringFromDate:self.publishedTime];
}
@end
21 changes: 21 additions & 0 deletions MJExtensionTests/Model/MJCat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// MJCat.h
// MJExtensionTests
//
// Created by Frank on 2020/6/9.
// Copyright © 2020 MJ Lee. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MJCat : NSObject <MJKeyValue>

@property (nonatomic, copy, nullable) NSString *name;
@property (nonatomic, copy, nullable) NSArray<NSString *> *nicknames;
@property (nonatomic, copy, nullable) NSString *address;

@end

NS_ASSUME_NONNULL_END
19 changes: 19 additions & 0 deletions MJExtensionTests/Model/MJCat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// MJCat.m
// MJExtensionTests
//
// Created by Frank on 2020/6/9.
// Copyright © 2020 MJ Lee. All rights reserved.
//

#import "MJCat.h"

@implementation MJCat

+ (NSDictionary *)mj_objectClassInArray {
return @{
@"nicknames" : NSString.class
};
}

@end
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ MJExtension
* [Coding](#Coding)
* [Camel -> underline](#Camel_underline)
* [NSString -> NSDate, nil -> @""](#NSString_NSDate)
* [NSDate -> NSString](#NSDate -> NSString)
* [More use cases](#More_use_cases)

---
Expand Down Expand Up @@ -550,7 +551,23 @@ Book *book = [Book mj_objectWithKeyValues:dict];
NSLog(@"name=%@, publisher=%@, publishedTime=%@", book.name, book.publisher, book.publishedTime);
```
### NSDate -> NSString
**模型转字典时, 修改 Date 类型至 String**
```objc
- (void)mj_objectDidConvertToKeyValues:(NSMutableDictionary *)keyValues {
// NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// formatter.dateFormat = @"yyy-MM-dd";
// should use sharedFormatter for better performance
keyValues[@"publishedTime"] = [sharedFormatter stringFromDate:self.publishedTime];
}
```



### <a id="More_use_cases"></a> More use cases【更多用法】

- Please reference to `NSObject+MJKeyValue.h` and `NSObject+MJCoding.h`


Expand Down

0 comments on commit af1c935

Please sign in to comment.