|
1 | 1 | import { readdir, readFile } from "node:fs/promises";
|
2 | 2 | import { resolve } from "node:path";
|
3 |
| -import { describe, test, expect } from "vitest"; |
| 3 | +import { describe, test, expect, beforeEach } from "vitest"; |
4 | 4 | import { VFileMessage } from "vfile-message";
|
5 |
| -import { rejson, rejsonStringify } from "./index.js"; |
| 5 | +import { |
| 6 | + fromJson, |
| 7 | + toJson, |
| 8 | + rejson, |
| 9 | + rejsonStringify, |
| 10 | + jsonObjectHas, |
| 11 | + jsonTypeOf, |
| 12 | + jsonArrayIter, |
| 13 | + jsonObjectKeys, |
| 14 | + jsonObjectValues, |
| 15 | + jsonObjectEntries |
| 16 | +} from "./index.js"; |
| 17 | + |
| 18 | +/** |
| 19 | + * @import { |
| 20 | + * JsonArrayNode, |
| 21 | + * JsonBooleanNode, |
| 22 | + * JsonNode, |
| 23 | + * JsonNullNode, |
| 24 | + * JsonNumberNode, |
| 25 | + * JsonObjectNode, |
| 26 | + * JsonStringNode |
| 27 | + * } from "./index.js" |
| 28 | + */ |
6 | 29 |
|
7 | 30 |
|
8 | 31 | describe("jsonast-util", async () => {
|
@@ -36,4 +59,154 @@ describe("jsonast-util", async () => {
|
36 | 59 | });
|
37 | 60 | }
|
38 | 61 | }
|
| 62 | + |
| 63 | + describe("object has property", () => { |
| 64 | + /** @type JsonNode */ |
| 65 | + let subject; |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + subject = fromJson(`{ |
| 69 | + "foo": 42 |
| 70 | + }`); |
| 71 | + }); |
| 72 | + |
| 73 | + test("true", () => { |
| 74 | + expect(jsonObjectHas("foo", subject)).to.eql(true); |
| 75 | + }); |
| 76 | + |
| 77 | + test("false", () => { |
| 78 | + expect(jsonObjectHas("bar", subject)).to.eql(false); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("typeOf", () => { |
| 83 | + test("null", () => { |
| 84 | + const subject = fromJson(`null`); |
| 85 | + if (jsonTypeOf(subject, "null")) { |
| 86 | + /** @type JsonNullNode */ |
| 87 | + const _typeCheck = subject; |
| 88 | + } else { |
| 89 | + expect.fail(); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + test("true", () => { |
| 94 | + const subject = fromJson(`true`); |
| 95 | + if (jsonTypeOf(subject, "boolean")) { |
| 96 | + /** @type JsonBooleanNode */ |
| 97 | + const _typeCheck = subject; |
| 98 | + } else { |
| 99 | + expect.fail(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + test("false", () => { |
| 104 | + const subject = fromJson(`false`); |
| 105 | + if (jsonTypeOf(subject, "boolean")) { |
| 106 | + /** @type JsonBooleanNode */ |
| 107 | + const _typeCheck = subject; |
| 108 | + } else { |
| 109 | + expect.fail(); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + test("number", () => { |
| 114 | + const subject = fromJson(`42`); |
| 115 | + if (jsonTypeOf(subject, "number")) { |
| 116 | + /** @type JsonNumberNode */ |
| 117 | + const _typeCheck = subject; |
| 118 | + } else { |
| 119 | + expect.fail(); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + test("string", () => { |
| 124 | + const subject = fromJson(`"foo"`); |
| 125 | + if (jsonTypeOf(subject, "string")) { |
| 126 | + /** @type JsonStringNode */ |
| 127 | + const _typeCheck = subject; |
| 128 | + } else { |
| 129 | + expect.fail(); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + test("array", () => { |
| 134 | + const subject = fromJson(`["foo", 42]`); |
| 135 | + if (jsonTypeOf(subject, "array")) { |
| 136 | + /** @type JsonArrayNode */ |
| 137 | + const _typeCheck = subject; |
| 138 | + } else { |
| 139 | + expect.fail(); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + test("object", () => { |
| 144 | + const subject = fromJson(`{ "foo": 42 }`); |
| 145 | + if (jsonTypeOf(subject, "object")) { |
| 146 | + /** @type JsonObjectNode */ |
| 147 | + const _typeCheck = subject; |
| 148 | + } else { |
| 149 | + expect.fail(); |
| 150 | + } |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + test("iter", () => { |
| 155 | + const subject = fromJson(`[1, 2]`); |
| 156 | + |
| 157 | + const generator = jsonArrayIter(subject); |
| 158 | + |
| 159 | + const first = generator.next(); |
| 160 | + expect(toJson(first.value)).to.equal(`1`); |
| 161 | + const second = generator.next(); |
| 162 | + expect(toJson(second.value)).to.equal(`2`); |
| 163 | + expect((generator.next()).done).to.equal(true); |
| 164 | + }); |
| 165 | + |
| 166 | + test("keys", () => { |
| 167 | + const subject = fromJson(`{ |
| 168 | + "a": 1, |
| 169 | + "b": 2 |
| 170 | + }`); |
| 171 | + |
| 172 | + const generator = jsonObjectKeys(subject); |
| 173 | + |
| 174 | + expect(generator.next().value).to.equal("a"); |
| 175 | + expect(generator.next().value).to.equal("b"); |
| 176 | + expect(generator.next().done).to.equal(true); |
| 177 | + }); |
| 178 | + |
| 179 | + test("values", () => { |
| 180 | + const subject = fromJson(`{ |
| 181 | + "a": 1, |
| 182 | + "b": 2 |
| 183 | + }`); |
| 184 | + |
| 185 | + const generator = jsonObjectValues(subject); |
| 186 | + |
| 187 | + const first = generator.next(); |
| 188 | + expect(toJson(first.value)).to.equal(`1`); |
| 189 | + const second = generator.next(); |
| 190 | + expect(toJson(second.value)).to.equal(`2`); |
| 191 | + expect((generator.next()).done).to.equal(true); |
| 192 | + }); |
| 193 | + |
| 194 | + test("entries", () => { |
| 195 | + const subject = fromJson(`{ |
| 196 | + "a": 1, |
| 197 | + "b": 2 |
| 198 | + }`); |
| 199 | + |
| 200 | + const generator = jsonObjectEntries(subject); |
| 201 | + |
| 202 | + const a = /** @type [string, JsonNode] */ ((generator.next()).value); |
| 203 | + expect(a[0]).to.equal("a"); |
| 204 | + expect(toJson(a[1])).to.equal(`1`); |
| 205 | + |
| 206 | + const b = /** @type [string, JsonNode] */ ((generator.next()).value); |
| 207 | + expect(b[0]).to.equal("b"); |
| 208 | + expect(toJson(b[1])).to.equal(`2`); |
| 209 | + |
| 210 | + expect(generator.next().done).to.equal(true); |
| 211 | + }); |
39 | 212 | });
|
0 commit comments