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

Commit

Permalink
(refactor) ergo-test is now its own package, depends on chai at runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Mar 26, 2019
1 parent 7c9fc1e commit 6f7c9e1
Show file tree
Hide file tree
Showing 50 changed files with 527 additions and 88 deletions.
6 changes: 6 additions & 0 deletions examples/initemittest/logic.ergo
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ contract InitEmitTest over TemplateModel state State {
return Response{
}
}
clause greet(request:Request) : Response {
set state State {};
emit Greeting{ message: "Voila!" };
return Response{
}
}
}
3 changes: 1 addition & 2 deletions examples/initemittest/state.json
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
{ "$class": "org.accordproject.cicero.contract.AccordContractState",
"stateId" : "1" }
{ "$class": "org.accordproject.initemittest.State" }
2 changes: 1 addition & 1 deletion packages/ergo-cli/.cucumber.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('@accordproject/ergo-engine/lib/steps');
require('@accordproject/ergo-test').Steps;
1 change: 1 addition & 0 deletions packages/ergo-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@accordproject/ergo-compiler": "0.7.3",
"@accordproject/ergo-engine": "0.7.3",
"@accordproject/ergo-test": "0.7.3",
"moment-mini": "2.22.1",
"jsome": "2.5.0",
"winston": "^3.2.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/ergo-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Core classes for executing Ergo contract logic using the JavaScript backend

The Ergo code is compiled to JavaScript and executed using the `vm2` npm module, which provides (somehwat)sandboxed execution.
The Ergo code is compiled to JavaScript and executed using the `vm2` npm module, which provides sandboxed execution.

## Installation

Expand All @@ -12,5 +12,5 @@ npm install @accordproject/ergo-engine --save

## More information

