UNPKG

2.91 kBJavaScriptView Raw
1import fs from 'fs';
2import path from 'path';
3import { errors, interpreter, Time } from '../src';
4
5const EXPECTATIONS_DIR = path.join(__dirname, 'data/interpreter-test-suite/decide/expectations');
6const TREES_DIR = path.join(__dirname, 'data/interpreter-test-suite/decide/trees');
7
8// List the trees
9const versionFolder = fs.readdirSync(TREES_DIR);
10
11describe('decide', () => {
12 const firstVersion = versionFolder[0];
13 const treeFiles = fs.readdirSync(path.join(TREES_DIR, firstVersion));
14 const firstTreeFile = treeFiles[0];
15 const firstTree = require(path.join(TREES_DIR, firstVersion, firstTreeFile));
16 const firstExpectation = require(path.join(EXPECTATIONS_DIR, firstVersion, firstTreeFile))[0];
17
18 describe(`"${firstTreeFile}"`, function() {
19 it(firstExpectation.title, function() {
20 if (firstExpectation.error) {
21 expect(() => interpreter.decide(firstTree, firstExpectation.context, firstExpectation.time ? new Time(firstExpectation.time.t, firstExpectation.time.tz) : {})).to.throw();
22 }
23 else {
24 expect(interpreter.decide(firstTree, firstExpectation.context, firstExpectation.time ? new Time(firstExpectation.time.t, firstExpectation.time.tz) : {})).to.be.deep.equal(firstExpectation.output);
25 }
26 });
27 });
28});
29
30describe('interpreter.decide', () => {
31 _.each(versionFolder, (version) => {
32 const treeFiles = fs.readdirSync(path.join(TREES_DIR, version));
33 _.each(treeFiles, (treeFile) => {
34 describe(`"${treeFile}"`, function() {
35 // Load the tree
36 let json = require(path.join(TREES_DIR, version, treeFile));
37
38 // Load the expectations for this tree.
39 const expectations = require(path.join(EXPECTATIONS_DIR, version, treeFile));
40
41 _.each(expectations, (expectation) => {
42 it(expectation.title, function() {
43 if (expectation.error) {
44 try {
45 interpreter.decide(json, expectation.context, expectation.time ? new Time(expectation.time.t, expectation.time.tz) : {});
46 throw new Error('\'interpreter.decide\' should throw a \'CraftAiError\'.');
47 }
48 catch (e) {
49 if (e instanceof errors.CraftAiError) {
50 expect(e.message).to.equal(expectation.error.message);
51 expect(e.metadata).to.deep.equal(expectation.error.metadata);
52 }
53 else {
54 throw e;
55 }
56 }
57 }
58 else {
59 if (!_.isUndefined(expectation.configuration)) {
60 json.configuration = _.assign(json.configuration, expectation.configuration);
61 }
62 expect(interpreter.decide(json, expectation.context, expectation.time ? new Time(expectation.time.t, expectation.time.tz) : {})).to.be.deep.equal(expectation.output);
63 }
64 });
65 });
66 });
67 });
68 });
69});