def test_incremental(self): r = b'{ "foo": "bar" }' ctx = _ffi.new('struct split_context *') for i in range(len(r) - 1): set_buffer(ctx, r[i:i + 1]) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == _lib.INCOMPLETE self.assertEqual(ctx.offset, 1) set_buffer(ctx, r[-1:]) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == 0 self.assertEqual(ctx.offset, 1)
def test_incremental(self): r = b'{ "foo": "bar" }' ctx = _ffi.new('struct split_context *') for i in range(len(r)-1): set_buffer(ctx, r[i:i+1]) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == _lib.INCOMPLETE self.assertEqual(ctx.offset, 1) set_buffer(ctx, r[-1:]) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == 0 self.assertEqual(ctx.offset, 1)
def test_multiple(self): r = b'{ "foo": "bar" } { "baz": "qux" }' ctx = split_string(r) self.assertEqual(ctx.error, 0) self.assertEqual(ctx.offset, 16) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == 0 self.assertEqual(ctx.offset, len(r))
def test_trailing_whitespace(self): r = b'{ "foo": "bar" } ' ctx = split_string(r) self.assertEqual(ctx.error, 0) self.assertEqual(ctx.offset, len(r) - 1) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == _lib.INCOMPLETE self.assertEqual(ctx.offset, len(r))
def test_trailing_whitespace(self): r = b'{ "foo": "bar" } ' ctx = split_string(r) self.assertEqual(ctx.error, 0) self.assertEqual(ctx.offset, len(r)-1) error = _lib.json_split(ctx) self.assertEqual(error, ctx.error) == _lib.INCOMPLETE self.assertEqual(ctx.offset, len(r))
def split_string(s): ctx = _ffi.new('struct split_context *') set_buffer(ctx, s) _lib.json_split(ctx) return ctx