From 8088d19373befc0e4e4d37def8eaea7c641ad07d Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 9 Mar 2015 14:22:29 +0100 Subject: [PATCH 1/2] Squashed 'libnatspec/natspecjs/' content from commit 4f31ed9 git-subtree-dir: libnatspec/natspecjs git-subtree-split: 4f31ed9698be96c347ea769b8b7e77a9507fb615 --- test.js | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 test.js diff --git a/test.js b/test.js new file mode 100644 index 000000000..59f46cdab --- /dev/null +++ b/test.js @@ -0,0 +1,149 @@ +var chai = require('chai'); +var natspec = require('../natspec.js'); +var assert = chai.assert; + +describe('natspec', function () { + it('should evaluate simple expression', function () { + // given + var expression = "`x = 1` + `y = 2` will be equal `x + y`"; + + // when + var result = natspec.evaluateExpression(expression); + var result2 = natspec.evaluateExpressionSafe(expression); + + // then + assert.equal(result, "1 + 2 will be equal 3"); + assert.equal(result2, "1 + 2 will be equal 3"); + }); + + it('should evalute expression using input params', function () { + //given + var expression = "Will multiply `a` by 7 and return `a * 7`."; + var method = 'multiply'; + var abi = [{ + "name": "multiply", + "constant": false, + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + }], + "outputs": [{ + "name": "d", + "type": "uint256" + }] + }]; + + var transaction = { + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" + }], + "id": 6 + }; + + var call = { + method: method, + abi: abi, + transaction: transaction + }; + + // when + var result = natspec.evaluateExpression(expression, call); + var result2 = natspec.evaluateExpressionSafe(expression, call); + + // then + assert.equal(result, "Will multiply 122 by 7 and return 854."); + assert.equal(result2, "Will multiply 122 by 7 and return 854."); + }); + + it('should evalute expression using input params', function () { + //given + var expression = "Will multiply `a` by 7 and return `a * 7`."; + var method = 'multiply'; + var abi = [{ + "name": "multiply", + "constant": false, + "type": "function", + "inputs": [{ + "name": "b", + "type": "uint256" + }], + "outputs": [{ + "name": "d", + "type": "uint256" + }] + }]; + + var transaction = { + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" + }], + "id": 6 + }; + + var call = { + method: method, + abi: abi, + transaction: transaction + }; + + // when + var exceptionThrow = function () { natspec.evaluateExpression(expression, call);} + var result = natspec.evaluateExpressionSafe(expression, call); + + // then + assert.equal(result, "Natspec evaluation failed, wrong input params"); + assert.throws(exceptionThrow, "Natspec evaluation failed, wrong input params"); + }); + + it('should evalute expression using input params', function () { + //given + var expression = "Will multiply `a` by 7 and return `a * 7`."; + var method = 'multiply2'; + var abi = [{ + "name": "multiply", + "constant": false, + "type": "function", + "inputs": [{ + "name": "a", + "type": "uint256" + }], + "outputs": [{ + "name": "d", + "type": "uint256" + }] + }]; + + var transaction = { + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa1000000000000000000000000000000000000000000000000000000000000007a" + }], + "id": 6 + }; + + var call = { + method: method, + abi: abi, + transaction: transaction + }; + + // when + var exceptionThrow = function () { natspec.evaluateExpression(expression, call);} + var result = natspec.evaluateExpressionSafe(expression, call); + + // then + assert.equal(result, "Natspec evaluation failed, method does not exist"); + assert.throws(exceptionThrow, "Natspec evaluation failed, method does not exist"); + }); +}); + + From c260cb235b63d52e8b24fb7c06f2d07e5e1b3c88 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Mon, 9 Mar 2015 15:22:07 +0100 Subject: [PATCH 2/2] fixed natspec evaluation --- natspec.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/natspec.cpp b/natspec.cpp index 8ba660418..cdcedca46 100644 --- a/natspec.cpp +++ b/natspec.cpp @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(natspec_eval_function_exists) // given NatspecExpressionEvaluator e; // when - string result = e.evalExpression("`typeof evaluateExpression`").toStdString(); + string result = e.evalExpression("`typeof natspec.evaluateExpression`").toStdString(); // then BOOST_CHECK_EQUAL(result, "function"); } @@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params) // given char const* abi = R"([ { - "name": "f", + "name": "multiply", "constant": false, "type": "function", "inputs": [ @@ -94,7 +94,18 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_input_params) ] } ])"; - NatspecExpressionEvaluator e(abi, "'f'", "[4]"); + + char const* transaction = R"({ + "jsonrpc": "2.0", + "method": "eth_call", + "params": [{ + "to": "0x8521742d3f456bd237e312d6e30724960f72517a", + "data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000004" + }], + "id": 6 + })"; + + NatspecExpressionEvaluator e(abi, transaction , "multiply"); // when string result = e.evalExpression("Will multiply `a` by 7 and return `a * 7`.").toStdString(); // then @@ -108,7 +119,7 @@ BOOST_AUTO_TEST_CASE(natspec_js_eval_error) // when string result = e.evalExpression("`test(`").toStdString(); // then - BOOST_CHECK_EQUAL(result, "`test(`"); + BOOST_CHECK_EQUAL(result, "Natspec evaluation failed, wrong input params"); } BOOST_AUTO_TEST_SUITE_END()