compare strings with keccak256

pull/3094/head
0mkar 6 years ago
parent ed2af0315a
commit 22105e3ef2
  1. 2
      package.json
  2. 16
      sol/tests.sol.js
  3. 16
      tests/examples_3/simple_string.sol
  4. 15
      tests/examples_3/simple_string_test.sol
  5. 35
      tests/testRunner.js

@ -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"

@ -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);
}
}
`

@ -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;
}
}

@ -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");
}
}

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

Loading…
Cancel
Save