From 25cbec92ed0cec2c92b60c062e7445bcbcc4796c Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 1 Sep 2018 15:56:32 +0530 Subject: [PATCH 1/6] 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) + }) + }) }) }) From 48a92a705ab5e6afa715b4f18c71ed811e0a2715 Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 1 Sep 2018 16:24:33 +0530 Subject: [PATCH 2/6] rename to number test. Add 2^3 = 8 combinations of uint & int tests. --- remix-tests/sol/tests.sol.js | 23 +++++++++++-- remix-tests/tests/integer/integer_test.sol | 15 --------- remix-tests/tests/number/number_test.sol | 38 ++++++++++++++++++++++ remix-tests/tests/testRunner.js | 10 +++--- 4 files changed, 64 insertions(+), 22 deletions(-) delete mode 100644 remix-tests/tests/integer/integer_test.sol create mode 100644 remix-tests/tests/number/number_test.sol diff --git a/remix-tests/sol/tests.sol.js b/remix-tests/sol/tests.sol.js index 23dceef449..ac0c98978a 100644 --- a/remix-tests/sol/tests.sol.js +++ b/remix-tests/sol/tests.sol.js @@ -110,7 +110,7 @@ library Assert { // 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 + // int is negative uint "a" always greater result = true; } else { result = (a > uint(b)); @@ -119,7 +119,7 @@ library Assert { } function greaterThan(int a, uint b, string message) public constant returns (bool result) { if(a < int(0)) { - // int is negative a always greater + // int is negative uint "b" always greater result = false; } else { result = (uint(a) > b); @@ -137,5 +137,24 @@ library Assert { emit AssertionEvent(result, message); } // TODO: safely compare between uint and int + function lesserThan(uint a, int b, string message) public constant returns (bool result) { + if(b < int(0)) { + // int is negative int "b" always lesser + result = false; + } else { + result = (a < uint(b)); + } + emit AssertionEvent(result, message); + } + + function lesserThan(int a, uint b, string message) public constant returns (bool result) { + if(a < int(0)) { + // int is negative int "a" always lesser + result = true; + } else { + result = (uint(a) < b); + } + emit AssertionEvent(result, message); + } } ` diff --git a/remix-tests/tests/integer/integer_test.sol b/remix-tests/tests/integer/integer_test.sol deleted file mode 100644 index 51df2af8bf..0000000000 --- a/remix-tests/tests/integer/integer_test.sol +++ /dev/null @@ -1,15 +0,0 @@ -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/number/number_test.sol b/remix-tests/tests/number/number_test.sol new file mode 100644 index 0000000000..9098490215 --- /dev/null +++ b/remix-tests/tests/number/number_test.sol @@ -0,0 +1,38 @@ +pragma solidity ^0.4.24; + +contract IntegerTest { + + // GREATER THAN [>] tests + function _2_shouldBeGreaterThan_1() public constant returns (bool) { + return Assert.greaterThan(uint(2), uint(1), "2 is greater than 1"); + } + + function _0_shouldBeGreaterThan_neg_1() public constant returns (bool) { + return Assert.greaterThan(uint(0), int(-1), "0 is greater than -1"); + } + + function _neg_1_shouldNotBeGreaterThan_1() public constant returns (bool) { + return Assert.greaterThan(int(-1), uint(1), "-1 is not greater than 1"); + } + + function _1_shouldBeGreaterThan_neg_1() public constant returns (bool) { + return Assert.greaterThan(uint(1), int(-1), "1 is greater than -1"); + } + + // LESSER THAN [<] tests + function _1_shouldBeLesserThan_2() public constant returns (bool) { + return Assert.lesserThan(uint(1), uint(2), "1 is lesser than 2"); + } + + function _neg_1_shouldBeLesserThan_0() public constant returns (bool) { + return Assert.lesserThan(int(-1), uint(0), "-1 is lesser than 0"); + } + + function _neg_2_shouldBeLesserThan_neg_1() public constant returns (bool) { + return Assert.lesserThan(int(-2), int(-1), "-2 is lesser than -1"); + } + + function _0_shouldNotBeLesserThan_neg_1() public constant returns (bool) { + return Assert.lesserThan(uint(0), int(-1), "0 is not lesser than -1"); + } +} diff --git a/remix-tests/tests/testRunner.js b/remix-tests/tests/testRunner.js index 3c9b7950a0..ccdd0c84b7 100644 --- a/remix-tests/tests/testRunner.js +++ b/remix-tests/tests/testRunner.js @@ -134,7 +134,7 @@ describe('testRunner', function () { // Test signed/unsigned integer weight describe('test number weight', function () { - let filename = 'tests/integer/integer_test.sol' + let filename = 'tests/number/number_test.sol' let tests = [], results = {} before(function (done) { @@ -150,11 +150,11 @@ describe('testRunner', function () { }) }) - it('should have 2 passing tests', function () { - assert.equal(results.passingNum, 2) + it('should have 6 passing tests', function () { + assert.equal(results.passingNum, 6) }) - it('should have 1 failing tests', function () { - assert.equal(results.failureNum, 1) + it('should have 2 failing tests', function () { + assert.equal(results.failureNum, 2) }) }) }) 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 3/6] 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) + }) + }) }) }) From 2791d61ee62cd2d0166d9818a87dbd5c6dc98aec Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 1 Sep 2018 16:24:33 +0530 Subject: [PATCH 4/6] rename to number test. Add 2^3 = 8 combinations of uint & int tests. --- remix-tests/sol/tests.sol.js | 23 +++++++++++-- remix-tests/tests/integer/integer_test.sol | 15 --------- remix-tests/tests/number/number_test.sol | 38 ++++++++++++++++++++++ remix-tests/tests/testRunner.js | 10 +++--- 4 files changed, 64 insertions(+), 22 deletions(-) delete mode 100644 remix-tests/tests/integer/integer_test.sol create mode 100644 remix-tests/tests/number/number_test.sol diff --git a/remix-tests/sol/tests.sol.js b/remix-tests/sol/tests.sol.js index 23dceef449..ac0c98978a 100644 --- a/remix-tests/sol/tests.sol.js +++ b/remix-tests/sol/tests.sol.js @@ -110,7 +110,7 @@ library Assert { // 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 + // int is negative uint "a" always greater result = true; } else { result = (a > uint(b)); @@ -119,7 +119,7 @@ library Assert { } function greaterThan(int a, uint b, string message) public constant returns (bool result) { if(a < int(0)) { - // int is negative a always greater + // int is negative uint "b" always greater result = false; } else { result = (uint(a) > b); @@ -137,5 +137,24 @@ library Assert { emit AssertionEvent(result, message); } // TODO: safely compare between uint and int + function lesserThan(uint a, int b, string message) public constant returns (bool result) { + if(b < int(0)) { + // int is negative int "b" always lesser + result = false; + } else { + result = (a < uint(b)); + } + emit AssertionEvent(result, message); + } + + function lesserThan(int a, uint b, string message) public constant returns (bool result) { + if(a < int(0)) { + // int is negative int "a" always lesser + result = true; + } else { + result = (uint(a) < b); + } + emit AssertionEvent(result, message); + } } ` diff --git a/remix-tests/tests/integer/integer_test.sol b/remix-tests/tests/integer/integer_test.sol deleted file mode 100644 index 51df2af8bf..0000000000 --- a/remix-tests/tests/integer/integer_test.sol +++ /dev/null @@ -1,15 +0,0 @@ -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/number/number_test.sol b/remix-tests/tests/number/number_test.sol new file mode 100644 index 0000000000..9098490215 --- /dev/null +++ b/remix-tests/tests/number/number_test.sol @@ -0,0 +1,38 @@ +pragma solidity ^0.4.24; + +contract IntegerTest { + + // GREATER THAN [>] tests + function _2_shouldBeGreaterThan_1() public constant returns (bool) { + return Assert.greaterThan(uint(2), uint(1), "2 is greater than 1"); + } + + function _0_shouldBeGreaterThan_neg_1() public constant returns (bool) { + return Assert.greaterThan(uint(0), int(-1), "0 is greater than -1"); + } + + function _neg_1_shouldNotBeGreaterThan_1() public constant returns (bool) { + return Assert.greaterThan(int(-1), uint(1), "-1 is not greater than 1"); + } + + function _1_shouldBeGreaterThan_neg_1() public constant returns (bool) { + return Assert.greaterThan(uint(1), int(-1), "1 is greater than -1"); + } + + // LESSER THAN [<] tests + function _1_shouldBeLesserThan_2() public constant returns (bool) { + return Assert.lesserThan(uint(1), uint(2), "1 is lesser than 2"); + } + + function _neg_1_shouldBeLesserThan_0() public constant returns (bool) { + return Assert.lesserThan(int(-1), uint(0), "-1 is lesser than 0"); + } + + function _neg_2_shouldBeLesserThan_neg_1() public constant returns (bool) { + return Assert.lesserThan(int(-2), int(-1), "-2 is lesser than -1"); + } + + function _0_shouldNotBeLesserThan_neg_1() public constant returns (bool) { + return Assert.lesserThan(uint(0), int(-1), "0 is not lesser than -1"); + } +} diff --git a/remix-tests/tests/testRunner.js b/remix-tests/tests/testRunner.js index 3c9b7950a0..ccdd0c84b7 100644 --- a/remix-tests/tests/testRunner.js +++ b/remix-tests/tests/testRunner.js @@ -134,7 +134,7 @@ describe('testRunner', function () { // Test signed/unsigned integer weight describe('test number weight', function () { - let filename = 'tests/integer/integer_test.sol' + let filename = 'tests/number/number_test.sol' let tests = [], results = {} before(function (done) { @@ -150,11 +150,11 @@ describe('testRunner', function () { }) }) - it('should have 2 passing tests', function () { - assert.equal(results.passingNum, 2) + it('should have 6 passing tests', function () { + assert.equal(results.passingNum, 6) }) - it('should have 1 failing tests', function () { - assert.equal(results.failureNum, 1) + it('should have 2 failing tests', function () { + assert.equal(results.failureNum, 2) }) }) }) From 60e55c9eea16ec277b45abc396ddfd3d57d97e05 Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 22 Sep 2018 11:41:02 +0530 Subject: [PATCH 5/6] Update remix-tests readme --- remix-tests/README.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/remix-tests/README.md b/remix-tests/README.md index efdfd25245..f18a3f48e4 100644 --- a/remix-tests/README.md +++ b/remix-tests/README.md @@ -44,21 +44,22 @@ contract MyTest { ``` Available special functions: -* `beforeEach` - runs before each test -* `beforeAll` - runs before all tests +* `beforeEach()` - runs before each test +* `beforeAll()` - runs before all tests #### Assert library -Available functions: -`Assert.ok(value, message)` -`Assert.equal(value1, value2, message)` -`Assert.notEqual(value1, value2, message)` - -supported values currently are: `bool` `uint` `int` `address` `bytes32` +| Available functions | Supported types | +| ------------- | ------------- | +| `Assert.ok()` | `bool` | +| `Assert.equal()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` | +| `Assert.notEqual()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` | +| `Assert.greaterThan()` | `uint`, `int` | +| `Assert.lesserThan()` | `uint`, `int` | ### Command Line -Remix-Tests will assume the tests will files whose name end with "_test.sol". e.g `simple_storage_test.sol` +Remix-Tests will assume the tests will files whose name end with `"_test.sol"`. e.g `simple_storage_test.sol` Usage: From 46f0105019531aae46de21ba7234da46ede5aecd Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Sat, 22 Sep 2018 14:56:08 +0530 Subject: [PATCH 6/6] Add to readme --- docs/unittesting_tab.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/unittesting_tab.md b/docs/unittesting_tab.md index 401a29a036..e128529dcd 100644 --- a/docs/unittesting_tab.md +++ b/docs/unittesting_tab.md @@ -16,9 +16,16 @@ Run Tests This execute tests. The execution is run in a separate environment and the result is displayed below. +| Available functions | Supported types | +| ------------- | ------------- | +| `Assert.ok()` | `bool` | +| `Assert.equal()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` | +| `Assert.notEqual()` | `uint`, `int`, `bool`, `address`, `bytes32`, `string` | +| `Assert.greaterThan()` | `uint`, `int` | +| `Assert.lesserThan()` | `uint`, `int` | + Continuous integration ---------------------- remix-tests is also a CLI, it can be used in a continuous integration environement which support node.js. Please find more information in the [remix-test repository](https://github.com/ethereum/remix/tree/master/remix-tests) -