2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
|
import { test, 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
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileEmptyFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["bits"], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUnicodeStringFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileStringObjectFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new String("string object")], 13);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileEmptyBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob()], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileBlobFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Blob(["bits"])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileEmptyFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File([], "world.txt")], 0);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileFileFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new File(["bits"], "world.txt")], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileArrayBufferFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new ArrayBuffer(8)], 8);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileTypedArrayFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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]),
|
|
|
|
|
new Uint32Array([0x53534150])
|
|
|
|
|
],
|
|
|
|
|
16
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileNumberInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([12], 2);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileArrayInFileBits(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testFirstArgument([[1, 2, 3]], 5);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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
|
|
|
|
}
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUsingFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy", "dummy");
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUsingSpecialCharacterInFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("dummy/foo", "dummy:foo");
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUsingNullFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(null, "null");
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUsingNumberFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument(1, "1");
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function fileUsingEmptyStringFileName(): void {
|
2018-11-04 13:05:02 -05:00
|
|
|
|
testSecondArgument("", "");
|
|
|
|
|
});
|