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 { test } from "../testing/mod.ts";
|
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrowsAsync,
|
|
|
|
assertThrows
|
|
|
|
} 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");
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readJsonFileNotExists(): 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);
|
|
|
|
}
|
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readEmptyJsonFile(): 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);
|
|
|
|
}
|
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readInvalidJsonFile(): 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);
|
|
|
|
}
|
|
|
|
);
|
2019-03-13 12:06:40 -04:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readValidArrayJsonFile(): 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"]);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readValidObjJsonFile(): 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" });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function readValidObjJsonFileWithRelativePath(): 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" });
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readJsonFileNotExistsSync(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readEmptyJsonFileSync(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readInvalidJsonFile(): 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readValidArrayJsonFileSync(): 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"]);
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readValidObjJsonFileSync(): 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
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(function readValidObjJsonFileSyncWithRelativePath(): void {
|
2019-03-19 13:23:22 -04:00
|
|
|
const json = readJsonSync("./fs/testdata/json_valid_obj.json");
|
|
|
|
|
|
|
|
assertEquals(json, { key1: "value1", key2: "value2" });
|
|
|
|
});
|