-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (72 loc) · 2.54 KB
/
index.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
/**
* © Copyright IBM Corp. 2016 All Rights Reserved
* Project name: JSONata
* This project is licensed under the MIT License, see LICENSE
*/
'use strict';
var assert = require('assert');
var chai = require("chai");
var fs = require("fs");
var jsonata = require('jsonata');
var path = require("path");
var expect = chai.expect;
var parseF = function(filename) {
const content = fs.readFileSync(filename, 'utf8');
if (content === 'undefined') {
return undefined;
}
return JSON.parse(content);
}
var consumeDirectory = function(p) {
var trueName = path.basename(p)
.replace(/%3A/g, ":")
.replace(/%22/g, "\"")
.replace(/%2A/g, "*")
.replace(/%3C/g, "<")
.replace(/%3E/g, ">")
.replace(/%0A/g, "\n")
.replace(/%3F/g, "\?")
.replace(/%09/g, "\t")
.replace(/%5C/g, "\\")
.replace(/%2F/g, "/")
.replace(/%7C/g, "|");
var innerDirectories = fs.readdirSync(p)
.map(fn => p + '/' + fn)
.filter(fn => fs.statSync(fn).isDirectory());
var directoryIsIt = fs.existsSync(p + '/expression.jsonata');
var directoryIsDescribe = innerDirectories.length > 0;
if(directoryIsIt) {
if(directoryIsDescribe) {
throw Error(p + " is both an it() and a describe()");
}
it(trueName, function() {
var str = fs.readFileSync(p + '/expression.jsonata', 'utf8');
if(!fs.existsSync(p + '/input.json')) {
expect(() => jsonata(str)).to.throw();
return;
}
var expression = jsonata(str);
var input = parseF(p + '/input.json');
if(fs.existsSync(p + '/bindings.json')) {
var bindings = parseF(p + '/bindings.json');
Object.keys(bindings).forEach(key => {
expression.assign(key, bindings[key]);
});
}
if(!fs.existsSync(p + '/output.json')) {
expect(() => expression.evaluate(input)).to.throw();
return;
}
var actual = expression.evaluate(input);
var expected = parseF(p + '/output.json');
expect(actual).to.deep.equal(expected);
});
} else if(directoryIsDescribe) {
describe(trueName, function () {
innerDirectories.forEach(consumeDirectory);
});
} else {
throw Error(p + " is neither an it() nor a describe()");
}
};
fs.readdirSync("./data").map(fn => "./data/" + fn).forEach(consumeDirectory);