diff options
author | 2023-01-21 03:12:59 -0800 | |
---|---|---|
committer | 2023-01-21 03:12:59 -0800 | |
commit | d955bfe50f7aa15aca9df3bf0a40fea26a132381 (patch) | |
tree | 2433e2b9dfc4bf712903417008f9b0c5b3900d69 /test/bun.js/buffer.test.js | |
parent | b8648adf873758a83911d3d28c94255d8c3fdd3c (diff) | |
download | bun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.tar.gz bun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.tar.zst bun-d955bfe50f7aa15aca9df3bf0a40fea26a132381.zip |
[buffer] Make Buffer.from pass more tests
Diffstat (limited to '')
-rw-r--r-- | test/bun.js/buffer.test.js | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/test/bun.js/buffer.test.js b/test/bun.js/buffer.test.js index 64c6cc41f..20fcddfa6 100644 --- a/test/bun.js/buffer.test.js +++ b/test/bun.js/buffer.test.js @@ -122,7 +122,8 @@ it("Buffer.from", () => { expect(Buffer.from([254], "utf16").join(",")).toBe("254"); expect(Buffer.isBuffer(Buffer.from([254], "utf16"))).toBe(true); - expect(Buffer.from(123).join(",")).toBe(Uint8Array.from(123).join(",")); + expect(() => Buffer.from(123).join(",")).toThrow(); + expect(Buffer.from({ length: 124 }).join(",")).toBe( Uint8Array.from({ length: 124 }).join(","), ); @@ -718,3 +719,61 @@ it("transcode", () => { // This is a masqueradesAsUndefined function expect(() => BufferModule.transcode()).toThrow("Not implemented"); }); + +it("Buffer.from (Node.js test/test-buffer-from.js)", () => { + const checkString = "test"; + + const check = Buffer.from(checkString); + + class MyString extends String { + constructor() { + super(checkString); + } + } + + class MyPrimitive { + [Symbol.toPrimitive]() { + return checkString; + } + } + + class MyBadPrimitive { + [Symbol.toPrimitive]() { + return 1; + } + } + + expect(Buffer.from(new String(checkString))).toStrictEqual(check); + expect(Buffer.from(new MyString())).toStrictEqual(check); + expect(Buffer.from(new MyPrimitive())).toStrictEqual(check); + + [ + {}, + new Boolean(true), + { + valueOf() { + return null; + }, + }, + { + valueOf() { + return undefined; + }, + }, + { valueOf: null }, + Object.create(null), + new Number(true), + new MyBadPrimitive(), + Symbol(), + 5n, + (one, two, three) => {}, + undefined, + null, + ].forEach((input) => { + expect(() => Buffer.from(input)).toThrow(); + expect(() => Buffer.from(input, "hex")).toThrow(); + }); + + expect(() => Buffer.allocUnsafe(10)).not.toThrow(); // Should not throw. + expect(() => Buffer.from("deadbeaf", "hex")).not.toThrow(); // Should not throw. +}); |