Skip to content

Commit 2f84c7e

Browse files
committed
Add tests for unparsing
1 parent bffc32d commit 2f84c7e

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"devDependencies": {
1818
"@types/node": "^22.5.5",
19+
"strip-ansi": "^7.1.0",
1920
"typescript": "^5.6.2"
2021
},
2122
"dependencies": {
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import either from '@matt.kantor/either'
2+
import stripAnsi from 'strip-ansi'
3+
import { testCases } from '../../test-utilities.test.js'
4+
import { type Atom, type Molecule } from '../parsing.js'
5+
import {
6+
inlinePlz,
7+
prettyJson,
8+
prettyPlz,
9+
type Notation,
10+
} from '../unparsing.js'
11+
12+
const unparser = (notation: Notation) => (value: Atom | Molecule) =>
13+
either.map(
14+
typeof value === 'string'
15+
? notation.atom(value)
16+
: notation.molecule(value, notation),
17+
stripAnsi, // terminal styling is not currently tested
18+
)
19+
20+
testCases(
21+
unparser(inlinePlz),
22+
input => `unparsing \`${JSON.stringify(input)}\``,
23+
)('inline plz', [
24+
[{}, either.makeRight('{}')],
25+
['a', either.makeRight('a')],
26+
['Hello, world!', either.makeRight('"Hello, world!"')],
27+
[{ 0: 'a' }, either.makeRight('{ a }')],
28+
[{ 1: 'a' }, either.makeRight('{ 1: a }')],
29+
[
30+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
31+
either.makeRight('{ a, b, 3: c, somethingElse: d }'),
32+
],
33+
[{ a: { b: { c: 'd' } } }, either.makeRight('{ a: { b: { c: d } } }')],
34+
[
35+
{
36+
identity: {
37+
0: '@function',
38+
parameter: 'a',
39+
body: { 0: '@lookup', 1: 'a' },
40+
},
41+
test: {
42+
0: '@apply',
43+
function: { 0: '@lookup', 1: 'identity' },
44+
argument: 'it works!',
45+
},
46+
},
47+
either.makeRight('{ identity: a => :a, test: :identity("it works!") }'),
48+
],
49+
])
50+
51+
testCases(
52+
unparser(prettyPlz),
53+
input => `unparsing \`${JSON.stringify(input)}\``,
54+
)('pretty plz', [
55+
[{}, either.makeRight('{}')],
56+
['a', either.makeRight('a')],
57+
['Hello, world!', either.makeRight('"Hello, world!"')],
58+
[{ 0: 'a' }, either.makeRight('{\n a\n}')],
59+
[{ 1: 'a' }, either.makeRight('{\n 1: a\n}')],
60+
[
61+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
62+
either.makeRight('{\n a\n b\n 3: c\n somethingElse: d\n}'),
63+
],
64+
[
65+
{ a: { b: { c: 'd' } } },
66+
either.makeRight('{\n a: {\n b: {\n c: d\n }\n }\n}'),
67+
],
68+
[
69+
{
70+
identity: {
71+
0: '@function',
72+
parameter: 'a',
73+
body: { 0: '@lookup', 1: 'a' },
74+
},
75+
test: {
76+
0: '@apply',
77+
function: { 0: '@lookup', 1: 'identity' },
78+
argument: 'it works!',
79+
},
80+
},
81+
either.makeRight(
82+
'{\n identity: a => :a\n test: :identity("it works!")\n}',
83+
),
84+
],
85+
])
86+
87+
testCases(
88+
unparser(prettyJson),
89+
input => `unparsing \`${JSON.stringify(input)}\``,
90+
)('pretty JSON', [
91+
[{}, either.makeRight('{}')],
92+
['a', either.makeRight('"a"')],
93+
['Hello, world!', either.makeRight('"Hello, world!"')],
94+
[{ 0: 'a' }, either.makeRight('{\n "0": "a"\n}')],
95+
[{ 1: 'a' }, either.makeRight('{\n "1": "a"\n}')],
96+
[
97+
{ 0: 'a', 1: 'b', 3: 'c', somethingElse: 'd' },
98+
either.makeRight(
99+
'{\n "0": "a",\n "1": "b",\n "3": "c",\n "somethingElse": "d"\n}',
100+
),
101+
],
102+
[
103+
{ a: { b: { c: 'd' } } },
104+
either.makeRight('{\n "a": {\n "b": {\n "c": "d"\n }\n }\n}'),
105+
],
106+
[
107+
{
108+
identity: {
109+
0: '@function',
110+
parameter: 'a',
111+
body: { 0: '@lookup', 1: 'a' },
112+
},
113+
test: {
114+
0: '@apply',
115+
function: { 0: '@lookup', 1: 'identity' },
116+
argument: 'it works!',
117+
},
118+
},
119+
either.makeRight(
120+
'{\n "identity": {\n "0": "@function",\n "parameter": "a",\n "body": {\n "0": "@lookup",\n "1": "a"\n }\n },\n "test": {\n "0": "@apply",\n "function": {\n "0": "@lookup",\n "1": "identity"\n },\n "argument": "it works!"\n }\n}',
121+
),
122+
],
123+
])

0 commit comments

Comments
 (0)