-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
381 additions
and
29 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
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,277 @@ | ||
const t = require('tap') | ||
const { JSONStreamError, parse } = require('../lib/json-stream.js') | ||
|
||
t.test('JSONStream', (t) => { | ||
t.test('JSONStreamError constructor', (t) => { | ||
const error = new JSONStreamError(new Error('error')) | ||
t.equal(error.message, 'error') | ||
t.equal(error.name, 'JSONStreamError') | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream empty constructor', (t) => { | ||
const JSONStream = require('../lib/json-stream.js') | ||
const { Minipass } = require('minipass') | ||
|
||
const stream = new JSONStream() | ||
t.equal(stream instanceof Minipass, true) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream undefined path', async (t) => { | ||
const stream = parse(undefined, {}) | ||
const buffer = Buffer.from('{"a": 1}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [ | ||
{ | ||
a: 1, | ||
}, | ||
]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream empty path', async (t) => { | ||
const stream = parse('', {}) | ||
const buffer = Buffer.from('{"a": {"b": 1}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream path with function', async (t) => { | ||
const stream = parse([ | ||
(a) => a, | ||
{ | ||
test: (a) => a, | ||
}, | ||
]) | ||
|
||
const buffer = Buffer.from('{"a": {"b": {"c": 1}, "d": 2}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [{ c: 1 }, 2]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream path array with number in path', async (t) => { | ||
const stream = parse([5]) | ||
const buffer = Buffer.from('{"a": {"b": {"c": 1}, "d": 2}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
}) | ||
|
||
t.test( | ||
'JSONStream path array with recursive and undefined value', | ||
async (t) => { | ||
const stream = parse(['a', '', undefined], {}) | ||
const buffer = Buffer.from('{"a": {"b": 1}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
} | ||
) | ||
|
||
t.test('JSONStream emitPath', async (t) => { | ||
const stream = parse(['a', { emitPath: true }]) | ||
const buffer = Buffer.from('{"a": {"b": 1}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [ | ||
{ | ||
value: 1, | ||
path: ['a', 'b'], | ||
}, | ||
]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream emitKey', async (t) => { | ||
const stream = parse(['a', { emitKey: true }]) | ||
const buffer = Buffer.from('{"a": {"b": 1}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [ | ||
{ | ||
key: 'b', | ||
value: 1, | ||
}, | ||
]) | ||
t.end() | ||
}) | ||
|
||
t.test( | ||
'JSONStream recursive path item followed by a valid key', | ||
async (t) => { | ||
const stream = parse(['', 'b']) | ||
const buffer = Buffer.from('{"b": {"f": 1}}') | ||
|
||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [{ f: 1 }]) | ||
t.end() | ||
} | ||
) | ||
|
||
t.test('JSONStream.write', (t) => { | ||
t.test('JSONStream write chunk', async (t) => { | ||
const stream = parse('*', {}) | ||
const buffer = Buffer.from('{"a": 1}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [1]) | ||
t.end() | ||
}) | ||
|
||
t.test( | ||
'JSONStream write chunk from buffer with null property', | ||
async (t) => { | ||
const stream = parse('*') | ||
const buffer = Buffer.from('{"a": null}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
} | ||
) | ||
|
||
t.test( | ||
'JSONStream write chunk with mapper that returns undefined', | ||
async (t) => { | ||
// eslint-disable-next-line no-unused-vars | ||
const stream = parse('..', (_value, [_key]) => { | ||
return null | ||
}) | ||
const buffer = Buffer.from('{"a":{"b": {"c": 1}}}') | ||
stream.write(buffer) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
} | ||
) | ||
|
||
t.test('JSONStream write chunk function encoding', async (t) => { | ||
const stream = parse('*', {}) | ||
const buffer = Buffer.from('{"a": 1}') | ||
stream.write(buffer, () => {}) | ||
stream.end() | ||
|
||
const data = await stream.collect() | ||
t.same(data, [1]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream write error', async (t) => { | ||
const stream = parse('*', {}) | ||
const buffer = Buffer.from('{') | ||
try { | ||
stream.write(buffer) | ||
} catch (error) { | ||
t.equal(error.message, 'Unexpected end of JSON input') | ||
} | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream write non-buffer non-string throws error', (t) => { | ||
const stream = parse('*', {}) | ||
try { | ||
stream.write({}) | ||
} catch (error) { | ||
t.equal( | ||
error.message, | ||
'Can only parse JSON from string or buffer input' | ||
) | ||
t.equal(error.name, 'TypeError') | ||
} | ||
t.end() | ||
}) | ||
|
||
t.test( | ||
'JSONStream write invalid chunk throws JSONStreamError from parser', | ||
(t) => { | ||
const stream = parse('*', {}) | ||
try { | ||
stream.write('not a valid chunk') | ||
} catch (error) { | ||
t.equal(error.name, 'JSONStreamError') | ||
t.equal(error.message, 'Unexpected "o" at position 1 in state STOP') | ||
} | ||
t.end() | ||
} | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
t.test('JSONStream.end', (t) => { | ||
t.test('JSONStream end chunk encoding', async (t) => { | ||
const stream = parse('*', {}) | ||
const buffer = Buffer.from('{"a": 1}') | ||
stream.end(buffer) | ||
const data = await stream.collect() | ||
t.same(data, [1]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream end chunk function encoding', async (t) => { | ||
const stream = parse('*', {}) | ||
const buffer = Buffer.from('{"a": 1}') | ||
stream.end(buffer, () => {}) | ||
const data = await stream.collect() | ||
t.same(data, [1]) | ||
t.end() | ||
}) | ||
|
||
t.test('JSONStream end chunk function', async (t) => { | ||
const stream = parse('*', {}) | ||
stream.end(() => {}) | ||
const data = await stream.collect() | ||
t.same(data, []) | ||
t.end() | ||
}) | ||
|
||
t.test( | ||
'JSONStream end invalid chunk throws JSONStreamError from parser', | ||
(t) => { | ||
const stream = parse('*', {}) | ||
try { | ||
stream.end('not a valid chunk') | ||
} catch (error) { | ||
t.equal(error.name, 'JSONStreamError') | ||
t.equal(error.message, 'Unexpected "o" at position 1 in state STOP') | ||
} | ||
t.end() | ||
} | ||
) | ||
|
||
t.end() | ||
}) | ||
|
||
t.end() | ||
}) |
Oops, something went wrong.