summaryrefslogtreecommitdiff
path: root/std/fs/read_json_test.ts
blob: c89006ec159739497528f3b795c1a68969a353b0 (plain)
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
109
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { test } from "../testing/mod.ts";
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");

test(async function readJsonFileNotExists(): Promise<void> {
  const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");

  await assertThrowsAsync(
    async (): Promise<void> => {
      await readJson(emptyJsonFile);
    }
  );
});

test(async function readEmptyJsonFile(): Promise<void> {
  const emptyJsonFile = path.join(testdataDir, "json_empty.json");

  await assertThrowsAsync(
    async (): Promise<void> => {
      await readJson(emptyJsonFile);
    }
  );
});

test(async function readInvalidJsonFile(): Promise<void> {
  const invalidJsonFile = path.join(testdataDir, "json_invalid.json");

  await assertThrowsAsync(
    async (): Promise<void> => {
      await readJson(invalidJsonFile);
    }
  );
});

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"]);
});

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" });
});

test(async function readValidObjJsonFileWithRelativePath(): Promise<void> {
  const json = await readJson("./fs/testdata/json_valid_obj.json");

  assertEquals(json, { key1: "value1", key2: "value2" });
});

test(function readJsonFileNotExistsSync(): void {
  const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");

  assertThrows((): void => {
    readJsonSync(emptyJsonFile);
  });
});

test(function readEmptyJsonFileSync(): void {
  const emptyJsonFile = path.join(testdataDir, "json_empty.json");

  assertThrows((): void => {
    readJsonSync(emptyJsonFile);
  });
});

test(function readInvalidJsonFile(): void {
  const invalidJsonFile = path.join(testdataDir, "json_invalid.json");

  assertThrows((): void => {
    readJsonSync(invalidJsonFile);
  });
});

test(function readValidArrayJsonFileSync(): void {
  const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");

  const json = readJsonSync(invalidJsonFile);

  assertEquals(json, ["1", "2", "3"]);
});

test(function readValidObjJsonFileSync(): void {
  const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");

  const json = readJsonSync(invalidJsonFile);

  assertEquals(json, { key1: "value1", key2: "value2" });
});

test(function readValidObjJsonFileSyncWithRelativePath(): void {
  const json = readJsonSync("./fs/testdata/json_valid_obj.json");

  assertEquals(json, { key1: "value1", key2: "value2" });
});