2019-03-13 12:06:40 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { test } from "../testing/mod.ts";
|
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrowsAsync,
|
|
|
|
assertThrows
|
|
|
|
} from "../testing/asserts.ts";
|
|
|
|
import { readJson, readJsonSync } from "./read_json.ts";
|
|
|
|
import * as path from "./path/mod.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-09-11 23:31:23 -04: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-09-11 23:31:23 -04: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-09-11 23:31:23 -04: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" });
|
|
|
|
});
|