1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const ErgoCompiler = require('../lib/compiler');
|
18 | const Chai = require('chai');
|
19 |
|
20 | Chai.should();
|
21 | Chai.use(require('chai-things'));
|
22 | Chai.use(require('chai-as-promised'));
|
23 |
|
24 | const Fs = require('fs');
|
25 | const Path = require('path');
|
26 |
|
27 | const TESTS_DIR = '../../tests';
|
28 |
|
29 | describe('ergo-compiler', () => {
|
30 |
|
31 | afterEach(() => {});
|
32 |
|
33 | describe('#targets', () => {
|
34 | it('should return all the compiler targets', () => {
|
35 | ErgoCompiler.availableTargets().should.deep.equal(['es6','java']);
|
36 | });
|
37 | it('es5 should *not* be a valid target', () => {
|
38 | (() => ErgoCompiler.isValidTarget('es5')).should.throw('Unknown target: es5 (available: es6,java)');
|
39 | });
|
40 | it('es6 should be a valid target', () => {
|
41 | ErgoCompiler.isValidTarget('es6').should.equal(true);
|
42 | });
|
43 | it('java should be a valid target', () => {
|
44 | ErgoCompiler.isValidTarget('java').should.equal(true);
|
45 | });
|
46 | it('should not be a valid target', () => {
|
47 | (() => ErgoCompiler.isValidTarget('es7')).should.throw('Unknown target: es7 (available: es6,java)');
|
48 | });
|
49 | });
|
50 |
|
51 | describe('#callname', function () {
|
52 | it('should sanitize call names for contracts', async function () {
|
53 | const contractName = 'org.accordproject.volumediscount.VolumeDiscount';
|
54 | const result = await ErgoCompiler.contractCallNamePromise(contractName);
|
55 | result.should.equal('orgXaccordprojectXvolumediscountXVolumeDiscount');
|
56 | });
|
57 | });
|
58 |
|
59 | describe('#parsefail', function () {
|
60 | it('should fail when Ergo does not parse', async function () {
|
61 | const ergoFile = Path.resolve(TESTS_DIR, 'bad-logic', 'logic/logic.ergo');
|
62 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
63 | const ctoFile = Path.resolve(TESTS_DIR, 'bad-logic', 'model/model.cto');
|
64 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
65 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', false);
|
66 | result.error.kind.should.equal('ParseError');
|
67 | result.error.message.should.equal('Parse error');
|
68 | result.error.locstart.should.deep.equal({ 'line' : 17, 'column' : 20 });
|
69 | result.error.locend.should.deep.equal({ 'line' : 17, 'column' : 23 });
|
70 | });
|
71 | });
|
72 | describe('#parseerrormessage', function () {
|
73 | it('should format parse error', async function () {
|
74 | const result = await ErgoCompiler.ergoErrorToString({ 'kind' : 'ParseError', 'message' : 'Parse error', 'fullMessage' : 'Parse error (at file ../../tests/bad-logic/logic/logic.ergo line 17 col 20). \ncontract HelloWorld ovr TemplateModel {\n ^^^ ', 'locstart' : { 'line' : 16, 'column' : 25 }, 'locend' : { 'line' : 16, 'column' : 26 } });
|
75 | result.should.deep.equal('Parse error');
|
76 | });
|
77 | });
|
78 | describe('#verboseparseerrormessage', function () {
|
79 | it('should format fullMessage parse error', async function () {
|
80 | const result = await ErgoCompiler.ergoVerboseErrorToString({ 'kind' : 'ParseError', 'message' : 'Parse error', 'fullMessage' : 'Parse error (at file ../../tests/bad-logic/logic/logic.ergo line 17 col 20). \ncontract HelloWorld ovr TemplateModel {\n ^^^ ', 'locstart' : { 'line' : 16, 'column' : 25 }, 'locend' : { 'line' : 16, 'column' : 26 } });
|
81 | result.should.deep.equal('Parse error (at file ../../tests/bad-logic/logic/logic.ergo line 17 col 20). \ncontract HelloWorld ovr TemplateModel {\n ^^^ ');
|
82 | });
|
83 | });
|
84 | describe('#compilationerrormessage', function () {
|
85 | it('should format compilation error', async function () {
|
86 | const result = await ErgoCompiler.ergoErrorToString({ 'kind' : 'CompilationError', 'message' : 'Import not found: org.accordproject.test', 'locstart' : { 'line' : -1, 'column' : -1 }, 'locend' : { 'line' : -1, 'column' : -1 } });
|
87 | result.should.deep.equal('Import not found: org.accordproject.test');
|
88 | });
|
89 | });
|
90 | describe('#compile', function () {
|
91 | it('should compile a smart Ergo contract to JavaScript (ES6)', async function () {
|
92 | const ergoFile = Path.resolve(TESTS_DIR, 'helloworld', 'logic/logic.ergo');
|
93 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
94 | const ctoFile = Path.resolve(TESTS_DIR, 'helloworld', 'model/model.cto');
|
95 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
96 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', false, false);
|
97 | result.success.should.not.be.null;
|
98 | });
|
99 | it('should compile a smart Ergo contract to JavaScript and print a warning (ES6)', async function () {
|
100 | const ergoFile = Path.resolve(TESTS_DIR, 'helloworldWarning', 'logic/logicWarn.ergo');
|
101 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
102 | const ctoFile = Path.resolve(TESTS_DIR, 'helloworldWarning', 'model/model.cto');
|
103 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
104 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', false, true);
|
105 | result.success.should.not.be.null;
|
106 | });
|
107 | it('should compile and link a smart Ergo contract to JavaScript (ES6)', async function () {
|
108 | const ergoFile = Path.resolve(TESTS_DIR, 'helloworld', 'logic/logic.ergo');
|
109 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
110 | const ctoFile = Path.resolve(TESTS_DIR, 'helloworld', 'model/model.cto');
|
111 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
112 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', true);
|
113 | result.success.should.not.be.null;
|
114 | });
|
115 | });
|
116 | describe('#compilelatedeliveryfail', function () {
|
117 | it('should fail when compiling a smart Ergo contract to JavaScript', async function () {
|
118 | const ergoFile = Path.resolve(TESTS_DIR, 'latedeliveryandpenalty2', 'logic/logic.ergo');
|
119 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
120 | const ctoFile = Path.resolve(TESTS_DIR, 'latedeliveryandpenalty2', 'model/model2.cto');
|
121 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
122 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', false);
|
123 | result.should.deep.equal({ 'error' : { 'kind' : 'CompilationError', 'fileName': null, 'message': 'Import not found: org.accordproject.test', 'fullMessage' : 'Compilation error. Import not found: org.accordproject.test', 'locstart' : { 'line' : -1, 'column' : -1 }, 'locend' : { 'line' : -1, 'column' : -1 } } });
|
124 | });
|
125 | });
|
126 | describe('#compilelatedeliveryandlinkfail', function () {
|
127 | it('should fail when compiling and linking a smart Ergo contract to JavaScript', async function () {
|
128 | const ergoFile = Path.resolve(TESTS_DIR, 'latedeliveryandpenalty2', 'logic/logic.ergo');
|
129 | const ergoContent = Fs.readFileSync(ergoFile, 'utf8');
|
130 | const ctoFile = Path.resolve(TESTS_DIR, 'latedeliveryandpenalty2', 'model/model2.cto');
|
131 | const ctoContent = Fs.readFileSync(ctoFile, 'utf8');
|
132 | const result = await ErgoCompiler.compile([{ 'name': ergoFile, 'content' : ergoContent }], [{ 'name': ctoFile, 'content' : ctoContent }], null, 'es6', true);
|
133 | result.should.deep.equal({ 'error' : { 'kind' : 'CompilationError', 'fileName': null, 'message': 'Import not found: org.accordproject.test', 'fullMessage' : 'Compilation error. Import not found: org.accordproject.test', 'locstart' : { 'line' : -1, 'column' : -1 }, 'locend' : { 'line' : -1, 'column' : -1 } } });
|
134 | });
|
135 | });
|
136 | describe('#parsecto', function () {
|
137 | it('should parse CTO', async function () {
|
138 | const ctoText = Fs.readFileSync(Path.resolve(TESTS_DIR, 'helloworld', 'model/model.cto'), 'utf8');
|
139 | const result = ErgoCompiler.parseCTOtoJSON(ctoText);
|
140 | result.should.not.be.null;
|
141 | });
|
142 | });
|
143 | });
|