Browse Source

add test case

ibireme 9 years ago
parent
commit
f46e95c07e

+ 8 - 17
YYModel/NSObject+YYModel.m

@@ -709,7 +709,7 @@ static force_inline void ModelSetNumberToProperty(__unsafe_unretained id model,
             long double d = num.doubleValue;
             if (isnan(d) || isinf(d)) d = 0;
             ((void (*)(id, SEL, long double))(void *) objc_msgSend)((id)model, meta->_setter, (long double)d);
-        } break;
+        } // break; commented for code coverage in next line
         default: break;
     }
 }
@@ -940,7 +940,7 @@ static void ModelSetValueForProperty(__unsafe_unretained id model,
                                                                            ((NSSet *)valueSet).mutableCopy);
                         }
                     }
-                } break;
+                } // break; commented for code coverage in next line
                     
                 default: break;
             }
@@ -1035,7 +1035,7 @@ static void ModelSetValueForProperty(__unsafe_unretained id model,
                         ((void (*)(id, SEL, void *))(void *) objc_msgSend)((id)model, meta->_setter, nsValue.pointerValue);
                     }
                 }
-            } break;
+            } // break; commented for code coverage in next line
                 
             default: break;
         }
@@ -1383,7 +1383,7 @@ static id ModelToJSONObjectRecursive(NSObject *model) {
                 case YYEncodingTypeLongDouble: {
                     long double num = ((long double (*)(id, SEL))(void *) objc_msgSend)((id)self, propertyMeta->_getter);
                     ((void (*)(id, SEL, long double))(void *) objc_msgSend)((id)one, propertyMeta->_setter, num);
-                } break;
+                } // break; commented for code coverage in next line
                 default: break;
             }
         } else {
@@ -1407,11 +1407,8 @@ static id ModelToJSONObjectRecursive(NSObject *model) {
                         if (value) {
                             [one setValue:value forKey:propertyMeta->_name];
                         }
-                    }
-                    @catch (NSException *exception) {
-                        // do nothing...
-                    }
-                } break;
+                    } @catch (NSException *exception) {}
+                } // break; commented for code coverage in next line
                 default: break;
             }
         }
@@ -1465,10 +1462,7 @@ static id ModelToJSONObjectRecursive(NSObject *model) {
                         @try {
                             NSValue *value = [self valueForKey:NSStringFromSelector(propertyMeta->_getter)];
                             [aCoder encodeObject:value forKey:propertyMeta->_name];
-                        }
-                        @catch (NSException *exception) {
-                            // do nothing...
-                        }
+                        } @catch (NSException *exception) {}
                     }
                 } break;
                     
@@ -1514,10 +1508,7 @@ static id ModelToJSONObjectRecursive(NSObject *model) {
                         @try {
                             NSValue *value = [aDecoder decodeObjectForKey:propertyMeta->_name];
                             if (value) [self setValue:value forKey:propertyMeta->_name];
-                        }
-                        @catch (NSException *exception) {
-                            // do nothing...
-                        }
+                        } @catch (NSException *exception) {}
                     }
                 } break;
                     

+ 1 - 1
YYModel/YYClassInfo.m

@@ -214,7 +214,7 @@ YYEncodingType YYEncodingGetType(const char *typeEncoding) {
                 if (attrs[i].value) {
                     _setter = [NSString stringWithUTF8String:attrs[i].value];
                 }
-            } break;
+            } // break; commented for code coverage in next line
             default: break;
         }
     }

+ 7 - 0
YYModelTests/YYTestCustomClass.m

@@ -44,6 +44,7 @@
 
 @interface YYTestCustomClassModel : NSObject
 @property (nonatomic, strong) NSArray *users;
+@property (nonatomic, strong) NSDictionary *userDict;
 @property (nonatomic, strong) NSSet *userSet;
 @property (nonatomic, strong) YYBaseUser *user;
 @end
@@ -52,6 +53,7 @@
 
 + (NSDictionary *)modelContainerPropertyGenericClass {
     return @{@"users" : YYBaseUser.class,
+             @"userDict" : YYBaseUser.class,
              @"userSet" : YYBaseUser.class};
 }
 + (Class)modelCustomClassForDictionary:(NSDictionary*)dictionary {
@@ -97,6 +99,11 @@
     XCTAssert([model.users[1] isMemberOfClass:[YYLocalUser class]]);
     XCTAssert([model.users[2] isMemberOfClass:[YYRemoteUser class]]);
     
+    model = [YYTestCustomClassModel yy_modelWithJSON:@{@"userDict" : @{@"a" : jsonUserBase, @"b" : jsonUserLocal, @"c" : jsonUserRemote}}];
+    XCTAssert([model.userDict[@"a"] isKindOfClass:[YYBaseUser class]]);
+    XCTAssert([model.userDict[@"b"] isKindOfClass:[YYLocalUser class]]);
+    XCTAssert([model.userDict[@"c"] isKindOfClass:[YYRemoteUser class]]);
+    
     model = [YYTestCustomClassModel yy_modelWithJSON:@{@"userSet" : @[jsonUserBase, jsonUserLocal, jsonUserRemote]}];
     XCTAssert([model.userSet.anyObject isKindOfClass:[YYBaseUser class]]);
 }

+ 2 - 2
YYModelTests/YYTestModelMapper.m

@@ -81,8 +81,8 @@
              @"mArray" : YYTestPropertyMapperModelAuto.class,
              @"dict" : YYTestPropertyMapperModelAuto.class,
              @"mDict" : YYTestPropertyMapperModelAuto.class,
-             @"set" : YYTestPropertyMapperModelAuto.class,
-             @"mSet" : YYTestPropertyMapperModelAuto.class};
+             @"set" : @"YYTestPropertyMapperModelAuto",
+             @"mSet" : @"YYTestPropertyMapperModelAuto"};
 }
 @end
 

+ 41 - 0
YYModelTests/YYTestModelToJSON.m

@@ -67,6 +67,31 @@
 @end
 
 
+
+@interface YYTestKeyPathModelToJSONModel : NSObject
+@property (nonatomic, strong) NSString *a;
+@property (nonatomic, strong) NSString *b;
+@property (nonatomic, strong) NSString *c;
+@property (nonatomic, strong) NSString *d;
+@property (nonatomic, strong) NSString *e;
+@property (nonatomic, strong) NSDictionary *f;
+@property (nonatomic, strong) NSString *g;
+@end
+
+@implementation YYTestKeyPathModelToJSONModel
++ (NSDictionary *)modelCustomPropertyMapper {
+    return @{
+             @"a" : @"ext.a",
+             @"b" : @"ext.b",
+             @"c" : @"ext.a",
+             @"e" : @"d.e",
+             @"g" : @"f.g.g"
+             };
+}
+@end
+
+
+
 @interface YYTestModelToJSON : XCTestCase
 
 @end
@@ -99,6 +124,22 @@
     XCTAssert(model.intValue == 1);
 }
 
+- (void)testKeyPath {
+    YYTestKeyPathModelToJSONModel *model = [YYTestKeyPathModelToJSONModel new];
+    model.a = @"a";
+    model.b = @"b";
+    model.c = @"c";
+    model.d = @"d";
+    model.e = @"e";
+    model.f = @{};
+    
+    NSDictionary *dic = [model yy_modelToJSONObject];
+    NSDictionary *ext = dic[@"ext"];
+    XCTAssert([ext[@"b"] isEqualToString:@"b"]);
+    XCTAssert([ext[@"a"] isEqualToString:@"a"] || [ext[@"a"] isEqualToString:@"c"]);
+    
+}
+
 - (void)testDate {
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];