Support modifiers

This commit is contained in:
Andre Medeiros 2018-08-30 12:49:19 -04:00
parent 824353fb32
commit bc5de4ef90
3 changed files with 61 additions and 0 deletions

View File

@ -128,6 +128,7 @@ class ContractSource {
case 'Assignment':
case 'EventDefinition':
case 'Literal':
case 'PlaceholderStatement':
case 'PragmaDirective':
case 'VariableDeclaration':
// We don't need to do anything with these. Just carry on.
@ -207,6 +208,7 @@ class ContractSource {
children = node.nodes;
break;
case 'ModifierDefinition':
case 'FunctionDefinition':
// Istanbul only wants the function definition, not the body, so we're
// going to do some fun math here.

View File

@ -0,0 +1,25 @@
pragma solidity ^0.4.23;
contract Modifiers {
uint public storedData;
constructor(uint initialValue) public {
storedData = initialValue;
}
modifier upTo(uint amount, uint desired) {
require(
desired <= amount,
"Value is too high."
);
_;
}
function set(uint x) public upTo(1000, x) {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}

View File

@ -0,0 +1,34 @@
/*global contract, config, it, assert*/
const Modifiers = require('Embark/contracts/Modifiers');
config({
contracts: {
"Modifiers": {
args: [100]
}
}
});
contract("Modifiers", function() {
it("should set constructor value", function(done) {
Modifiers.methods.storedData().call().then((result) => {
assert.strictEqual(parseInt(result, 10), 100);
done();
});
});
it("set storage value when valid", function(done) {
Modifiers.methods.set(150).send().then(() => {
Modifiers.methods.get().call().then((result) => {
assert.strictEqual(parseInt(result, 10), 150);
done();
});
});
});
it("refuses storage value when invalid", function(done) {
Modifiers.methods.set(10000).send().catch((_err) => {
done();
});
});
});