diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets index c167eb44..52a59ea8 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets @@ -26,101 +26,117 @@ struct napicoercetobool { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State textcont: string = 'napi_coerce_to_bool() 用于将任意类型的 JavaScript 值' - + '(例如 number 或 string)强制转换为 Boolean。' + + '(例如 number 或 string)强制转换为 boolean。' + '如果 API 成功,则返回 napi_ok。' + '该 API 实现了 ECMAScript 语言规范的 第 7.1.2 节 中定义的抽象操作 ToBoolean()。'; @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' - + 'const result1 = addon.testNapiCoerceToBool(\'test\'); // 非空字符串 -> true \n' - + 'const result2 = addon.testNapiCoerceToBool(\'\'); // 空字符串 -> false \n' - + 'const result3 = addon.testNapiCoerceToBool(123); // 非零值 -> true \n' - + 'const result4 = addon.testNapiCoerceToBool(0); // 零值 -> false \n' + + 'const testStr2BoolResult = addon.testNapiCoerceToBool(\'test\'); // 非空字符串 -> true \n' + + 'const testEmptyStr2BoolResult = addon.testNapiCoerceToBool(\'\'); // 空字符串 -> false \n' + + 'const testNum2BoolResult = addon.testNapiCoerceToBool(123); // 非零值 -> true \n' + + 'const testZeroNum2BoolResult = addon.testNapiCoerceToBool(0); // 零值 -> false \n' + + 'const testNull2BoolResult = addon.testNapiCoerceToBool(null); // null -> false \n' + + 'const testUndef2BoolResult = addon.testNapiCoerceToBool(undefined); // undefined -> false \n' + '// 输出强制转换结果 \n' - + 'console.log(`\'test\' -> ${result1}`); \n' - + 'console.log(`\'\' -> ${result2}`); \n' - + 'console.log(`123 -> ${result3}`); \n' - + 'console.log(`0 -> ${result4}`); \n'; + + 'console.log(`\'test\' -> ${testStr2BoolResult}`); \n' + + 'console.log(`\'\' -> ${testEmptyStr2BoolResult}`); \n' + + 'console.log(`123 -> ${testNum2BoolResult}`); \n' + + 'console.log(`0 -> ${testZeroNum2BoolResult}`); \n' + + 'console.log(`null -> ${testNull2BoolResult}`); \n' + + 'console.log(`undefined -> ${testUndef2BoolResult}`); \n'; controller: TextAreaController = new TextAreaController() private btnFontColor: Resource = $r('app.color.white'); private pixelMapFormat: image.PixelMapFormat = 3; build() { - Column() { - // 标题 - TitleBar({ title: $r('app.string.napi_coerce_to_bool') }) - + Scroll() { Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_coerce_to_bool') }) + Column() { - TextArea({ - text: this.textcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor('#182431') - .backgroundColor('#FFFFFF') - .enabled(false) - - TextArea({ - text: this.testcont, - placeholder: '', - }) - .placeholderFont({ size: 16, weight: 400 }) - .width('90%') - .margin(10) - .fontSize(16) - .fontColor('#ff400336') - .backgroundColor('#ff985307') - .enabled(false) - } - .width('100%') - .alignItems(HorizontalAlign.Center) - .justifyContent(FlexAlign.Start) - - Row() { - - Button($r('app.string.napi_coerce_to_bool'), { type: ButtonType.Capsule }) - .backgroundColor(Color.Blue) - .width('80%') - .height(48) - .fontSize(16) - .fontWeight(500) - .fontColor(this.btnFontColor) - .margin({ left: 24 }) - .id('napi_coerce_to_bool') - .onClick(() => { - try { - // Test coercing string/number values to boolean - const testStr: string = 'test'; - const testEmptyStr: string = ''; - const testNum: number = 123; - const testZeroNum: number = 0; - let testStr2BoolResult = testNapi.testNapiCoerceToBool(testStr); - let testEmptyStr2BoolResult = testNapi.testNapiCoerceToBool(testEmptyStr); - let testNum2BoolResult = testNapi.testNapiCoerceToBool(testNum); - let testZeroNum2BoolResult = testNapi.testNapiCoerceToBool(testZeroNum); - // Replace result in testcont - this.testcont = this.testcont.replace('${result1}', `## ${testStr2BoolResult} ##`); - this.testcont = this.testcont.replace('${result2}', `## ${testEmptyStr2BoolResult} ##`); - this.testcont = this.testcont.replace('${result3}', `## ${testNum2BoolResult} ##`); - this.testcont = this.testcont.replace('${result4}', `## ${testZeroNum2BoolResult} ##`); - // Print the results - hilog.info(0x0000, TAG, `(${typeof (testStr)})${testStr} -> ${testStr2BoolResult}`); - hilog.info(0x0000, TAG, `(${typeof (testEmptyStr)})${testEmptyStr} -> ${testEmptyStr2BoolResult}`); - hilog.info(0x0000, TAG, `(${typeof (testNum)})${testNum} -> ${testNum2BoolResult}`); - hilog.info(0x0000, TAG, `(${typeof (testZeroNum)})${testZeroNum} -> ${testZeroNum2BoolResult}`); - } catch (error) { - hilog.error(0x0000, TAG, `Catch error testNapiCoerceToBool: ${error.message}}`) - } + Column() { + TextArea({ + text: this.textcont, + placeholder: '', }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#182431') + .backgroundColor('#FFFFFF') + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#ff400336') + .backgroundColor('#ff985307') + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_coerce_to_bool'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_coerce_to_bool') + .onClick(() => { + try { + // Test coercing string/number/null/undefined values to boolean + const testStr: string = 'test'; + const testEmptyStr: string = ''; + const testNum: number = 123; + const testZeroNum: number = 0; + const testNull: null = null; + const testUndef: undefined = undefined; + const testStr2BoolResult = testNapi.testNapiCoerceToBool(testStr); + const testEmptyStr2BoolResult = testNapi.testNapiCoerceToBool(testEmptyStr); + const testNum2BoolResult = testNapi.testNapiCoerceToBool(testNum); + const testZeroNum2BoolResult = testNapi.testNapiCoerceToBool(testZeroNum); + const testNull2BoolResult = testNapi.testNapiCoerceToBool(testNull); + const testUndef2BoolResult = testNapi.testNapiCoerceToBool(testUndef); + + // Replace result in testcont + this.testcont = this.testcont.replace('${testStr2BoolResult}', `## ${testStr2BoolResult} ##`); + this.testcont = this.testcont.replace('${testEmptyStr2BoolResult}', `## ${testEmptyStr2BoolResult} ##`); + this.testcont = this.testcont.replace('${testNum2BoolResult}', `## ${testNum2BoolResult} ##`); + this.testcont = this.testcont.replace('${testZeroNum2BoolResult}', `## ${testZeroNum2BoolResult} ##`); + this.testcont = this.testcont.replace('${testNull2BoolResult}', `## ${testNull2BoolResult} ##`); + this.testcont = this.testcont.replace('${testUndef2BoolResult}', `## ${testUndef2BoolResult} ##`); + + // Print the results + hilog.info(0x0000, TAG, `(${typeof (testStr)})${testStr} -> ${testStr2BoolResult}`); + hilog.info(0x0000, TAG, `(${typeof (testEmptyStr)})${testEmptyStr} -> ${testEmptyStr2BoolResult}`); + hilog.info(0x0000, TAG, `(${typeof (testNum)})${testNum} -> ${testNum2BoolResult}`); + hilog.info(0x0000, TAG, `(${typeof (testZeroNum)})${testZeroNum} -> ${testZeroNum2BoolResult}`); + hilog.info(0x0000, TAG, `(${typeof (testNull)})${testNull} -> ${testNull2BoolResult}`); + hilog.info(0x0000, TAG, `(${typeof (testUndef)})${testUndef} -> ${testUndef2BoolResult}`); + } catch (error) { + hilog.error(0x0000, TAG, `Catch error testNapiCoerceToBool: ${error.message}}`) + } + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) } .width('100%') - .height(48) - .alignItems(VerticalAlign.Center) - .justifyContent(FlexAlign.SpaceBetween) } - .width('100%') } .height('100%') .width('100%') diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets index 9b5a974e..14942feb 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets @@ -38,36 +38,51 @@ export default function jsAbstractOpsTest() { // Presets a clear action, which is performed after all test cases of the test suite end. // This API supports only one parameter: clear action function. }) + + /** + * cpp/javascriptapi/jsabstractops/napicoercetobool.cpp + * pages/javascript/jsabstractops/napicoercetobool.ets + */ it('napiCoerceToBool', 0, () => { hilog.info(0x0000, 'testTag', '%{public}s', 'it napiCoerceToBool begin'); - // Test coercing string/number values to boolean + // Test coercing string/number/null/undefined values to boolean const testStr: string = 'test'; const testEmptyStr: string = ''; const testNum: number = 123; const testZeroNum: number = 0; - let testStr2BoolResult = testNapi.testNapiCoerceToBool(testStr); - let testEmptyStr2BoolResult = testNapi.testNapiCoerceToBool(testEmptyStr); - let testNum2BoolResult = testNapi.testNapiCoerceToBool(testNum); - let testZeroNum2BoolResult = testNapi.testNapiCoerceToBool(testZeroNum); + const testNull: null = null; + const testUndef: undefined = undefined; + const testStr2BoolResult = testNapi.testNapiCoerceToBool(testStr); + const testEmptyStr2BoolResult = testNapi.testNapiCoerceToBool(testEmptyStr); + const testNum2BoolResult = testNapi.testNapiCoerceToBool(testNum); + const testZeroNum2BoolResult = testNapi.testNapiCoerceToBool(testZeroNum); + const testNull2BoolResult = testNapi.testNapiCoerceToBool(testNull); + const testUndef2BoolResult = testNapi.testNapiCoerceToBool(testUndef); // Print the results hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testStr}) = ${testStr2BoolResult}`); hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testEmptyStr}) = ${testEmptyStr2BoolResult}`); hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testNum}) = ${testNum2BoolResult}`); hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testZeroNum}) = ${testZeroNum2BoolResult}`); + hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testNull}) = ${testNull2BoolResult}`); + hilog.info(0x0000, 'testTag', `napi_coerce_to_bool(${testUndef}) = ${testUndef2BoolResult}`); // Define assertion methods for type checking expect(testStr2BoolResult).assertInstanceOf('Boolean'); expect(testEmptyStr2BoolResult).assertInstanceOf('Boolean'); expect(testNum2BoolResult).assertInstanceOf('Boolean'); expect(testZeroNum2BoolResult).assertInstanceOf('Boolean'); + expect(testNull2BoolResult).assertInstanceOf('Boolean'); + expect(testUndef2BoolResult).assertInstanceOf('Boolean'); // Define assertion methods for value checking expect(testStr2BoolResult).assertTrue(); expect(testEmptyStr2BoolResult).assertFalse(); expect(testNum2BoolResult).assertTrue(); expect(testZeroNum2BoolResult).assertFalse(); + expect(testNull2BoolResult).assertFalse(); + expect(testUndef2BoolResult).assertFalse(); hilog.info(0x0000, 'testTag', '%{public}s', 'it napiCoerceToBool end'); }) })