-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_json_test.ts
104 lines (75 loc) · 3 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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
assertEquals,
assertThrows,
assertThrowsAsync,
} from "https://deno.land/std@0.102.0/testing/asserts.ts";
import * as path from "https://deno.land/std@0.102.0/path/mod.ts";
import { readJson, readJsonSync } from "./read_json.ts";
const testdataDir = path.resolve("testdata");
Deno.test("readJsonFileNotExists", async function (): Promise<void> {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
await assertThrowsAsync(async (): Promise<void> => {
await readJson(emptyJsonFile);
});
});
Deno.test("readEmptyJsonFile", async function (): Promise<void> {
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
await assertThrowsAsync(async (): Promise<void> => {
await readJson(emptyJsonFile);
});
});
Deno.test("readInvalidJsonFile", async function (): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
await assertThrowsAsync(async (): Promise<void> => {
await readJson(invalidJsonFile);
});
});
Deno.test("readValidArrayJsonFile", async function (): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = await readJson(invalidJsonFile);
assertEquals(json, ["1", "2", "3"]);
});
Deno.test("readValidObjJsonFile", async function (): Promise<void> {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = await readJson(invalidJsonFile);
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test("readValidObjJsonFileWithRelativePath", async function (): Promise<
void
> {
const json = await readJson("./testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test("readJsonFileNotExistsSync", function (): void {
const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");
assertThrows((): void => {
readJsonSync(emptyJsonFile);
});
});
Deno.test("readEmptyJsonFileSync", function (): void {
const emptyJsonFile = path.join(testdataDir, "json_empty.json");
assertThrows((): void => {
readJsonSync(emptyJsonFile);
});
});
Deno.test("readInvalidJsonFile", function (): void {
const invalidJsonFile = path.join(testdataDir, "json_invalid.json");
assertThrows((): void => {
readJsonSync(invalidJsonFile);
});
});
Deno.test("readValidArrayJsonFileSync", function (): void {
const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");
const json = readJsonSync(invalidJsonFile);
assertEquals(json, ["1", "2", "3"]);
});
Deno.test("readValidObjJsonFileSync", function (): void {
const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");
const json = readJsonSync(invalidJsonFile);
assertEquals(json, { key1: "value1", key2: "value2" });
});
Deno.test("readValidObjJsonFileSyncWithRelativePath", function (): void {
const json = readJsonSync("./testdata/json_valid_obj.json");
assertEquals(json, { key1: "value1", key2: "value2" });
});