From 0e43174deeb1fcc0e1ad49eed1e2aaf96cd5847d Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 1 Sep 2018 15:56:32 +0530 Subject: [PATCH] add number weight tests --- remix-tests/sol/tests.sol.js | 40 ++++++++++++++++++++++ remix-tests/tests/integer/integer_test.sol | 15 ++++++++ remix-tests/tests/testRunner.js | 30 ++++++++++++++-- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 remix-tests/tests/integer/integer_test.sol diff --git a/remix-tests/sol/tests.sol.js b/remix-tests/sol/tests.sol.js index b53cdaba67..23dceef449 100644 --- a/remix-tests/sol/tests.sol.js +++ b/remix-tests/sol/tests.sol.js @@ -97,5 +97,45 @@ library Assert { AssertionEvent(result, message); } + /*----------------- Greater than --------------------*/ + function greaterThan(uint a, uint b, string message) public constant returns (bool result) { + result = (a > b); + emit AssertionEvent(result, message); + } + + function greaterThan(int a, int b, string message) public constant returns (bool result) { + result = (a > b); + emit AssertionEvent(result, message); + } + // TODO: safely compare between uint and int + function greaterThan(uint a, int b, string message) public constant returns (bool result) { + if(b < int(0)) { + // int is negative a always greater + result = true; + } else { + result = (a > uint(b)); + } + emit AssertionEvent(result, message); + } + function greaterThan(int a, uint b, string message) public constant returns (bool result) { + if(a < int(0)) { + // int is negative a always greater + result = false; + } else { + result = (uint(a) > b); + } + emit AssertionEvent(result, message); + } + /*----------------- Lesser than --------------------*/ + function lesserThan(uint a, uint b, string message) public constant returns (bool result) { + result = (a < b); + emit AssertionEvent(result, message); + } + + function lesserThan(int a, int b, string message) public constant returns (bool result) { + result = (a < b); + emit AssertionEvent(result, message); + } + // TODO: safely compare between uint and int } ` diff --git a/remix-tests/tests/integer/integer_test.sol b/remix-tests/tests/integer/integer_test.sol new file mode 100644 index 0000000000..51df2af8bf --- /dev/null +++ b/remix-tests/tests/integer/integer_test.sol @@ -0,0 +1,15 @@ +pragma solidity ^0.4.24; + +contract IntegerTest { + function _2_shouldBeGreaterThan_1() public constant returns (bool) { + return Assert.greaterThan(uint(2), uint(1), "2 is not greater than 1"); + } + + function _2_shouldBeGreaterThan_neg_1() public constant returns (bool) { + return Assert.greaterThan(uint(2), int(-1), "2 is not greater than -1"); + } + + function _neg_1_shouldNotBeGreaterThan_2() public constant returns (bool) { + return Assert.greaterThan(int(-1), uint(2), "-1 is not greater than 2"); + } +} diff --git a/remix-tests/tests/testRunner.js b/remix-tests/tests/testRunner.js index 7875461c06..3c9b7950a0 100644 --- a/remix-tests/tests/testRunner.js +++ b/remix-tests/tests/testRunner.js @@ -95,8 +95,8 @@ describe('testRunner', function () { }) }) - // Test string comparision - describe('test with beforeAll', function () { + // Test string equality + describe('test string equality', function () { let filename = 'tests/examples_3/simple_string_test.sol' let tests = [], results = {} @@ -131,5 +131,31 @@ describe('testRunner', function () { ]) }) }) + + // Test signed/unsigned integer weight + describe('test number weight', function () { + let filename = 'tests/integer/integer_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 + done() + } + TestRunner.runTest('IntegerTest', contracts.IntegerTest, testCallback, resultsCallback) + }) + }) + + it('should have 2 passing tests', function () { + assert.equal(results.passingNum, 2) + }) + it('should have 1 failing tests', function () { + assert.equal(results.failureNum, 1) + }) + }) }) })