-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtest.js
121 lines (109 loc) · 3.95 KB
/
test.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
112
113
114
115
116
117
118
119
120
const { pgnToActisenseSerialFormat, FromPgn } = require("@canboat/canboatjs");
const path = require('path')
const fs = require('fs')
const chai = require('chai')
const assert = chai.assert
chai.Should()
//chai.use(require('chai-things'))
chai.use(require('chai-json-equal'));
const parser = new FromPgn()
let skSelfData = {}
let skData = {}
const app = {
getSelfPath: (path) => {
return skSelfData[path]
},
getPath: (path) => {
return skData[path]
},
debug: (msg) => {
}
}
function load_conversions () {
fpath = path.join(__dirname, '../conversions')
files = fs.readdirSync(fpath)
return files.map(fname => {
pgn = path.basename(fname, '.js')
return require(path.join(fpath, pgn))(app, {});
}).filter(converter => { return typeof converter !== 'undefined'; });
}
const conversions = load_conversions()
describe('every conversion has a test', () => {
conversions.forEach(conversion => {
if ( !Array.isArray(conversion) ) {
conversion = [ conversion ]
}
conversion.forEach(conversion => {
it(`${conversion.title} has a test`, function (done) {
var subConversions = conversion.conversions
if ( typeof subConversions === 'undefined' ) {
subConversions = [ conversion ]
} else if ( typeof subConversions === 'function' ) {
subConversions = subConversions(Array.isArray(conversion.testOptions) ? conversion.testOptions[0] : conversion.testOptions)
}
assert(subConversions != undefined)
subConversions.forEach(subConv => {
subConv.should.have.property('tests')
})
done()
})
})
})
})
describe('conversions work', () => {
conversions.forEach(conversion => {
if ( !Array.isArray(conversion) ) {
conversion = [ conversion ]
}
conversion.forEach(conversion => {
let optionsList = Array.isArray(conversion.testOptions) ? conversion.testOptions : [ conversion.testOptions ]
optionsList.forEach((options, oidx) => {
var subConversions = conversion.conversions
if ( typeof subConversions === 'undefined' ) {
subConversions = [ conversion ]
} else if ( typeof subConversions === 'function' ) {
subConversions = subConversions(options || {})
}
subConversions.forEach(subConv => {
//subConv.should.have.property('tests')
if ( subConv.tests ) {
subConv.tests.forEach((test, idx) => {
it(`${conversion.title} test # ${oidx}/${idx} works`, function (done) {
skData = test.skData || {}
skSelfData = test.skSelfData || {}
let results = subConv.callback.call(null, ...test.input)
assert.equal(results.length, test.expected.length, 'number of results returned does not match the number of expected results')
let error
results.forEach((res, idx) => {
try
{
let encoded = pgnToActisenseSerialFormat(res)
let pgn = parser.parseString(encoded)
delete pgn.description
delete pgn.src
delete pgn.timestamp
delete pgn.input
let expected = test.expected[idx]
if ( typeof expected === 'function' ) {
expected = expected(options)
}
let preprocess = expected["__preprocess__"]
if ( preprocess ) {
preprocess(pgn)
delete expected["__preprocess__"]
}
//console.log('parsed: ' + JSON.stringify(pgn, null, 2))
pgn.should.jsonEqual(expected)
} catch ( e ) {
error = e
}
})
done(error)
})
})
}
})
})
})
})
})