Skip to content

Commit

Permalink
fix(ios): type error when conversion of bool value to ctx value
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Sep 20, 2024
1 parent e425834 commit 3818c9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion framework/ios/utils/NSObject+CtxValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ - (CtxValuePtr)convertToCtxValue:(const CtxPtr &)context {
@implementation NSNumber (CtxValue)

- (CtxValuePtr)convertToCtxValue:(const CtxPtr &)context {
return context->CreateNumber([self doubleValue]);
if ([self isKindOfClass:[@YES class]]) {
return context->CreateBoolean(self.boolValue);
} else {
return context->CreateNumber(self.doubleValue);
}
}

@end
Expand Down
6 changes: 5 additions & 1 deletion tests/ios/HippyCtxValueConvertTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ - (void)testNSNumberToCtxValue {

// NSNumber (Boolean)
- (void)testBoolToCtxValue {
auto testCtxBoolean = _context->CreateBoolean(true);
NSNumber *testOCBool = @YES;
CtxValuePtr testCtxBoolean = [testOCBool convertToCtxValue:_context];
XCTAssert(_context->IsBoolean(testCtxBoolean));
XCTAssertTrue([ObjectFromCtxValue(_context, testCtxBoolean) boolValue] == [testOCBool boolValue]);
testOCBool = @NO;
testCtxBoolean = [testOCBool convertToCtxValue:_context];
XCTAssert(_context->IsBoolean(testCtxBoolean));
XCTAssertTrue([ObjectFromCtxValue(_context, testCtxBoolean) boolValue] == [testOCBool boolValue]);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ios/HippyOC2CtxValueTest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ - (void)testOCObject2CtxValue {
NSMutableDictionary *nestedDic = [NSMutableDictionary dictionary];
nestedDic[@"testZeroData"] = [NSData data];
nestedDic[@"testNumber"] = @200;
nestedDic[@"testBool"] = @YES;
nestedDic[@"testNull"] = [NSNull null];
nestedDic[@"testString"] = @"";
nestedDic[@"testString1"] = @"0";
Expand All @@ -70,6 +71,12 @@ - (void)testOCObject2CtxValue {
ctxValue = [@[testDic, testDic, testDic] convertToCtxValue:context];
XCTAssert(ctxValue != nullptr);

NSArray *testDicArr = ObjectFromCtxValue(context, ctxValue);
XCTAssert(testDicArr.count == 3);
XCTAssert([testDicArr.firstObject[@"testDic"][@"testNumber"] intValue] == 200);
XCTAssert([testDicArr.firstObject[@"testDic"][@"testBool"] boolValue] == YES);
XCTAssert([testDicArr.firstObject[@"testDic"][@"testString1"] isEqualToString:@"0"]);

NSMutableArray *testArr = [NSMutableArray array];
[testArr addObject:[NSData data]];
[testArr addObject:[NSDate date]];
Expand Down

0 comments on commit 3818c9c

Please sign in to comment.