/
read_json_test.ts
108 lines (79 loc) · 2.94 KB
/
read_json_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
assertEquals,
assertThrowsAsync,
assertThrows,
} from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import { readJson, readJsonSync } from "./read_json.ts";
const testdataDir = path.resolve("fs", "testdata");
Deno.test(async function readJsonFileNotExists(): Promise<void> {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
await assertThrowsAsync(
async (): Promise<void> => {
await readJson(emptyJsonFile);
}
);
});
Deno.test(async function readEmptyJsonFile(): Promise<void> {
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
await assertThrowsAsync(
async (): Promise<void> => {
await readJson(emptyJsonFile);
}
);
});
Deno.test(async function readInvalidJsonFile(): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
await assertThrowsAsync(
async (): Promise<void> => {
await readJson(invalidJsonFile);
}
);
});
Deno.test(async function readValidArrayJsonFile(): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = await readJson(invalidJsonFile);
assertEquals(json, ["1", "2", "3"]);
});
Deno.test(async function readValidObjJsonFile(): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = await readJson(invalidJsonFile);
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test(async function readValidObjJsonFileWithRelativePath(): Promise<void> {
const json = await readJson("./fs/testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test(function readJsonFileNotExistsSync(): void {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
assertThrows((): void => {
readJsonSync(emptyJsonFile);
});
});
Deno.test(function readEmptyJsonFileSync(): void {
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
assertThrows((): void => {
readJsonSync(emptyJsonFile);
});
});
Deno.test(function readInvalidJsonFile(): void {
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
assertThrows((): void => {
readJsonSync(invalidJsonFile);
});
});
Deno.test(function readValidArrayJsonFileSync(): void {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = readJsonSync(invalidJsonFile);
assertEquals(json, ["1", "2", "3"]);
});
Deno.test(function readValidObjJsonFileSync(): void {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = readJsonSync(invalidJsonFile);
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test(function readValidObjJsonFileSyncWithRelativePath(): void {
const json = readJsonSync("./fs/testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});