2024-01-01 14:58:21 -05:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
|
import { assert, assertEquals } from "./test_util.ts";
|
2018-11-04 13:05:02 -05:00
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2021-08-05 07:08:58 -04:00
|
|
|
|
function testFirstArgument(arg1: any[], expectedSize: number) {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
const file = new File(arg1, "name");
|
|
|
|
|
assert(file instanceof File);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(file.name, "name");
|
|
|
|
|
assertEquals(file.size, expectedSize);
|
|
|
|
|
assertEquals(file.type, "");
|
2018-11-04 13:05:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileStringFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["bits"], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUnicodeStringFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileStringObjectFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new String("string object")], 13);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyBlobFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob()], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileBlobFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob(["bits"])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileEmptyFileFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File([], "world.txt")], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileFileFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File(["bits"], "world.txt")], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileArrayBufferFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new ArrayBuffer(8)], 8);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileTypedArrayFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileVariousFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(
|
|
|
|
|
[
|
|
|
|
|
"bits",
|
|
|
|
|
new Blob(["bits"]),
|
|
|
|
|
new Blob(),
|
|
|
|
|
new Uint8Array([0x50, 0x41]),
|
|
|
|
|
new Uint16Array([0x5353]),
|
2020-03-28 13:03:49 -04:00
|
|
|
|
new Uint32Array([0x53534150]),
|
2018-11-04 13:05:02 -05:00
|
|
|
|
],
|
2020-07-14 15:24:17 -04:00
|
|
|
|
16,
|
2018-11-04 13:05:02 -05:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileNumberInFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([12], 2);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileArrayInFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([[1, 2, 3]], 5);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileObjectInFileBits() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
// "[object Object]"
|
|
|
|
|
testFirstArgument([{}], 15);
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2021-08-05 07:08:58 -04:00
|
|
|
|
function testSecondArgument(arg2: any, expectedFileName: string) {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
const file = new File(["bits"], arg2);
|
|
|
|
|
assert(file instanceof File);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(file.name, expectedFileName);
|
2018-11-04 13:05:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy", "dummy");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingNullFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(null, "null");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingNumberFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(1, "1");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function fileUsingEmptyStringFileName() {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("", "");
|
|
|
|
|
});
|
2023-09-14 01:06:58 -04:00
|
|
|
|
|
|
|
|
|
Deno.test(function inspectFile() {
|
|
|
|
|
assertEquals(
|
|
|
|
|
Deno.inspect(new File([], "file-name.txt")),
|
|
|
|
|
`File { name: "file-name.txt", size: 0, type: "" }`,
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
Deno.inspect(new File([], "file-name.txt", { type: "text/plain" })),
|
|
|
|
|
`File { name: "file-name.txt", size: 0, type: "text/plain" }`,
|
|
|
|
|
);
|
|
|
|
|
});
|