From e64c928896e12438d8a680f0607cb35c8b02c80d Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Wed, 22 Aug 2018 19:39:22 +0530 Subject: [PATCH 1/2] compare strings with keccak256 --- package.json | 2 +- sol/tests.sol.js | 16 +++++------ tests/examples_3/simple_string.sol | 16 +++++++++++ tests/examples_3/simple_string_test.sol | 15 +++++++++++ tests/testRunner.js | 35 +++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 tests/examples_3/simple_string.sol create mode 100644 tests/examples_3/simple_string_test.sol diff --git a/package.json b/package.json index ffee15ffc4..e313928b5b 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "colors": "^1.1.2", "commander": "^2.13.0", "remix-simulator": "latest", - "remix-solidity": "https://github.com/0mkara/remix-solidity", + "remix-solidity": "^0.2.2", "solc": "^0.4.24", "standard": "^10.0.3", "web3": "1.0.0-beta.27" diff --git a/sol/tests.sol.js b/sol/tests.sol.js index 2fa6821bbe..f766da8041 100644 --- a/sol/tests.sol.js +++ b/sol/tests.sol.js @@ -51,10 +51,10 @@ library Assert { } // TODO: needs to be convert to bytes first to be comparable - //function equal(string a, string b, string message) public returns (bool result) { - // result = (a == b); - // AssertionEvent(result, message); - //} + function equal(string a, string b, string message) public returns (bool result) { + result = (keccak256(a) == keccak256(b)); + AssertionEvent(result, message); + } function notEqual(uint a, uint b, string message) public returns (bool result) { result = (a != b); @@ -94,10 +94,10 @@ library Assert { } // TODO: needs to be convert to bytes first to be comparable - //function notEqual(string a, string b, string message) public returns (bool result) { - // result = (a != b); - // AssertionEvent(result, message); - //} + function notEqual(string a, string b, string message) public returns (bool result) { + result = (keccak256(a) != keccak256(b)); + AssertionEvent(result, message); + } } ` diff --git a/tests/examples_3/simple_string.sol b/tests/examples_3/simple_string.sol new file mode 100644 index 0000000000..597c15afb0 --- /dev/null +++ b/tests/examples_3/simple_string.sol @@ -0,0 +1,16 @@ +pragma solidity ^0.4.7; +contract SimpleString { + string public storedData; + + function SimpleString() public { + storedData = "Hello"; + } + + function set(string x) public { + storedData = x; + } + + function get() public view returns (string retVal) { + return storedData; + } +} diff --git a/tests/examples_3/simple_string_test.sol b/tests/examples_3/simple_string_test.sol new file mode 100644 index 0000000000..415ea17165 --- /dev/null +++ b/tests/examples_3/simple_string_test.sol @@ -0,0 +1,15 @@ +pragma solidity ^0.4.7; +import "./tests.sol"; +import "./simple_string.sol"; + +contract MyTest { + SimpleString foo; + + function beforeAll() { + foo = new SimpleString(); + } + + function initialValueShouldBeHello() public constant returns (bool) { + return Assert.equal(foo.get(), "Hello", "initial value is not correct"); + } +} diff --git a/tests/testRunner.js b/tests/testRunner.js index b6426f5ce5..4dcb7ca261 100644 --- a/tests/testRunner.js +++ b/tests/testRunner.js @@ -94,5 +94,40 @@ describe('testRunner', function () { ]) }) }) + + // Test string comparision + describe('test with beforeAll', function () { + let filename = 'tests/examples_3/simple_string_test.sol' + let tests = [], results = {} + + before(function (done) { + compileAndDeploy(filename, function (_err, contracts) { + var testCallback = function (test) { + tests.push(test) + } + var resultsCallback = function (_err, _results) { + results = _results + console.log(results) + done() + } + TestRunner.runTest('MyTest', contracts.MyTest, testCallback, resultsCallback) + }) + }) + + it('should 1 passing tests', function () { + assert.equal(results.passingNum, 1) + }) + + it('should 0 failing tests', function () { + assert.equal(results.failureNum, 0) + }) + + it('should returns 2 messages', function () { + assert.deepEqual(tests, [ + { type: 'contract', value: 'MyTest', filename: 'simple_string_test.sol' }, + { type: 'testPass', value: 'Initial value should be hello', time: 1, context: 'MyTest' } + ]) + }) + }) }) }) From 08c8cbeefac3ae3ba4d9253b37b7521af660d66f Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Thu, 23 Aug 2018 00:42:30 +0530 Subject: [PATCH 2/2] improve string tests --- tests/examples_3/simple_string.sol | 6 +----- tests/examples_3/simple_string_test.sol | 8 ++++++-- tests/testRunner.js | 17 +++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/examples_3/simple_string.sol b/tests/examples_3/simple_string.sol index 597c15afb0..25773f148c 100644 --- a/tests/examples_3/simple_string.sol +++ b/tests/examples_3/simple_string.sol @@ -3,11 +3,7 @@ contract SimpleString { string public storedData; function SimpleString() public { - storedData = "Hello"; - } - - function set(string x) public { - storedData = x; + storedData = "Hello world!"; } function get() public view returns (string retVal) { diff --git a/tests/examples_3/simple_string_test.sol b/tests/examples_3/simple_string_test.sol index 415ea17165..4f8109764a 100644 --- a/tests/examples_3/simple_string_test.sol +++ b/tests/examples_3/simple_string_test.sol @@ -2,7 +2,7 @@ pragma solidity ^0.4.7; import "./tests.sol"; import "./simple_string.sol"; -contract MyTest { +contract StringTest { SimpleString foo; function beforeAll() { @@ -10,6 +10,10 @@ contract MyTest { } function initialValueShouldBeHello() public constant returns (bool) { - return Assert.equal(foo.get(), "Hello", "initial value is not correct"); + return Assert.equal(foo.get(), "Hello world!", "initial value is not correct"); + } + + function valueShouldBeHelloWorld() public constant returns (bool) { + return Assert.equal(foo.get(), "Hello wordl!", "initial value is not correct"); } } diff --git a/tests/testRunner.js b/tests/testRunner.js index 4dcb7ca261..2eda171678 100644 --- a/tests/testRunner.js +++ b/tests/testRunner.js @@ -50,7 +50,7 @@ describe('testRunner', function () { assert.equal(results.failureNum, 2) }) - it('should returns 3 messages', function () { + it('should returns 5 messages', function () { assert.deepEqual(tests, [ { type: 'contract', value: 'MyTest', filename: 'simple_storage_test.sol' }, { type: 'testFailure', value: 'Should trigger one fail', time: 1, context: 'MyTest', errMsg: 'the test 1 fails' }, @@ -107,10 +107,10 @@ describe('testRunner', function () { } var resultsCallback = function (_err, _results) { results = _results - console.log(results) done() } - TestRunner.runTest('MyTest', contracts.MyTest, testCallback, resultsCallback) + TestRunner.runTest('StringTest', contracts.StringTest, testCallback, resultsCallback) + TestRunner.runTest('StringTest2', contracts.StringTest2, testCallback, resultsCallback) }) }) @@ -118,14 +118,15 @@ describe('testRunner', function () { assert.equal(results.passingNum, 1) }) - it('should 0 failing tests', function () { - assert.equal(results.failureNum, 0) + it('should 1 failing tests', function () { + assert.equal(results.failureNum, 1) }) - it('should returns 2 messages', function () { + it('should returns 3 messages', function () { assert.deepEqual(tests, [ - { type: 'contract', value: 'MyTest', filename: 'simple_string_test.sol' }, - { type: 'testPass', value: 'Initial value should be hello', time: 1, context: 'MyTest' } + { type: 'contract', value: 'StringTest', filename: 'simple_string_test.sol' }, + { type: 'testFailure', value: 'Value should be hello world', time: 1, context: 'StringTest', "errMsg": "function returned false" }, + { type: 'testPass', value: 'Initial value should be hello', time: 1, context: 'StringTest' }, ]) }) })