-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): add unit tests for partial-json-parser
- Loading branch information
1 parent
8479a30
commit a96f91c
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { partialParse } from '@anthropic-ai/sdk/_vendor/partial-json-parser/parser'; | ||
|
||
describe('partialParse', () => { | ||
test('a valid complete JSON string', () => { | ||
expect(partialParse(`{"foo": "bar", "thing": "baz"}`)).toEqual({ foo: 'bar', thing: 'baz' }); | ||
}); | ||
|
||
test('a valid partial JSON string', () => { | ||
expect(partialParse(`{"foo": "bar", "thing": "`)).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
test('empty JSON object', () => { | ||
expect(partialParse(`{}`)).toEqual({}); | ||
}); | ||
|
||
test('incomplete nested JSON object', () => { | ||
expect(partialParse(`{"foo": {"bar": "baz"}`)).toEqual({ foo: { bar: 'baz' } }); | ||
}); | ||
|
||
test('complete nested JSON object', () => { | ||
expect(partialParse(`{"foo": {"bar": "baz"}}`)).toEqual({ foo: { bar: 'baz' } }); | ||
}); | ||
|
||
test('JSON array with incomplete object', () => { | ||
expect(partialParse(`{"foo": [{"bar": "baz"}`)).toEqual({ foo: [{ bar: 'baz' }] }); | ||
}); | ||
|
||
test('JSON array with complete objects', () => { | ||
expect(partialParse(`{"foo": [{"bar": "baz"}, {"qux": "quux"}]}`)).toEqual({ | ||
foo: [{ bar: 'baz' }, { qux: 'quux' }], | ||
}); | ||
}); | ||
|
||
test('string with escaped characters', () => { | ||
expect(partialParse(`{"foo": "bar\\\"baz"}`)).toEqual({ foo: 'bar"baz' }); | ||
}); | ||
|
||
test('string with incomplete escape sequence', () => { | ||
expect(partialParse(`{"foo": "bar\\`)).toEqual({}); | ||
}); | ||
|
||
test('invalid JSON string gracefully', () => { | ||
expect(partialParse(`{"foo": "bar", "thing": "baz"`)).toEqual({ foo: 'bar', thing: 'baz' }); | ||
}); | ||
|
||
test('JSON string with null value', () => { | ||
expect(partialParse(`{"foo": null, "bar": "baz"}`)).toEqual({ foo: null, bar: 'baz' }); | ||
}); | ||
|
||
test('JSON string with number values', () => { | ||
expect(partialParse(`{"foo": 123, "bar": 45.67}`)).toEqual({ foo: 123, bar: 45.67 }); | ||
}); | ||
|
||
test('JSON string with boolean values', () => { | ||
expect(partialParse(`{"foo": true, "bar": false}`)).toEqual({ foo: true, bar: false }); | ||
}); | ||
|
||
test('JSON string with mixed data types', () => { | ||
expect(partialParse(`{"foo": "bar", "baz": 123, "qux": true, "quux": null}`)).toEqual({ | ||
foo: 'bar', | ||
baz: 123, | ||
qux: true, | ||
quux: null, | ||
}); | ||
}); | ||
|
||
test('JSON string with partial literal tokens', () => { | ||
expect(partialParse(`{"foo": "bar", "baz": nul`)).toEqual({ foo: 'bar' }); | ||
expect(partialParse(`{"foo": "bar", "baz": tr`)).toEqual({ foo: 'bar' }); | ||
expect(partialParse(`{"foo": "bar", "baz": truee`)).toEqual({ foo: 'bar' }); | ||
expect(partialParse(`{"foo": "bar", "baz": fal`)).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
test('deeply nested JSON objects', () => { | ||
expect(partialParse(`{"a": {"b": {"c": {"d": "e"}}}}`)).toEqual({ a: { b: { c: { d: 'e' } } } }); | ||
}); | ||
|
||
test('deeply nested partial JSON objects', () => { | ||
expect(partialParse(`{"a": {"b": {"c": {"d": "e`)).toEqual({ a: { b: { c: {} } } }); | ||
}); | ||
}); |