2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-13 12:06:40 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrowsAsync,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrows,
|
2019-03-13 12:06:40 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-13 12:06:40 -04:00
|
|
|
import { readJson, readJsonSync } from "./read_json.ts";
|
|
|
|
|
|
|
|
const testdataDir = path.resolve("fs", "testdata");
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readJsonFileNotExists", async function (): Promise<void> {
|
2019-03-13 12:06:40 -04:00
|
|
|
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await readJson(emptyJsonFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readEmptyJsonFile", async function (): Promise<void> {
|
2019-03-13 12:06:40 -04:00
|
|
|
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await readJson(emptyJsonFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readInvalidJsonFile", async function (): Promise<void> {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await readJson(invalidJsonFile);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidArrayJsonFile", async function (): Promise<void> {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
|
|
|
|
|
|
|
|
const json = await readJson(invalidJsonFile);
|
|
|
|
|
|
|
|
assertEquals(json, ["1", "2", "3"]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidObjJsonFile", async function (): Promise<void> {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
|
|
|
|
|
|
|
|
const json = await readJson(invalidJsonFile);
|
|
|
|
|
|
|
|
assertEquals(json, { key1: "value1", key2: "value2" });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidObjJsonFileWithRelativePath", async function (): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-03-19 13:23:22 -04:00
|
|
|
const json = await readJson("./fs/testdata/json_valid_obj.json");
|
|
|
|
|
|
|
|
assertEquals(json, { key1: "value1", key2: "value2" });
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readJsonFileNotExistsSync", function (): void {
|
2019-03-13 12:06:40 -04:00
|
|
|
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
readJsonSync(emptyJsonFile);
|
|
|
|
});
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readEmptyJsonFileSync", function (): void {
|
2019-03-13 12:06:40 -04:00
|
|
|
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
readJsonSync(emptyJsonFile);
|
|
|
|
});
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readInvalidJsonFile", function (): void {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
|
|
|
|
|
2019-11-13 13:42:34 -05:00
|
|
|
assertThrows((): void => {
|
|
|
|
readJsonSync(invalidJsonFile);
|
|
|
|
});
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidArrayJsonFileSync", function (): void {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
|
|
|
|
|
|
|
|
const json = readJsonSync(invalidJsonFile);
|
|
|
|
|
|
|
|
assertEquals(json, ["1", "2", "3"]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidObjJsonFileSync", function (): void {
|
2019-03-13 12:06:40 -04:00
|
|
|
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
|
|
|
|
|
|
|
|
const json = readJsonSync(invalidJsonFile);
|
|
|
|
|
|
|
|
assertEquals(json, { key1: "value1", key2: "value2" });
|
|
|
|
});
|
2019-03-19 13:23:22 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("readValidObjJsonFileSyncWithRelativePath", function (): void {
|
2019-03-19 13:23:22 -04:00
|
|
|
const json = readJsonSync("./fs/testdata/json_valid_obj.json");
|
|
|
|
|
|
|
|
assertEquals(json, { key1: "value1", key2: "value2" });
|
|
|
|
});
|