[Ergo Language](https://ergo.accordproject.org)
[Accord Project Documentation](https://docs.accordproject.org)

65 changes: 1 addition & 64 deletions packages/ergo-engine/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,9 @@
// Moment serialization function to preserves utcOffset. See https://momentjs.com/docs/#/displaying/as-json/
const momentToJson = function() { return this.format(); };

const Chai = require('chai');
const expect = Chai.expect;

const Moment = require('moment-mini');
Moment.fn.toJSON = momentToJson;

/**
* Resolve the root directory
*
* @param {string} parameters Cucumber's World parameters
* @return {string} root directory used to resolve file names
*/
function resolveRootDir(parameters) {
if (parameters.rootdir) {
return parameters.rootdir;
} else {
return '.';
}
}

/**
* Ensures there is a proper current time
*
Expand All @@ -56,50 +39,4 @@ function setCurrentTime(currentTime) {
}
}

/**
* Compare actual and expected result components
*
* @param {string} expected the expected component as specified in the test workload
* @param {string} actual the actual component as returned by the engine
*/
function compareComponent(expected,actual) {
if (!expected) {
expect(actual).to.equal(expected);
} else {
delete expected.timestamp;
delete actual.timestamp;
// Some basic deep comparison for arrays, since Chai doesn't do the right thing
if (Array.isArray(actual)) {
for (let i = 0; i < expected.length; i++) {
delete expected[i].timestamp;
delete actual[i].timestamp;
expect(actual[i]).to.deep.include(expected[i]);
}
} else {
expect(actual).to.deep.include(expected);
}
}
}

/**
* Compare actual result and expected result
*
* @param {string} expected the expected successful result as specified in the test workload
* @param {string} actual the successful result as returned by the engine
*/
function compareSuccess(expected,actual) {
if (expected.hasOwnProperty('state')) {
expect(actual).to.have.property('state');
compareComponent(expected.state, actual.state);
}
if (expected.hasOwnProperty('response')) {
expect(actual).to.have.property('response');
compareComponent(expected.response, actual.response);
}
if (expected.hasOwnProperty('emit')) {
expect(actual).to.have.property('emit');
compareComponent(expected.emit, actual.emit);
}
}

module.exports = { momentToJson, resolveRootDir, setCurrentTime, compareComponent, compareSuccess };
module.exports = { momentToJson, setCurrentTime };
4 changes: 1 addition & 3 deletions packages/ergo-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"postlint": "npm run licchk",
"licchk": "license-check",
"test:mocha": "mocha",
"test:cucumber": "cucumber-js test/features --require lib/steps.js --world-parameters '{\"rootdir\":\"./test\"}'",
"test:all": "npm run test:mocha && npm run test:cucumber",
"test:all": "npm run test:mocha",
"test:cov": "nyc npm run test:all",
"test": "npm run test:all && npm run test:cov"
},
Expand All @@ -39,7 +38,6 @@
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"chai-things": "0.2.0",
"cucumber": "^5.1.0",
"eslint": "4.19.1",
"jsdoc": "3.5.5",
"lerna": "2.11.0",
Expand Down
60 changes: 53 additions & 7 deletions packages/ergo-engine/test/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
'use strict';

const Engine = require('../lib/engine');
const Util = require('../lib/util');
const TemplateLogic = require('@accordproject/ergo-compiler').TemplateLogic;

const Chai = require('chai');
const expect = Chai.expect;

Expand All @@ -30,6 +30,52 @@ const Path = require('path');
// Set of tests
const workload = JSON.parse(Fs.readFileSync(Path.resolve(__dirname, 'workload.json'), 'utf8'));

/**
* Compare actual and expected result components
*
* @param {string} expected the expected component as specified in the test workload
* @param {string} actual the actual component as returned by the engine
*/
function compareComponent(expected,actual) {
if (!expected) {
expect(actual).to.equal(expected);
} else {
delete expected.timestamp;
delete actual.timestamp;
// Some basic deep comparison for arrays, since Chai doesn't do the right thing
if (Array.isArray(actual)) {
for (let i = 0; i < expected.length; i++) {
delete expected[i].timestamp;
delete actual[i].timestamp;
expect(actual[i]).to.deep.include(expected[i]);
}
} else {
expect(actual).to.deep.include(expected);
}
}
}

/**
* Compare actual result and expected result
*
* @param {string} expected the expected successful result as specified in the test workload
* @param {string} actual the successful result as returned by the engine
*/
function compareSuccess(expected,actual) {
if (expected.hasOwnProperty('state')) {
expect(actual).to.have.property('state');
compareComponent(expected.state, actual.state);
}
if (expected.hasOwnProperty('response')) {
expect(actual).to.have.property('response');
compareComponent(expected.response, actual.response);
}
if (expected.hasOwnProperty('emit')) {
expect(actual).to.have.property('emit');
compareComponent(expected.emit, actual.emit);
}
}

describe('Execute ES6', () => {

let engine;
Expand Down Expand Up @@ -83,7 +129,7 @@ describe('Execute ES6', () => {
} else {
return engine.compileAndInit(templateLogic, contractName, contractJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
} else {
Expand All @@ -99,7 +145,7 @@ describe('Execute ES6', () => {
} else {
return engine.compileAndInvoke(templateLogic, contractName, clauseName, contractJson, params, stateJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
} else {
Expand All @@ -113,7 +159,7 @@ describe('Execute ES6', () => {
} else {
return engine.compileAndExecute(templateLogic, contractName, contractJson, requestJson, stateJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
}
Expand Down Expand Up @@ -174,7 +220,7 @@ describe('Execute ES5', () => {
} else {
return engine.compileAndInit(templateLogic, contractName, contractJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
} else {
Expand All @@ -190,7 +236,7 @@ describe('Execute ES5', () => {
} else {
return engine.compileAndInvoke(templateLogic, contractName, clauseName, contractJson, params, stateJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
} else {
Expand All @@ -204,7 +250,7 @@ describe('Execute ES5', () => {
} else {
return engine.compileAndExecute(templateLogic, contractName, contractJson, requestJson, stateJson, currentTime)
.then((actualAnswer) => {
return Util.compareSuccess(expected, actualAnswer);
return compareSuccess(expected, actualAnswer);
});
}
}
Expand Down
8 changes: 0 additions & 8 deletions packages/ergo-engine/test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,3 @@ describe('Initialize current time', () => {
return Util.setCurrentTime(undefined).format().should.not.be.null;
});
});
describe('Resolve root directory for Cucumber', () => {
it('Should resolve to given root directory', function () {
return Util.resolveRootDir({rootdir:'foo/bar'}).should.equal('foo/bar');
});
it('Should resolve to \'.\'', function () {
return Util.resolveRootDir({}).should.equal('.');
});
});
42 changes: 42 additions & 0 deletions packages/ergo-engine/test/workload.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@
}
}
},
{
"name": "initemittest",
"dir": "examples/initemittest",
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"request": "request.json",
"state": null,
"contractName": "org.accordproject.initemittest.InitEmitTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.initemittest.Response"
},
"state": {
"$class": "org.accordproject.initemittest.State"
}
}
},
{
"name": "initemittest (greeting)",
"dir": "examples/initemittest",
"ergo": ["logic.ergo"],
"models": ["model.cto"],
"contract": "contract.json",
"request": "request.json",
"state": "state.json",
"contractName": "org.accordproject.initemittest.InitEmitTest",
"currentTime": null,
"expected": {
"response": {
"$class": "org.accordproject.initemittest.Response"
},
"state": {
"$class": "org.accordproject.initemittest.State"
},
"emit": [{
"$class": "org.accordproject.initemittest.Greeting",
"message": "Voila!"
}]
}
},
{
"name": "bad logic",
"dir": "examples/bad-logic",
Expand Down
6 changes: 6 additions & 0 deletions packages/ergo-test/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage
docs
node_modules
out
test/data
lib/momenttojson.js
48 changes: 48 additions & 0 deletions packages/ergo-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
env:
es6: true
node: true
mocha: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 8
sourceType:
- script
rules:
indent:
- error
- 4
linebreak-style:
- error
- unix
quotes:
- error
- single
semi:
- error
- always
no-unused-vars:
- error
- args: none
no-console: warn
curly: error
eqeqeq: error
no-throw-literal: error
strict: error
no-var: error
dot-notation: error
no-tabs: error
no-trailing-spaces: error
# no-use-before-define: error
no-useless-call: error
no-with: error
operator-linebreak: error
require-jsdoc:
- error
- require:
ClassDeclaration: true
MethodDefinition: true
FunctionDeclaration: true
valid-jsdoc:
- error
- requireReturn: false
yoda: error
7 changes: 7 additions & 0 deletions packages/ergo-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*~
.#*
.DS_Store
node_modules/
log/
.nyc_output
coverage
Loading

0 comments on commit 6f7c9e1

Please sign in to comment.