2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
|
import { unitTest, assert, assertEquals } from "./test_util.ts";
|
2018-11-04 13:05:02 -05:00
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
function testFirstArgument(arg1: any[], expectedSize: number): void {
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["bits"], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUnicodeStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileStringObjectFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new String("string object")], 13);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob()], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob(["bits"])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileEmptyFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File([], "world.txt")], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File(["bits"], "world.txt")], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileArrayBufferFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new ArrayBuffer(8)], 8);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileTypedArrayFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileVariousFileBits(): void {
|
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
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileNumberInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([12], 2);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileArrayInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([[1, 2, 3]], 5);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileObjectInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
// "[object Object]"
|
|
|
|
|
testFirstArgument([{}], 15);
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-19 15:36:18 -05:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
function testSecondArgument(arg2: any, expectedFileName: string): void {
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy", "dummy");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingSpecialCharacterInFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy/foo", "dummy:foo");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingNullFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(null, "null");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingNumberFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(1, "1");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function fileUsingEmptyStringFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("", "");
|
|
|
|
|
});
|