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
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
2024-05-28 03:12:24 +00:00
|
|
|
import { validateBinaryLike } from "./_validate_binary_like.ts";
|
2023-09-22 09:47:24 +00:00
|
|
|
|
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,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type number",
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(undefined);
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type undefined",
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(null);
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type null",
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike({});
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type Object",
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(new class MyClass {}());
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type MyClass",
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
2023-12-17 20:16:38 +00:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
validateBinaryLike(Object.create(null));
|
|
|
|
},
|
|
|
|
TypeError,
|
2024-08-23 03:32:15 +00:00
|
|
|
"Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type object",
|
2023-12-17 20:16:38 +00:00
|
|
|
);
|
2023-09-22 09:47:24 +00:00
|
|
|
});
|