1'use strict'; 2const common = require('../../common'); 3const assert = require('assert'); 4const test = require(`./build/${common.buildType}/test_conversions`); 5 6const boolExpected = /boolean was expected/; 7const numberExpected = /number was expected/; 8const stringExpected = /string was expected/; 9 10const testSym = Symbol('test'); 11 12assert.strictEqual(test.asBool(false), false); 13assert.strictEqual(test.asBool(true), true); 14assert.throws(() => test.asBool(undefined), boolExpected); 15assert.throws(() => test.asBool(null), boolExpected); 16assert.throws(() => test.asBool(Number.NaN), boolExpected); 17assert.throws(() => test.asBool(0), boolExpected); 18assert.throws(() => test.asBool(''), boolExpected); 19assert.throws(() => test.asBool('0'), boolExpected); 20assert.throws(() => test.asBool(1), boolExpected); 21assert.throws(() => test.asBool('1'), boolExpected); 22assert.throws(() => test.asBool('true'), boolExpected); 23assert.throws(() => test.asBool({}), boolExpected); 24assert.throws(() => test.asBool([]), boolExpected); 25assert.throws(() => test.asBool(testSym), boolExpected); 26 27[test.asInt32, test.asUInt32, test.asInt64].forEach((asInt) => { 28 assert.strictEqual(asInt(0), 0); 29 assert.strictEqual(asInt(1), 1); 30 assert.strictEqual(asInt(1.0), 1); 31 assert.strictEqual(asInt(1.1), 1); 32 assert.strictEqual(asInt(1.9), 1); 33 assert.strictEqual(asInt(0.9), 0); 34 assert.strictEqual(asInt(999.9), 999); 35 assert.strictEqual(asInt(Number.NaN), 0); 36 assert.throws(() => asInt(undefined), numberExpected); 37 assert.throws(() => asInt(null), numberExpected); 38 assert.throws(() => asInt(false), numberExpected); 39 assert.throws(() => asInt(''), numberExpected); 40 assert.throws(() => asInt('1'), numberExpected); 41 assert.throws(() => asInt({}), numberExpected); 42 assert.throws(() => asInt([]), numberExpected); 43 assert.throws(() => asInt(testSym), numberExpected); 44}); 45 46assert.strictEqual(test.asInt32(-1), -1); 47assert.strictEqual(test.asInt64(-1), -1); 48assert.strictEqual(test.asUInt32(-1), Math.pow(2, 32) - 1); 49 50assert.strictEqual(test.asDouble(0), 0); 51assert.strictEqual(test.asDouble(1), 1); 52assert.strictEqual(test.asDouble(1.0), 1.0); 53assert.strictEqual(test.asDouble(1.1), 1.1); 54assert.strictEqual(test.asDouble(1.9), 1.9); 55assert.strictEqual(test.asDouble(0.9), 0.9); 56assert.strictEqual(test.asDouble(999.9), 999.9); 57assert.strictEqual(test.asDouble(-1), -1); 58assert.ok(Number.isNaN(test.asDouble(Number.NaN))); 59assert.throws(() => test.asDouble(undefined), numberExpected); 60assert.throws(() => test.asDouble(null), numberExpected); 61assert.throws(() => test.asDouble(false), numberExpected); 62assert.throws(() => test.asDouble(''), numberExpected); 63assert.throws(() => test.asDouble('1'), numberExpected); 64assert.throws(() => test.asDouble({}), numberExpected); 65assert.throws(() => test.asDouble([]), numberExpected); 66assert.throws(() => test.asDouble(testSym), numberExpected); 67 68assert.strictEqual(test.asString(''), ''); 69assert.strictEqual(test.asString('test'), 'test'); 70assert.throws(() => test.asString(undefined), stringExpected); 71assert.throws(() => test.asString(null), stringExpected); 72assert.throws(() => test.asString(false), stringExpected); 73assert.throws(() => test.asString(1), stringExpected); 74assert.throws(() => test.asString(1.1), stringExpected); 75assert.throws(() => test.asString(Number.NaN), stringExpected); 76assert.throws(() => test.asString({}), stringExpected); 77assert.throws(() => test.asString([]), stringExpected); 78assert.throws(() => test.asString(testSym), stringExpected); 79 80assert.strictEqual(test.toBool(true), true); 81assert.strictEqual(test.toBool(1), true); 82assert.strictEqual(test.toBool(-1), true); 83assert.strictEqual(test.toBool('true'), true); 84assert.strictEqual(test.toBool('false'), true); 85assert.strictEqual(test.toBool({}), true); 86assert.strictEqual(test.toBool([]), true); 87assert.strictEqual(test.toBool(testSym), true); 88assert.strictEqual(test.toBool(false), false); 89assert.strictEqual(test.toBool(undefined), false); 90assert.strictEqual(test.toBool(null), false); 91assert.strictEqual(test.toBool(0), false); 92assert.strictEqual(test.toBool(Number.NaN), false); 93assert.strictEqual(test.toBool(''), false); 94 95assert.strictEqual(test.toNumber(0), 0); 96assert.strictEqual(test.toNumber(1), 1); 97assert.strictEqual(test.toNumber(1.1), 1.1); 98assert.strictEqual(test.toNumber(-1), -1); 99assert.strictEqual(test.toNumber('0'), 0); 100assert.strictEqual(test.toNumber('1'), 1); 101assert.strictEqual(test.toNumber('1.1'), 1.1); 102assert.strictEqual(test.toNumber([]), 0); 103assert.strictEqual(test.toNumber(false), 0); 104assert.strictEqual(test.toNumber(null), 0); 105assert.strictEqual(test.toNumber(''), 0); 106assert.ok(Number.isNaN(test.toNumber(Number.NaN))); 107assert.ok(Number.isNaN(test.toNumber({}))); 108assert.ok(Number.isNaN(test.toNumber(undefined))); 109assert.throws(() => test.toNumber(testSym), TypeError); 110 111assert.deepStrictEqual({}, test.toObject({})); 112assert.deepStrictEqual({ 'test': 1 }, test.toObject({ 'test': 1 })); 113assert.deepStrictEqual([], test.toObject([])); 114assert.deepStrictEqual([ 1, 2, 3 ], test.toObject([ 1, 2, 3 ])); 115assert.deepStrictEqual(new Boolean(false), test.toObject(false)); 116assert.deepStrictEqual(new Boolean(true), test.toObject(true)); 117assert.deepStrictEqual(new String(''), test.toObject('')); 118assert.deepStrictEqual(new Number(0), test.toObject(0)); 119assert.deepStrictEqual(new Number(Number.NaN), test.toObject(Number.NaN)); 120assert.deepStrictEqual(new Object(testSym), test.toObject(testSym)); 121assert.notDeepStrictEqual(test.toObject(false), false); 122assert.notDeepStrictEqual(test.toObject(true), true); 123assert.notDeepStrictEqual(test.toObject(''), ''); 124assert.notDeepStrictEqual(test.toObject(0), 0); 125assert.ok(!Number.isNaN(test.toObject(Number.NaN))); 126 127assert.strictEqual(test.toString(''), ''); 128assert.strictEqual(test.toString('test'), 'test'); 129assert.strictEqual(test.toString(undefined), 'undefined'); 130assert.strictEqual(test.toString(null), 'null'); 131assert.strictEqual(test.toString(false), 'false'); 132assert.strictEqual(test.toString(true), 'true'); 133assert.strictEqual(test.toString(0), '0'); 134assert.strictEqual(test.toString(1.1), '1.1'); 135assert.strictEqual(test.toString(Number.NaN), 'NaN'); 136assert.strictEqual(test.toString({}), '[object Object]'); 137assert.strictEqual(test.toString({ toString: () => 'test' }), 'test'); 138assert.strictEqual(test.toString([]), ''); 139assert.strictEqual(test.toString([ 1, 2, 3 ]), '1,2,3'); 140assert.throws(() => test.toString(testSym), TypeError); 141 142assert.deepStrictEqual(test.testNull.getValueBool(), { 143 envIsNull: 'Invalid argument', 144 valueIsNull: 'Invalid argument', 145 resultIsNull: 'Invalid argument', 146 inputTypeCheck: 'A boolean was expected' 147}); 148 149assert.deepStrictEqual(test.testNull.getValueInt32(), { 150 envIsNull: 'Invalid argument', 151 valueIsNull: 'Invalid argument', 152 resultIsNull: 'Invalid argument', 153 inputTypeCheck: 'A number was expected' 154}); 155 156assert.deepStrictEqual(test.testNull.getValueUint32(), { 157 envIsNull: 'Invalid argument', 158 valueIsNull: 'Invalid argument', 159 resultIsNull: 'Invalid argument', 160 inputTypeCheck: 'A number was expected' 161}); 162 163assert.deepStrictEqual(test.testNull.getValueInt64(), { 164 envIsNull: 'Invalid argument', 165 valueIsNull: 'Invalid argument', 166 resultIsNull: 'Invalid argument', 167 inputTypeCheck: 'A number was expected' 168}); 169 170 171assert.deepStrictEqual(test.testNull.getValueDouble(), { 172 envIsNull: 'Invalid argument', 173 valueIsNull: 'Invalid argument', 174 resultIsNull: 'Invalid argument', 175 inputTypeCheck: 'A number was expected' 176}); 177 178assert.deepStrictEqual(test.testNull.coerceToBool(), { 179 envIsNull: 'Invalid argument', 180 valueIsNull: 'Invalid argument', 181 resultIsNull: 'Invalid argument', 182 inputTypeCheck: 'napi_ok' 183}); 184 185assert.deepStrictEqual(test.testNull.coerceToObject(), { 186 envIsNull: 'Invalid argument', 187 valueIsNull: 'Invalid argument', 188 resultIsNull: 'Invalid argument', 189 inputTypeCheck: 'napi_ok' 190}); 191 192assert.deepStrictEqual(test.testNull.coerceToString(), { 193 envIsNull: 'Invalid argument', 194 valueIsNull: 'Invalid argument', 195 resultIsNull: 'Invalid argument', 196 inputTypeCheck: 'napi_ok' 197}); 198 199assert.deepStrictEqual(test.testNull.getValueStringUtf8(), { 200 envIsNull: 'Invalid argument', 201 valueIsNull: 'Invalid argument', 202 wrongTypeIn: 'A string was expected', 203 bufAndOutLengthIsNull: 'Invalid argument' 204}); 205 206assert.deepStrictEqual(test.testNull.getValueStringLatin1(), { 207 envIsNull: 'Invalid argument', 208 valueIsNull: 'Invalid argument', 209 wrongTypeIn: 'A string was expected', 210 bufAndOutLengthIsNull: 'Invalid argument' 211}); 212 213assert.deepStrictEqual(test.testNull.getValueStringUtf16(), { 214 envIsNull: 'Invalid argument', 215 valueIsNull: 'Invalid argument', 216 wrongTypeIn: 'A string was expected', 217 bufAndOutLengthIsNull: 'Invalid argument' 218}); 219