Skip to content

Commit e4ad768

Browse files
committed
test: add utf-16 test cases
1 parent b17e533 commit e4ad768

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/parallel/test-json-parser-string.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,60 @@ describe('node:json', () => {
174174
const result = parse('{"text":"line1\\nline2"}');
175175
assert.strictEqual(result.text, 'line1\nline2');
176176
});
177+
178+
test('parses non-BMP characters in string values', () => {
179+
const result = parse('{"emoji":"😀"}');
180+
assert.strictEqual(result.emoji, '😀');
181+
});
182+
183+
test('parses non-BMP characters in object keys', () => {
184+
const result = parse('{"😀":1}');
185+
assert.strictEqual(result['😀'], 1);
186+
});
187+
188+
test('parses UTF-16 surrogate pairs expressed as \\u escapes', () => {
189+
const result = parse('{"emoji":"\\uD83D\\uDE00"}');
190+
assert.strictEqual(result.emoji, '😀');
191+
});
192+
193+
test('parses another UTF-16 surrogate pair (musical symbol)', () => {
194+
const result = parse('{"symbol":"\\uD834\\uDD1E"}');
195+
assert.strictEqual(result.symbol, '𝄞');
196+
});
197+
198+
test('throws SyntaxError on unpaired high surrogate \\u escape', () => {
199+
assert.throws(
200+
() => parse('{"s":"\\uD800"}'),
201+
{
202+
name: 'SyntaxError',
203+
}
204+
);
205+
});
206+
207+
test('throws SyntaxError on unpaired low surrogate \\u escape', () => {
208+
assert.throws(
209+
() => parse('{"s":"\\uDC00"}'),
210+
{
211+
name: 'SyntaxError',
212+
}
213+
);
214+
});
215+
216+
test('throws SyntaxError on invalid surrogate pair order', () => {
217+
assert.throws(
218+
() => parse('{"s":"\\uDC00\\uD800"}'),
219+
{
220+
name: 'SyntaxError',
221+
}
222+
);
223+
});
224+
225+
test('throws SyntaxError when high surrogate is not followed by low surrogate', () => {
226+
assert.throws(
227+
() => parse('{"s":"\\uD83D\\u0041"}'),
228+
{
229+
name: 'SyntaxError',
230+
}
231+
);
232+
});
177233
});

0 commit comments

Comments
 (0)