UNPKG

10.3 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const chai = require('chai');
18const should = chai.should();
19
20chai.should();
21const expect = chai.expect;
22chai.use(require('chai-things'));
23chai.use(require('chai-as-promised'));
24
25const fs = require('fs');
26const Path = require('path');
27
28const TESTS_DIR = '../../tests';
29
30const ScriptManager = require('../lib/scriptmanager');
31const APModelManager = require('../lib/apmodelmanager');
32
33const modelManager = new APModelManager();
34const jsSample = fs.readFileSync('./test/data/test.js','utf8');
35const jsSample2 = fs.readFileSync('./test/data/test2.js','utf8');
36const ergoSample = fs.readFileSync('./test/data/test.ergo', 'utf8');
37const ergoSample2 = fs.readFileSync('./test/data/test2.ergo', 'utf8');
38
39describe('ScriptManager', () => {
40
41 describe('#constructor', () => {
42
43 it('should instantiate a script manager', async function() {
44 (() => new ScriptManager('es6',modelManager)).should.not.be.null;
45 });
46
47 it('should support both JavaScript and Ergo scripts', async function() {
48 const scriptManager = new ScriptManager('es6',modelManager);
49 const script1 = scriptManager.createScript('test.js','.js',jsSample);
50 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
51 scriptManager.addScript(script1);
52 scriptManager.addScript(script2);
53 scriptManager.getScript('test.js').should.not.be.null;
54 scriptManager.getScript('test.ergo').should.not.be.null;
55 scriptManager.getScripts().length.should.equal(2);
56 scriptManager.getAllScripts().length.should.equal(2);
57 scriptManager.getScriptsForTarget('ergo').length.should.equal(1);
58 scriptManager.getScriptsForTarget('es5').length.should.equal(1);
59 scriptManager.getScriptsForTarget('java').length.should.equal(0);
60 (() => scriptManager.hasInit()).should.throw('Function __init was not found in logic');
61 (() => scriptManager.hasDispatch()).should.not.throw;
62 scriptManager.getLogic().map(x => x.name).should.deep.equal(['test.ergo']);
63 scriptManager.allFunctionDeclarations().length.should.equal(2);
64 scriptManager.allFunctionDeclarations().map(x => x.getName()).should.deep.equal(['paymentClause','__dispatch']);
65 scriptManager.getCompiledScript().getContents().length.should.equal(47864);
66 scriptManager.getCompiledJavaScript().length.should.equal(47864);
67 scriptManager.allFunctionDeclarations().length.should.equal(152);
68 scriptManager.allFunctionDeclarations().filter(x => x.name === '__init').length.should.equal(1);
69 expect(scriptManager.hasInit()).to.not.throw;
70 expect(scriptManager.hasDispatch()).to.not.throw;
71 });
72
73 it('should compile Ergo scripts', async function() {
74 const scriptManager = new ScriptManager('es6',modelManager, null, {});
75 const script1 = scriptManager.createScript('test.js','.js',jsSample);
76 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
77 scriptManager.addScript(script1);
78 scriptManager.addScript(script2);
79 scriptManager.compileLogic().getContents().length.should.equal(47864);
80 scriptManager.getCompiledScript().getContents().length.should.equal(47864);
81 scriptManager.getAllScripts().length.should.equal(3);
82 });
83
84 it('should fail to compile an Ergo script with a parse error', async function() {
85 const scriptManager = new ScriptManager('es6',modelManager);
86 const script1 = scriptManager.createScript('test.js','.js',jsSample);
87 const script2 = scriptManager.createScript('test2.ergo','.ergo',ergoSample2);
88 scriptManager.addScript(script1);
89 scriptManager.addScript(script2);
90 (() => scriptManager.compileLogic()).should.throw('Parse error (at file test2.ergo line 33 col 0). ');
91 });
92
93 it('should return no compiled script when no Ergo or JS', async function() {
94 const scriptManager = new ScriptManager('es6',modelManager);
95 scriptManager.getCombinedScripts().should.equal('');
96 should.equal(scriptManager.compileLogic(false),null);
97 });
98 it('should return no combined script when not compiled', async function() {
99 const scriptManager = new ScriptManager('es6',modelManager);
100 const script2 = scriptManager.createScript('test2.ergo','.ergo',ergoSample2);
101 scriptManager.addScript(script2);
102 scriptManager.getCombinedScripts().should.equal('');
103 });
104
105 it('should fail to compile an Ergo script with an undefined built-in function', async function() {
106 const scriptManager = new ScriptManager('es6',modelManager);
107 const ergoErr = fs.readFileSync(Path.join(TESTS_DIR,'smoke/builtinErr.ergo'), 'utf8');
108 const script1 = scriptManager.createScript('builtinErr.ergo','.ergo',ergoErr);
109 scriptManager.addScript(script1);
110 (() => scriptManager.compileLogic()).should.throw('System error. [ec2en/function] Function org.accordproject.builtin.foo did not get inlined');
111 });
112
113 it('should delete both JavaScript and Ergo scripts if they exist', async function() {
114 const scriptManager = new ScriptManager('es6',modelManager);
115 const script1 = scriptManager.createScript('test.js','.js',jsSample);
116 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
117 scriptManager.addScript(script1);
118 scriptManager.addScript(script2);
119 scriptManager.getScript('test.js').should.not.be.null;
120 scriptManager.getScript('test.ergo').should.not.be.null;
121 scriptManager.deleteScript('test.js');
122 scriptManager.deleteScript('test.ergo');
123 scriptManager.getScripts().length.should.equal(0);
124 });
125
126 it('should fail deleting a script which does not exist', async function() {
127 const scriptManager = new ScriptManager('es6',modelManager);
128 const script1 = scriptManager.createScript('test.js','.js',jsSample);
129 scriptManager.addScript(script1);
130 return (() => scriptManager.deleteScript('test.ergo')).should.throw('Script file does not exist');
131 });
132
133 it('should clear scripts', async function() {
134 const scriptManager = new ScriptManager('es6',modelManager);
135 const script1 = scriptManager.createScript('test.js','.js',jsSample);
136 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
137 scriptManager.addScript(script1);
138 scriptManager.addScript(script2);
139 scriptManager.getScript('test.js').should.not.be.null;
140 scriptManager.getScript('test.ergo').should.not.be.null;
141 scriptManager.clearScripts();
142 });
143
144 it('should get scripts identifiers', async function() {
145 const scriptManager = new ScriptManager('es6',modelManager);
146 const script1 = scriptManager.createScript('test.js','.js',jsSample);
147 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
148 scriptManager.addScript(script1);
149 scriptManager.addScript(script2);
150 scriptManager.getScriptIdentifiers().should.deep.equal(['test.js','test.ergo']);
151 });
152
153 it('should update script', async function() {
154 const scriptManager = new ScriptManager('es6',modelManager);
155 const script1 = scriptManager.createScript('test.js','.js',jsSample);
156 const script2 = scriptManager.createScript('test.js','.js',jsSample2);
157 scriptManager.addScript(script1);
158 scriptManager.getScript('test.js').getTokens().length.should.equal(51);
159 scriptManager.updateScript(script2);
160 scriptManager.getScript('test.js').getTokens().length.should.equal(52);
161 });
162
163 it('should fail updating a script which does not exist', async function() {
164 const scriptManager = new ScriptManager('es6',modelManager);
165 const script1 = scriptManager.createScript('test.js','.js',jsSample);
166 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
167 scriptManager.addScript(script1);
168 return (() => scriptManager.updateScript(script2)).should.throw('Script file does not exist');
169 });
170
171 it('should modify script', async function() {
172 const scriptManager = new ScriptManager('es6',modelManager);
173 const script1 = scriptManager.createScript('test.js','.js',jsSample);
174 scriptManager.addScript(script1);
175 scriptManager.modifyScript('test.js','.js',jsSample2);
176 scriptManager.getScript('test.js').getTokens().length.should.equal(52);
177 });
178
179 it('clear all scripts', async function() {
180 const scriptManager = new ScriptManager('es6',modelManager);
181 const script1 = scriptManager.createScript('test.js','.js',jsSample);
182 const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
183 scriptManager.addScript(script1);
184 scriptManager.addScript(script2);
185 scriptManager.compileLogic().getContents().length.should.equal(47864);
186 scriptManager.getCompiledJavaScript().length.should.equal(47864);
187 scriptManager.clearScripts();
188 return (() => scriptManager.getCompiledJavaScript()).should.throw('Did not find any compiled JavaScript logic');
189 });
190
191 });
192
193});