Skip to content

Commit

Permalink
chore(tests): add unit tests for partial-json-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie authored and stainless-app[bot] committed Jun 28, 2024
1 parent 8479a30 commit a96f91c
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/lib/partial-json.test.ts
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: {} } } });
});
});

0 comments on commit a96f91c

Please sign in to comment.