1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const Script = require('../lib/script');
|
18 | const APModelManager = require('../lib/apmodelmanager');
|
19 |
|
20 | const chai = require('chai');
|
21 | const fs = require('fs');
|
22 |
|
23 | chai.should();
|
24 | chai.use(require('chai-things'));
|
25 | chai.use(require('chai-as-promised'));
|
26 |
|
27 | const jsSample = fs.readFileSync('./test/data/test.js','utf8');
|
28 | const ctoSample = fs.readFileSync('./test/data/test.cto','utf8');
|
29 | const ergoSample = fs.readFileSync('./test/data/test.ergo', 'utf8');
|
30 |
|
31 | describe('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 | });
|