2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-22 09:47:24 +00:00
|
|
|
|
|
|
|
import { assertEquals, assertThrows } from "../assert/mod.ts";
|
|
|
|
import { validateBinaryLike } from "./_util.ts";
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("validateBinaryLike()", () => {
|
2023-09-22 09:47:24 +00:00
|
|
|
assertEquals(validateBinaryLike("hello"), new TextEncoder().encode("hello"));
|
|
|
|
assertEquals(
|
|
|
|
validateBinaryLike(new Uint8Array([1, 2, 3])),
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
validateBinaryLike(new Uint8Array([1, 2, 3]).buffer),
|
|
|
|
new Uint8Array([1, 2, 3]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-12-28 03:03:43 +00:00
|
|
|
Deno.test("validateBinaryLike() throws on invalid inputs", () => {
|
2023-09-22 09:47:24 +00:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(1);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type number.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(undefined);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type undefined.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(null);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type null.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike({});
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type Object.",
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(new class MyClass {}());
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type MyClass.",
|
|
|
|
);
|
2023-12-17 20:16:38 +00:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(Object.create(null));
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type object.",
|
|
|
|
);
|
2023-09-22 09:47:24 +00:00
|
|
|
});
|