-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
yaml-test-suite.js
73 lines (68 loc) · 2.39 KB
/
yaml-test-suite.js
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
68
69
70
71
72
73
import fs from 'fs'
import path from 'path'
import { YAMLWarning } from '../src/errors'
import resolve from '../src/index'
const testDirs = fs.readdirSync(path.resolve(__dirname, 'yaml-test-suite'))
.filter(dir => ['.git', 'meta', 'name', 'tags'].indexOf(dir) === -1)
const matchJson = (stream, json) => {
if (!json) return
const received = stream[0] ? stream[0].toJSON() : null
const expected = JSON.parse(json)
if (!received || typeof received !== 'object') {
expect(received).toBe(expected)
} else {
expect(received).toMatchObject(expected)
}
}
const skipErrors = [
'3HFZ',
'5U3A',
'9C9N',
'9KBC',
'B63P',
'ZCZ6',
'ZL4Z',
]
testDirs.forEach(dir => {
const root = path.resolve(__dirname, 'yaml-test-suite', dir)
const name = fs.readFileSync(path.resolve(root, '==='), 'utf8')
const yaml = fs.readFileSync(path.resolve(root, 'in.yaml'), 'utf8')
let json, error, outYaml
try { json = fs.readFileSync(path.resolve(root, 'in.json'), 'utf8') } catch (e) {}
try { fs.readFileSync(path.resolve(root, 'error'), 'utf8'); error = true } catch (e) {}
try { outYaml = fs.readFileSync(path.resolve(root, 'out.yaml'), 'utf8') } catch (e) {}
if (!error && !json && !outYaml) return
test(`${dir}: ${name}`, () => {
const stream = resolve(yaml)
matchJson(stream, json)
if (error) {
if (skipErrors.includes(dir)) return
if (stream[0].errors.length === 0) {
log: dir, name,
'\nIN\n' + yaml,
'\nJSON\n' + JSON.stringify(stream[0], null, ' ')
}
expect(stream[0].errors).not.toHaveLength(0)
} else {
const errors = stream
.map(doc => doc.errors.filter(err => !(err instanceof YAMLWarning)))
.filter(docErrors => docErrors.length > 0)
expect(errors).toHaveLength(0)
const src2 = stream.map(doc => String(doc).replace(/\n$/, '')).join('\n---\n') + '\n'
const stream2 = resolve(src2)
trace: name,
'\nIN\n' + yaml,
'\nJSON\n' + JSON.stringify(stream[0], null, ' '),
'\n\nOUT\n' + src2,
'\nOUT-JSON\n' + JSON.stringify(src2),
'\nRE-JSON\n' + JSON.stringify(stream2[0], null, ' ')
matchJson(stream2, json)
if (outYaml) {
const expStream = resolve(outYaml)
const resJson = stream.map(doc => doc.toJSON())
const expJson = expStream.map(doc => doc.toJSON())
expect(resJson).toMatchObject(expJson)
}
}
})
})