Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): type error when conversion of bool value to ctx value #4045

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading