UNPKG

3.89 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 Script = require('../lib/script');
18const APModelManager = require('../lib/apmodelmanager');
19
20const chai = require('chai');
21const fs = require('fs');
22
23chai.should();
24chai.use(require('chai-things'));
25chai.use(require('chai-as-promised'));
26
27const jsSample = fs.readFileSync('./test/data/test.js','utf8');
28const ctoSample = fs.readFileSync('./test/data/test.cto','utf8');
29const ergoSample = fs.readFileSync('./test/data/test.ergo', 'utf8');
30
31describe('Script', () => {
32 let modelManager;
33
34 beforeEach(async function () {
35 modelManager = new APModelManager();
36 modelManager.addCTOModel(ctoSample,'test.cto',true);
37 });
38
39 describe('#constructor', () => {
40
41 it('should instantiate a JavaScript script', async function() {
42 const script = new Script(modelManager,'test.js','.js',jsSample);
43 script.getLanguage().should.equal('.js');
44 script.getIdentifier().should.equal('test.js');
45 script.getContents().should.equal(jsSample);
46 script.getTokens().length.should.equal(51);
47 });
48
49 it('should instantiate an Ergo script', async function() {
50 const script = new Script(modelManager,'test.ergo','.ergo',ergoSample,'org.accordproject.helloemit.HelloWorld');
51 script.getLanguage().should.equal('.ergo');
52 script.getIdentifier().should.equal('test.ergo');
53 script.getContractName().should.equal('org.accordproject.helloemit.HelloWorld');
54 script.getContents().should.equal(ergoSample);
55 script.getTokens().length.should.equal(0);
56 });
57
58 it('should fail to instantiate for empty script', async function() {
59 (() => new Script(modelManager,'test.js','.js','')).should.throw('Empty script contents');
60 });
61
62 it('should fail to instantiate for buggy JavaScript', async function() {
63 (() => new Script(modelManager,'test.js','.js','foo bar')).should.throw('Failed to parse test.js: Unexpected token');
64 });
65
66 });
67 describe('#getFunctionDeclarations', () => {
68
69 it('should instantiate a JavaScript script', async function() {
70 const script = new Script(modelManager,'test.js','.js',jsSample);
71 const decls = script.getFunctionDeclarations();
72 decls.map(fd => fd.getName()).should.deep.equal(['paymentClause','__dispatch']);
73 const decl = decls[0];
74 decl.getThrows().should.equal('');
75 decl.getLanguage().should.equal('.js');
76 decl.getFunctionText().length.should.equal(169);
77 decl.getVisibility().should.equal('+');
78 decl.getDecorators().should.deep.equal(['param','param','param','param']);
79 decl.getReturnType().should.equal('void');
80 decl.getParameterNames().should.deep.equal(['context']);
81 decl.getParameterTypes().should.deep.equal([
82 'Context',
83 'org.accordproject.copyrightlicense.PaymentRequest',
84 'org.accordproject.copyrightlicense.PayOut',
85 'Event'
86 ]);
87 });
88
89 it('should fail to instantiate a JavaScript script', async function() {
90 (() => new Script(null,'test.js','.js',jsSample)).should.throw('ModelManager is required.');
91 });
92
93 });
94
95});