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] 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' } + ]) + }) + }) }) })