From bc5de4ef90e7fe3e43594ae956ef756a0c1acbc3 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Thu, 30 Aug 2018 12:49:19 -0400 Subject: [PATCH] Support modifiers --- lib/modules/coverage/contract_source.js | 2 ++ .../coverage_app/contracts/modifiers.sol | 25 ++++++++++++++ test_apps/coverage_app/test/modifiers_spec.js | 34 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 test_apps/coverage_app/contracts/modifiers.sol create mode 100644 test_apps/coverage_app/test/modifiers_spec.js diff --git a/lib/modules/coverage/contract_source.js b/lib/modules/coverage/contract_source.js index 9171d92c..899a97c5 100644 --- a/lib/modules/coverage/contract_source.js +++ b/lib/modules/coverage/contract_source.js @@ -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. diff --git a/test_apps/coverage_app/contracts/modifiers.sol b/test_apps/coverage_app/contracts/modifiers.sol new file mode 100644 index 00000000..828a4ea7 --- /dev/null +++ b/test_apps/coverage_app/contracts/modifiers.sol @@ -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; + } +} diff --git a/test_apps/coverage_app/test/modifiers_spec.js b/test_apps/coverage_app/test/modifiers_spec.js new file mode 100644 index 00000000..ea6d8afa --- /dev/null +++ b/test_apps/coverage_app/test/modifiers_spec.js @@ -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(); + }); + }); +});