From b9cf3e02908ee31464998e66570e4551d941eae3 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 25 Jan 2022 14:52:23 +0800 Subject: [PATCH] squash: add tests for stringable objects --- .../test-fs-promises-file-handle-write.js | 7 ++++++- .../test-fs-write-sync-optional-params.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-promises-file-handle-write.js b/test/parallel/test-fs-promises-file-handle-write.js index 3c25842d8bf9cc..b558335c9f6c34 100644 --- a/test/parallel/test-fs-promises-file-handle-write.js +++ b/test/parallel/test-fs-promises-file-handle-write.js @@ -53,7 +53,12 @@ async function validateNonUint8ArrayWrite() { async function validateNonStringValuesWrite() { const filePathForHandle = path.resolve(tmpDir, 'tmp-non-string-write.txt'); const fileHandle = await open(filePathForHandle, 'w+'); - const nonStringValues = [123, {}, new Map()]; + const nonStringValues = [ + 123, {}, new Map(), + new String('notPrimitive'), + { toString() { return 'amObject'; } }, + { [Symbol.toPrimitive]: (hint) => 'amObject' }, + ]; for (const nonStringValue of nonStringValues) { await assert.rejects( fileHandle.write(nonStringValue), diff --git a/test/parallel/test-fs-write-sync-optional-params.js b/test/parallel/test-fs-write-sync-optional-params.js index 111ce5387b124b..c8659610070819 100644 --- a/test/parallel/test-fs-write-sync-optional-params.js +++ b/test/parallel/test-fs-write-sync-optional-params.js @@ -71,6 +71,23 @@ const buffer = Buffer.from('zyx'); 'It must be >= 0 && <= 9007199254740991. Received -1' }); + // Test if object is not interpreted as string + for (const buffer of [ + {}, + new Date(), + new String('notPrimitive'), + { toString() { return 'amObject'; } }, + { [Symbol.toPrimitive]: (hint) => 'amObject' }, + ]) { + assert.throws(() => { + fs.writeSync(fd, buffer); + }, { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: /^The "buffer" argument must be of type string or an instance of Buffer, TypedArray, or DataView/ + }); + } + fs.closeSync(fd); for (const params of [