summaryrefslogtreecommitdiff
path: root/fs/read_json_test.ts
blob: 46ce1ec08b0cbdf0c25d5763edb85df347140618 (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
// 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");

test(async function readJsonFileNotExists() {
  const emptyJsonFile = path.join(testdataDir, "json_not_exists.json");

  await assertThrowsAsync(async () => {
    await readJson(emptyJsonFile);
  });
});

test(async function readEmptyJsonFile() {
  const emptyJsonFile = path.join(testdataDir, "json_empty.json");

  await assertThrowsAsync(async () => {
    await readJson(emptyJsonFile);
  });
});

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

  await assertThrowsAsync(async () => {
    await readJson(invalidJsonFile);
  });
});

test(async function readValidJsonFile() {
  const invalidJsonFile = path.join(testdataDir, "json_valid_array.json");

  const json = await readJson(invalidJsonFile);

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

test(async function readValidJsonFile() {
  const invalidJsonFile = path.join(testdataDir, "json_valid_obj.json");

  const json = await readJson(invalidJsonFile);

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

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

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

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

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

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

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

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

  const json = readJsonSync(invalidJsonFile);

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

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

  const json = readJsonSync(invalidJsonFile);

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