2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-20 02:50:48 -04:00
|
|
|
import { readFile } from "./_fs_readFile.ts";
|
|
|
|
import * as path from "../../../path/mod.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { assert, assertEquals } from "../../../testing/asserts.ts";
|
2020-05-20 02:50:48 -04:00
|
|
|
|
2020-09-08 11:37:58 -04:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testData = path.resolve(moduleDir, "..", "testdata", "hello.txt");
|
2020-05-20 02:50:48 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("readFileSuccess", async function () {
|
2020-06-23 22:32:43 -04:00
|
|
|
const data: Uint8Array = await readFile(testData);
|
2020-05-20 02:50:48 -04:00
|
|
|
|
|
|
|
assert(data instanceof Uint8Array);
|
2020-06-23 22:32:43 -04:00
|
|
|
assertEquals(new TextDecoder().decode(data), "hello world");
|
2020-05-20 02:50:48 -04:00
|
|
|
});
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
Deno.test("readFileBinarySuccess", async function () {
|
|
|
|
const data: Uint8Array = await readFile(testData, "binary");
|
|
|
|
|
|
|
|
assert(data instanceof Uint8Array);
|
|
|
|
assertEquals(new TextDecoder().decode(data), "hello world");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("readFileBinaryObjectSuccess", async function () {
|
|
|
|
const data: Uint8Array = await readFile(testData, { encoding: "binary" });
|
|
|
|
|
|
|
|
assert(data instanceof Uint8Array);
|
|
|
|
assertEquals(new TextDecoder().decode(data), "hello world");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("readFileStringObjectSuccess", async function () {
|
|
|
|
const data: string = await readFile(testData, { encoding: "utf8" });
|
2020-05-20 02:50:48 -04:00
|
|
|
|
|
|
|
assertEquals(typeof data, "string");
|
2020-06-23 22:32:43 -04:00
|
|
|
assertEquals(data, "hello world");
|
2020-05-20 02:50:48 -04:00
|
|
|
});
|
|
|
|
|
2020-06-27 16:52:49 -04:00
|
|
|
Deno.test("readFileEncodeHexSuccess", async function () {
|
|
|
|
const data: string = await readFile(testData, { encoding: "hex" });
|
|
|
|
assertEquals(typeof data, "string");
|
|
|
|
assertEquals(data as string, "68656c6c6f20776f726c64");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("readFileEncodeBase64Success", async function () {
|
|
|
|
const data: string = await readFile(testData, { encoding: "base64" });
|
|
|
|
assertEquals(typeof data, "string");
|
|
|
|
assertEquals(data as string, "aGVsbG8gd29ybGQ=");
|
|
|
|
});
|
|
|
|
|
2020-06-23 22:32:43 -04:00
|
|
|
Deno.test("readFileStringSuccess", async function () {
|
|
|
|
const data: string = await readFile(testData, "utf8");
|
2020-05-20 02:50:48 -04:00
|
|
|
|
|
|
|
assertEquals(typeof data, "string");
|
2020-06-23 22:32:43 -04:00
|
|
|
assertEquals(data, "hello world");
|
2020-05-20 02:50:48 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("readFileError", async function () {
|
2020-05-20 02:50:48 -04:00
|
|
|
try {
|
|
|
|
await readFile("invalid-file", "utf8");
|
|
|
|
} catch (e) {
|
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
|
|
|
}
|
|
|
|
});
|