/
yaml-test-suite.js
111 lines (101 loc) · 3.18 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import fs from 'fs'
import path from 'path'
import { CST, parseAllDocuments, Parser } from 'yaml'
import { testEvents } from 'yaml/test-events'
const skip = {
B63P: ['errors'], // allow ... after directives
SF5V: ['errors'] // allow duplicate %YAML directives
}
const testDirs = fs
.readdirSync(path.resolve(__dirname, 'yaml-test-suite'))
.filter(dir => /^[A-Z0-9]{4}$/.test(dir))
const matchJson = (docs, json) => {
if (!json) return
const received = docs[0] ? docs.map(doc => doc.toJS()) : null
const expected =
docs.length > 1
? json
.replace(/\n$/, '')
.split('\n')
.map(line => JSON.parse(line))
: [JSON.parse(json)]
if (!received || typeof received !== 'object') {
expect(received).toBe(expected)
} else {
expect(received).toMatchObject(expected)
}
}
testDirs.forEach(dir => {
const root = path.resolve(__dirname, 'yaml-test-suite', dir)
const name = fs.readFileSync(path.resolve(root, '==='), 'utf8').trim()
const yaml = fs.readFileSync(path.resolve(root, 'in.yaml'), 'utf8')
let error, events, json, outYaml
try {
fs.readFileSync(path.resolve(root, 'error'), 'utf8')
error = true
} catch (e) {
/* ignore error */
}
try {
// Too much variance in event stream length for error cases
events = !error && fs.readFileSync(path.resolve(root, 'test.event'), 'utf8')
} catch (e) {
/* ignore error */
}
try {
json = fs.readFileSync(path.resolve(root, 'in.json'), 'utf8')
} catch (e) {
/* ignore error */
}
try {
outYaml = fs.readFileSync(path.resolve(root, 'out.yaml'), 'utf8')
} catch (e) {
/* ignore error */
}
function _test(name, cb) {
const sd = skip[dir]
if (sd === true || (sd && sd.includes(name))) test.skip(name, cb)
else test(name, cb)
}
describe(`${dir}: ${name}`, () => {
test('cst stringify', () => {
let res = ''
new Parser(tok => (res += CST.stringify(tok))).parse(yaml)
expect(res).toBe(yaml)
})
const docs = parseAllDocuments(yaml, { resolveKnownTags: false })
if (events) {
_test('test.event', () => {
const res = testEvents(yaml)
expect(res.events.join('\n') + '\n').toBe(events)
expect(res.error).toBeNull()
})
}
if (json) _test('in.json', () => matchJson(docs, json))
_test('errors', () => {
const errors = docs
.map(doc => doc.errors)
.filter(docErrors => docErrors.length > 0)
if (error) {
expect(errors).not.toHaveLength(0)
} else {
expect(errors).toHaveLength(0)
}
})
if (!error) {
const src2 =
docs.map(doc => String(doc).replace(/\n$/, '')).join('\n...\n') + '\n'
const docs2 = parseAllDocuments(src2, { resolveKnownTags: false })
if (json) _test('stringfy+re-parse', () => matchJson(docs2, json))
if (outYaml) {
_test('out.yaml', () => {
const resDocs = parseAllDocuments(yaml)
const resJson = resDocs.map(doc => doc.toJS({ mapAsMap: true }))
const expDocs = parseAllDocuments(outYaml)
const expJson = expDocs.map(doc => doc.toJS({ mapAsMap: true }))
expect(resJson).toMatchObject(expJson)
})
}
}
})
})