From be6ae6bcafc295f508d2938a2c6dbcff5d6db59e Mon Sep 17 00:00:00 2001 From: wwwcg Date: Fri, 20 Sep 2024 20:35:48 +0800 Subject: [PATCH] fix(ios): type error when conversion of bool value to ctx value (#4045) --- framework/ios/utils/NSObject+CtxValue.mm | 6 +++++- tests/ios/HippyCtxValueConvertTest.mm | 6 +++++- tests/ios/HippyOC2CtxValueTest.mm | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/framework/ios/utils/NSObject+CtxValue.mm b/framework/ios/utils/NSObject+CtxValue.mm index 591c24aa007..2dbd3939d65 100644 --- a/framework/ios/utils/NSObject+CtxValue.mm +++ b/framework/ios/utils/NSObject+CtxValue.mm @@ -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 diff --git a/tests/ios/HippyCtxValueConvertTest.mm b/tests/ios/HippyCtxValueConvertTest.mm index ed45bab3183..f902c748b0d 100644 --- a/tests/ios/HippyCtxValueConvertTest.mm +++ b/tests/ios/HippyCtxValueConvertTest.mm @@ -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]); } diff --git a/tests/ios/HippyOC2CtxValueTest.mm b/tests/ios/HippyOC2CtxValueTest.mm index eb4e3c09c2c..1a1315cc757 100644 --- a/tests/ios/HippyOC2CtxValueTest.mm +++ b/tests/ios/HippyOC2CtxValueTest.mm @@ -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"; @@ -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]];