Skip to content

Commit f76d3ec

Browse files
committed
test(representation): add test suite for JSON String rep
1 parent 72d4861 commit f76d3ec

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/representation/json-string.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const to = (jsonPointer) => {
1+
export const to = (jsonPointer) => {
22
return JSON.stringify(jsonPointer);
33
};
44

5-
const from = (jsonString) => {
5+
export const from = (jsonString) => {
66
try {
7-
return JSON.parse(jsonString);
7+
return String(JSON.parse(jsonString));
88
} catch {
99
return jsonString;
1010
}

test/representation/json-string.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { assert } from 'chai';
2+
3+
import { JSONString } from '../../src/index.js';
4+
5+
describe('JSON String Representation (isomorphic behavior)', function () {
6+
it('should preserve identity under to() and from() (RFC 6901)', function () {
7+
const pointers = [
8+
'',
9+
'/foo',
10+
'/foo/0',
11+
'/',
12+
'/a~1b',
13+
'/c%d',
14+
'/e^f',
15+
'/g|h',
16+
'/i\\j',
17+
'/k"l',
18+
'/ ',
19+
'/m~0n',
20+
];
21+
22+
pointers.forEach((pointer) => {
23+
const jsonString = JSONString.to(pointer);
24+
const decodedPointer = JSONString.from(jsonString);
25+
assert.strictEqual(decodedPointer, pointer, `Failed for pointer: ${pointer}`);
26+
});
27+
});
28+
29+
context('edge cases', function () {
30+
specify('should handle empty string conversion', function () {
31+
assert.strictEqual(JSONString.to(''), '""');
32+
assert.strictEqual(JSONString.from('""'), '');
33+
});
34+
35+
specify('should handle special characters correctly', function () {
36+
assert.strictEqual(JSONString.to('/foo"bar'), '"/foo\\"bar"'); // escape quotes
37+
assert.strictEqual(JSONString.from('"/foo\\"bar"'), '/foo"bar');
38+
39+
assert.strictEqual(JSONString.to('/back\\slash'), '"/back\\\\slash"'); // escape backslash
40+
assert.strictEqual(JSONString.from('"/back\\\\slash"'), '/back\\slash');
41+
});
42+
43+
specify('should handle unicode characters correctly', function () {
44+
assert.strictEqual(JSONString.to('/😀'), '"/😀"');
45+
assert.strictEqual(JSONString.from('"/😀"'), '/😀');
46+
});
47+
48+
specify('should return original string if `from()` is given malformed JSON', function () {
49+
assert.strictEqual(JSONString.from('not a json string'), 'not a json string');
50+
assert.strictEqual(JSONString.from('{invalid:json}'), '{invalid:json}');
51+
assert.strictEqual(JSONString.from('42'), '42');
52+
});
53+
54+
specify('should correctly handle deeply nested JSON Pointers', function () {
55+
const nestedPointer = '/foo/bar/baz/qux/quux/corge/grault/garply/waldo/fred';
56+
const jsonString = JSONString.to(nestedPointer);
57+
const decodedPointer = JSONString.from(jsonString);
58+
assert.strictEqual(decodedPointer, nestedPointer);
59+
});
60+
61+
specify('should preserve leading and trailing spaces', function () {
62+
assert.strictEqual(JSONString.to(' /foo/bar '), '" /foo/bar "');
63+
assert.strictEqual(JSONString.from('" /foo/bar "'), ' /foo/bar ');
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)