11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst test = require(`./build/${common.buildType}/test_conversions`); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst boolExpected = /boolean was expected/; 71cb0ef41Sopenharmony_ciconst numberExpected = /number was expected/; 81cb0ef41Sopenharmony_ciconst stringExpected = /string was expected/; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst testSym = Symbol('test'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciassert.strictEqual(test.asBool(false), false); 131cb0ef41Sopenharmony_ciassert.strictEqual(test.asBool(true), true); 141cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(undefined), boolExpected); 151cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(null), boolExpected); 161cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(Number.NaN), boolExpected); 171cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(0), boolExpected); 181cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(''), boolExpected); 191cb0ef41Sopenharmony_ciassert.throws(() => test.asBool('0'), boolExpected); 201cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(1), boolExpected); 211cb0ef41Sopenharmony_ciassert.throws(() => test.asBool('1'), boolExpected); 221cb0ef41Sopenharmony_ciassert.throws(() => test.asBool('true'), boolExpected); 231cb0ef41Sopenharmony_ciassert.throws(() => test.asBool({}), boolExpected); 241cb0ef41Sopenharmony_ciassert.throws(() => test.asBool([]), boolExpected); 251cb0ef41Sopenharmony_ciassert.throws(() => test.asBool(testSym), boolExpected); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci[test.asInt32, test.asUInt32, test.asInt64].forEach((asInt) => { 281cb0ef41Sopenharmony_ci assert.strictEqual(asInt(0), 0); 291cb0ef41Sopenharmony_ci assert.strictEqual(asInt(1), 1); 301cb0ef41Sopenharmony_ci assert.strictEqual(asInt(1.0), 1); 311cb0ef41Sopenharmony_ci assert.strictEqual(asInt(1.1), 1); 321cb0ef41Sopenharmony_ci assert.strictEqual(asInt(1.9), 1); 331cb0ef41Sopenharmony_ci assert.strictEqual(asInt(0.9), 0); 341cb0ef41Sopenharmony_ci assert.strictEqual(asInt(999.9), 999); 351cb0ef41Sopenharmony_ci assert.strictEqual(asInt(Number.NaN), 0); 361cb0ef41Sopenharmony_ci assert.throws(() => asInt(undefined), numberExpected); 371cb0ef41Sopenharmony_ci assert.throws(() => asInt(null), numberExpected); 381cb0ef41Sopenharmony_ci assert.throws(() => asInt(false), numberExpected); 391cb0ef41Sopenharmony_ci assert.throws(() => asInt(''), numberExpected); 401cb0ef41Sopenharmony_ci assert.throws(() => asInt('1'), numberExpected); 411cb0ef41Sopenharmony_ci assert.throws(() => asInt({}), numberExpected); 421cb0ef41Sopenharmony_ci assert.throws(() => asInt([]), numberExpected); 431cb0ef41Sopenharmony_ci assert.throws(() => asInt(testSym), numberExpected); 441cb0ef41Sopenharmony_ci}); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciassert.strictEqual(test.asInt32(-1), -1); 471cb0ef41Sopenharmony_ciassert.strictEqual(test.asInt64(-1), -1); 481cb0ef41Sopenharmony_ciassert.strictEqual(test.asUInt32(-1), Math.pow(2, 32) - 1); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(0), 0); 511cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(1), 1); 521cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(1.0), 1.0); 531cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(1.1), 1.1); 541cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(1.9), 1.9); 551cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(0.9), 0.9); 561cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(999.9), 999.9); 571cb0ef41Sopenharmony_ciassert.strictEqual(test.asDouble(-1), -1); 581cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(test.asDouble(Number.NaN))); 591cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble(undefined), numberExpected); 601cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble(null), numberExpected); 611cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble(false), numberExpected); 621cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble(''), numberExpected); 631cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble('1'), numberExpected); 641cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble({}), numberExpected); 651cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble([]), numberExpected); 661cb0ef41Sopenharmony_ciassert.throws(() => test.asDouble(testSym), numberExpected); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciassert.strictEqual(test.asString(''), ''); 691cb0ef41Sopenharmony_ciassert.strictEqual(test.asString('test'), 'test'); 701cb0ef41Sopenharmony_ciassert.throws(() => test.asString(undefined), stringExpected); 711cb0ef41Sopenharmony_ciassert.throws(() => test.asString(null), stringExpected); 721cb0ef41Sopenharmony_ciassert.throws(() => test.asString(false), stringExpected); 731cb0ef41Sopenharmony_ciassert.throws(() => test.asString(1), stringExpected); 741cb0ef41Sopenharmony_ciassert.throws(() => test.asString(1.1), stringExpected); 751cb0ef41Sopenharmony_ciassert.throws(() => test.asString(Number.NaN), stringExpected); 761cb0ef41Sopenharmony_ciassert.throws(() => test.asString({}), stringExpected); 771cb0ef41Sopenharmony_ciassert.throws(() => test.asString([]), stringExpected); 781cb0ef41Sopenharmony_ciassert.throws(() => test.asString(testSym), stringExpected); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(true), true); 811cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(1), true); 821cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(-1), true); 831cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool('true'), true); 841cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool('false'), true); 851cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool({}), true); 861cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool([]), true); 871cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(testSym), true); 881cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(false), false); 891cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(undefined), false); 901cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(null), false); 911cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(0), false); 921cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(Number.NaN), false); 931cb0ef41Sopenharmony_ciassert.strictEqual(test.toBool(''), false); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(0), 0); 961cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(1), 1); 971cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(1.1), 1.1); 981cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(-1), -1); 991cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber('0'), 0); 1001cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber('1'), 1); 1011cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber('1.1'), 1.1); 1021cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber([]), 0); 1031cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(false), 0); 1041cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(null), 0); 1051cb0ef41Sopenharmony_ciassert.strictEqual(test.toNumber(''), 0); 1061cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(test.toNumber(Number.NaN))); 1071cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(test.toNumber({}))); 1081cb0ef41Sopenharmony_ciassert.ok(Number.isNaN(test.toNumber(undefined))); 1091cb0ef41Sopenharmony_ciassert.throws(() => test.toNumber(testSym), TypeError); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciassert.deepStrictEqual({}, test.toObject({})); 1121cb0ef41Sopenharmony_ciassert.deepStrictEqual({ 'test': 1 }, test.toObject({ 'test': 1 })); 1131cb0ef41Sopenharmony_ciassert.deepStrictEqual([], test.toObject([])); 1141cb0ef41Sopenharmony_ciassert.deepStrictEqual([ 1, 2, 3 ], test.toObject([ 1, 2, 3 ])); 1151cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Boolean(false), test.toObject(false)); 1161cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Boolean(true), test.toObject(true)); 1171cb0ef41Sopenharmony_ciassert.deepStrictEqual(new String(''), test.toObject('')); 1181cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Number(0), test.toObject(0)); 1191cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Number(Number.NaN), test.toObject(Number.NaN)); 1201cb0ef41Sopenharmony_ciassert.deepStrictEqual(new Object(testSym), test.toObject(testSym)); 1211cb0ef41Sopenharmony_ciassert.notStrictEqual(test.toObject(false), false); 1221cb0ef41Sopenharmony_ciassert.notStrictEqual(test.toObject(true), true); 1231cb0ef41Sopenharmony_ciassert.notStrictEqual(test.toObject(''), ''); 1241cb0ef41Sopenharmony_ciassert.notStrictEqual(test.toObject(0), 0); 1251cb0ef41Sopenharmony_ciassert.ok(!Number.isNaN(test.toObject(Number.NaN))); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(''), ''); 1281cb0ef41Sopenharmony_ciassert.strictEqual(test.toString('test'), 'test'); 1291cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(undefined), 'undefined'); 1301cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(null), 'null'); 1311cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(false), 'false'); 1321cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(true), 'true'); 1331cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(0), '0'); 1341cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(1.1), '1.1'); 1351cb0ef41Sopenharmony_ciassert.strictEqual(test.toString(Number.NaN), 'NaN'); 1361cb0ef41Sopenharmony_ciassert.strictEqual(test.toString({}), '[object Object]'); 1371cb0ef41Sopenharmony_ciassert.strictEqual(test.toString({ toString: () => 'test' }), 'test'); 1381cb0ef41Sopenharmony_ciassert.strictEqual(test.toString([]), ''); 1391cb0ef41Sopenharmony_ciassert.strictEqual(test.toString([ 1, 2, 3 ]), '1,2,3'); 1401cb0ef41Sopenharmony_ciassert.throws(() => test.toString(testSym), TypeError); 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueBool(), { 1431cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1441cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1451cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1461cb0ef41Sopenharmony_ci inputTypeCheck: 'A boolean was expected', 1471cb0ef41Sopenharmony_ci}); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueInt32(), { 1501cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1511cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1521cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1531cb0ef41Sopenharmony_ci inputTypeCheck: 'A number was expected', 1541cb0ef41Sopenharmony_ci}); 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueUint32(), { 1571cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1581cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1591cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1601cb0ef41Sopenharmony_ci inputTypeCheck: 'A number was expected', 1611cb0ef41Sopenharmony_ci}); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueInt64(), { 1641cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1651cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1661cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1671cb0ef41Sopenharmony_ci inputTypeCheck: 'A number was expected', 1681cb0ef41Sopenharmony_ci}); 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueDouble(), { 1721cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1731cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1741cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1751cb0ef41Sopenharmony_ci inputTypeCheck: 'A number was expected', 1761cb0ef41Sopenharmony_ci}); 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.coerceToBool(), { 1791cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1801cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1811cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1821cb0ef41Sopenharmony_ci inputTypeCheck: 'napi_ok', 1831cb0ef41Sopenharmony_ci}); 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.coerceToObject(), { 1861cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1871cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1881cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1891cb0ef41Sopenharmony_ci inputTypeCheck: 'napi_ok', 1901cb0ef41Sopenharmony_ci}); 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.coerceToString(), { 1931cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 1941cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 1951cb0ef41Sopenharmony_ci resultIsNull: 'Invalid argument', 1961cb0ef41Sopenharmony_ci inputTypeCheck: 'napi_ok', 1971cb0ef41Sopenharmony_ci}); 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueStringUtf8(), { 2001cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 2011cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 2021cb0ef41Sopenharmony_ci wrongTypeIn: 'A string was expected', 2031cb0ef41Sopenharmony_ci bufAndOutLengthIsNull: 'Invalid argument', 2041cb0ef41Sopenharmony_ci}); 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueStringLatin1(), { 2071cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 2081cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 2091cb0ef41Sopenharmony_ci wrongTypeIn: 'A string was expected', 2101cb0ef41Sopenharmony_ci bufAndOutLengthIsNull: 'Invalid argument', 2111cb0ef41Sopenharmony_ci}); 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ciassert.deepStrictEqual(test.testNull.getValueStringUtf16(), { 2141cb0ef41Sopenharmony_ci envIsNull: 'Invalid argument', 2151cb0ef41Sopenharmony_ci valueIsNull: 'Invalid argument', 2161cb0ef41Sopenharmony_ci wrongTypeIn: 'A string was expected', 2171cb0ef41Sopenharmony_ci bufAndOutLengthIsNull: 'Invalid argument', 2181cb0ef41Sopenharmony_ci}); 219