summaryrefslogtreecommitdiff
path: root/cli/js/tests/file_test.ts
diff options
context:
space:
mode:
authorBartek Iwańczuk <biwanczuk@gmail.com>2020-03-10 01:06:47 +0100
committerGitHub <noreply@github.com>2020-03-10 01:06:47 +0100
commit68119e1d7ed23421ccdcba20532ebe9ae3df9f18 (patch)
tree455dd170024112e3388adc27ebebb119b0ecda38 /cli/js/tests/file_test.ts
parentdad8036766dca3417b79858b9a04d90447f88605 (diff)
reorg: move js runtime tests to cli/js/tests/ (#4250)
All Deno runtime test files were moved to cli/js/tests/ directory. It makes a clear distinction that cli/js/tests/ contains code that is run under Deno runtime as opposed to code in cli/js/ which is used to create bundle and snapshot with "deno_typescript".
Diffstat (limited to 'cli/js/tests/file_test.ts')
-rw-r--r--cli/js/tests/file_test.ts105
1 files changed, 105 insertions, 0 deletions
diff --git a/cli/js/tests/file_test.ts b/cli/js/tests/file_test.ts
new file mode 100644
index 000000000..1a7a5f88b
--- /dev/null
+++ b/cli/js/tests/file_test.ts
@@ -0,0 +1,105 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { unitTest, assert, assertEquals } from "./test_util.ts";
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+function testFirstArgument(arg1: any[], expectedSize: number): void {
+ const file = new File(arg1, "name");
+ assert(file instanceof File);
+ assertEquals(file.name, "name");
+ assertEquals(file.size, expectedSize);
+ assertEquals(file.type, "");
+}
+
+unitTest(function fileEmptyFileBits(): void {
+ testFirstArgument([], 0);
+});
+
+unitTest(function fileStringFileBits(): void {
+ testFirstArgument(["bits"], 4);
+});
+
+unitTest(function fileUnicodeStringFileBits(): void {
+ testFirstArgument(["𝓽𝓮𝔁𝓽"], 16);
+});
+
+unitTest(function fileStringObjectFileBits(): void {
+ testFirstArgument([new String("string object")], 13);
+});
+
+unitTest(function fileEmptyBlobFileBits(): void {
+ testFirstArgument([new Blob()], 0);
+});
+
+unitTest(function fileBlobFileBits(): void {
+ testFirstArgument([new Blob(["bits"])], 4);
+});
+
+unitTest(function fileEmptyFileFileBits(): void {
+ testFirstArgument([new File([], "world.txt")], 0);
+});
+
+unitTest(function fileFileFileBits(): void {
+ testFirstArgument([new File(["bits"], "world.txt")], 4);
+});
+
+unitTest(function fileArrayBufferFileBits(): void {
+ testFirstArgument([new ArrayBuffer(8)], 8);
+});
+
+unitTest(function fileTypedArrayFileBits(): void {
+ testFirstArgument([new Uint8Array([0x50, 0x41, 0x53, 0x53])], 4);
+});
+
+unitTest(function fileVariousFileBits(): void {
+ testFirstArgument(
+ [
+ "bits",
+ new Blob(["bits"]),
+ new Blob(),
+ new Uint8Array([0x50, 0x41]),
+ new Uint16Array([0x5353]),
+ new Uint32Array([0x53534150])
+ ],
+ 16
+ );
+});
+
+unitTest(function fileNumberInFileBits(): void {
+ testFirstArgument([12], 2);
+});
+
+unitTest(function fileArrayInFileBits(): void {
+ testFirstArgument([[1, 2, 3]], 5);
+});
+
+unitTest(function fileObjectInFileBits(): void {
+ // "[object Object]"
+ testFirstArgument([{}], 15);
+});
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+function testSecondArgument(arg2: any, expectedFileName: string): void {
+ const file = new File(["bits"], arg2);
+ assert(file instanceof File);
+ assertEquals(file.name, expectedFileName);
+}
+
+unitTest(function fileUsingFileName(): void {
+ testSecondArgument("dummy", "dummy");
+});
+
+unitTest(function fileUsingSpecialCharacterInFileName(): void {
+ testSecondArgument("dummy/foo", "dummy:foo");
+});
+
+unitTest(function fileUsingNullFileName(): void {
+ testSecondArgument(null, "null");
+});
+
+unitTest(function fileUsingNumberFileName(): void {
+ testSecondArgument(1, "1");
+});
+
+unitTest(function fileUsingEmptyStringFileName(): void {
+ testSecondArgument("", "");
+});