Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
fix(wasm) More tests for WASM + engine
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Aug 19, 2020
1 parent 1b7b3d9 commit c3883e4
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/ergo-engine/test/buildengine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const Chai = require('chai');

Chai.should();
Chai.use(require('chai-things'));
Chai.use(require('chai-as-promised'));

const buildEngine = require('../lib/buildengine');

describe('#buildengine', () => {
it('should build a VM engine for ES6', async () => {
const engine = buildEngine('es6',false);
engine.kind().should.equal('vm2');
});

it('should build a eval engine for ES6', async () => {
const engine = buildEngine('es6',true);
engine.kind().should.equal('eval');
});

it('should build an engine for WASM', async () => {
const engine = buildEngine('wasm',false);
engine.kind().should.equal('wasm');
});
});
3 changes: 2 additions & 1 deletion packages/ergo-engine/test/evalengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const EvalEngine = require('../lib/evalengine');
const LogicManager = require('@accordproject/ergo-compiler').LogicManager;

// Set of tests
const workload = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, 'workload.json'), 'utf8'));
const workload = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, 'workload_es6.json'), 'utf8'));

describe('#evalengine', () => {
it('should behave as a proper Eval Engine', () => {
const engine = new EvalEngine();
engine.kind().should.equal('eval');
engine.instantiate('const a = 1;').should.not.be.null;
engine.invokeCall(2,null,null,{ a : 1 },'class C { static f() { return context.a + utcOffset; } }','C','f').should.equal(3);
(() => engine.invokeCall(2,null,null,{ a : 1 },'class C { static f() { return context.a + utcOffset; } }',null,'f')).should.throw('Cannot invoke contract without a contract name');
});

it('should cache a script', async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/ergo-engine/test/vmengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ const VMEngine = require('../lib/vmengine');
const LogicManager = require('@accordproject/ergo-compiler').LogicManager;

// Set of tests
const workload = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, 'workload.json'), 'utf8'));
const workload = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, 'workload_es6.json'), 'utf8'));

describe('#vmengine', () => {
it('should behave as a proper VM engine', async () => {
const engine = new VMEngine();
engine.kind().should.equal('vm2');
engine.instantiate('const a = 1;').should.not.be.null;
expect (await engine.invokeCall(2,null,null,{ a : 1 },'class C { static f() { return context.a + utcOffset; } }','C','f')).to.equal(3);
(() => engine.invokeCall(2,null,null,{ a : 1 },'class C { static f() { return context.a + utcOffset; } }',null,'f')).should.throw('Cannot invoke contract without a contract name');
});

it('should cache a script', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,59 @@
"expected": {
"response": "\nThis is a fixed interest loan to the amount of 100000.0\nat the yearly interest rate of 2.5%\nwith a loan term of 15,\nand monthly payments of 667.0\n"
}
},
{
"name": "helloworldenforce",
"dir": "../../../tests/helloworldenforce",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state.json",
"contractName": "org.accordproject.helloworld.HelloWorld",
"currentTime": null,
"expected": {
"error": "[Ergo] Hello world is disabled"
}
},
{
"name": "statetest",
"dir": "../../../tests/statetest",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state1.json",
"contractName": "org.accordproject.statetest.StateTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.statetest.MyResponse"
},
"state": {
"$class": "org.accordproject.statetest.State",
"counter": 7
}
}
},
{
"name": "statetest",
"dir": "../../../tests/statetest",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state2.json",
"contractName": "org.accordproject.statetest.StateTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.statetest.MyResponse"
},
"state": {
"$class": "org.accordproject.statetest.State",
"counter": 8
}
}
}
]
71 changes: 71 additions & 0 deletions packages/ergo-engine/test/workload_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,76 @@
"output" : "Bonjour, Fred Blogs (Accord Project)"
}
}
},
{
"name": "helloworldenforce",
"dir": "../../../tests/helloworldenforce",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state.json",
"contractName": "org.accordproject.helloworld.HelloWorld",
"currentTime": null,
"expected": {
"error": "[Ergo] Hello world is disabled"
}
},
{
"name": "constanttest",
"dir": "../../../tests/constanttest",
"ergo": ["logic/library.ergo","logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state.json",
"contractName": "org.accordproject.constanttest.EnumTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.constanttest.Response",
"thing": 3.14
}
}
},
{
"name": "statetest",
"dir": "../../../tests/statetest",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state1.json",
"contractName": "org.accordproject.statetest.StateTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.statetest.MyResponse"
},
"state": {
"$class": "org.accordproject.statetest.State",
"counter": 7
}
}
},
{
"name": "statetest",
"dir": "../../../tests/statetest",
"ergo": ["logic/logic.ergo"],
"models": ["model/model.cto"],
"contract": "data.json",
"request": "request.json",
"state": "state2.json",
"contractName": "org.accordproject.statetest.StateTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.statetest.MyResponse"
},
"state": {
"$class": "org.accordproject.statetest.State",
"counter": 8
}
}
}
]
4 changes: 4 additions & 0 deletions tests/statetest/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$class": "org.accordproject.statetest.TemplateModel",
"init": 0
}
31 changes: 31 additions & 0 deletions tests/statetest/logic/logic.ergo
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.statetest

contract StateTest over TemplateModel state State {
// Simple Clause
clause next(request : MyRequest) : MyResponse {
set state.counter = state.counter + request.input;
return MyResponse{
}
}

clause init() : Unit {
set state State{
counter: contract.init
};
return
}
}
35 changes: 35 additions & 0 deletions tests/statetest/model/model.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.statetest

transaction MyRequest {
o Integer input
}

transaction MyResponse {
}

concept State {
o Integer counter
}

/**
* The template model
*/
concept TemplateModel {
o Integer init
}

// this is a change!
4 changes: 4 additions & 0 deletions tests/statetest/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$class": "org.accordproject.statetest.MyRequest",
"input": 7
}
4 changes: 4 additions & 0 deletions tests/statetest/state1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$class": "org.accordproject.statetest.State",
"counter" : 0
}
4 changes: 4 additions & 0 deletions tests/statetest/state2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$class": "org.accordproject.statetest.State",
"counter" : 1
}

0 comments on commit c3883e4

Please sign in to comment.