|
| 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