-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
67 lines (56 loc) · 1.71 KB
/
test.js
File metadata and controls
67 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, test } from 'node:test'
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
function mx(input, ...args) {
return spawnSync('node', ['index.js', ...args], {
input: typeof input === 'string' ? input : JSON.stringify(input),
encoding: 'utf8',
})
}
describe('print', () => {
test('object', () => {
const { stdout } = mx({ greeting: 'hello world' })
assert.equal(stdout, '{\n "greeting": "hello world"\n}\n')
})
test('array', () => {
const { stdout } = mx([1, 2, 3])
assert.equal(stdout, '[\n 1\n 2\n 3\n]\n')
})
})
describe('query', () => {
test('.prop', () => {
const { stdout } = mx({ name: 'hello' }, '.name')
assert.equal(stdout, '"hello"\n')
})
test('.prop.nested', () => {
const { stdout } = mx({ a: { b: 'value' } }, '.a.b')
assert.equal(stdout, '"value"\n')
})
test('.array[index]', () => {
const { stdout } = mx([10, 20, 30], '.[1]')
assert.equal(stdout, '20\n')
})
test('.prop.array[index]', () => {
const { stdout } = mx({ items: [10, 20, 30] }, '.items[1]')
assert.equal(stdout, '20\n')
})
test('.prop.array[index].value', () => {
const { stdout } = mx(
{ data: { list: [{ x: 1 }, { x: 2 }] } },
'.data.list[1].x',
)
assert.equal(stdout, '2\n')
})
test('nested objects', () => {
const { stdout } = mx({ a: { b: { c: { d: 42 } } } }, '.a.b.c.d')
assert.equal(stdout, '42\n')
})
test('returns sub-object', () => {
const { stdout } = mx({ a: { b: 1 } }, '.a')
assert.equal(stdout, '{\n "b": 1\n}\n')
})
test('returns sub-array', () => {
const { stdout } = mx({ a: [1, 2] }, '.a')
assert.equal(stdout, '[\n 1\n 2\n]\n')
})
})