diff --git a/docs/assert_library.md b/docs/assert_library.md new file mode 100644 index 0000000000..8b33971294 --- /dev/null +++ b/docs/assert_library.md @@ -0,0 +1,95 @@ +Remix Assert Library +==================== + +* [Assert.ok(value[, message])](#assert-ok-value-message) +* [Assert.equal(actual, expected[, message])](#assert-equal-actual-expected-message) +* [Assert.notEqual(actual, expected[, message])](#assert-notequal-actual-expected-message) +* [Assert.greaterThan(value1, value2[, message])](#assert-greaterthan-value1-value2-message) +* [Assert.lesserThan(value1, value2[, message])](#assert-lesserthan-value1-value2-message) + + +## Assert + +### Assert.ok(value[, message]) +* `value`: \ +* `message`: \ + +Tests if value is truthy. `message` is returned in case of failure. + +Examples: +``` +Assert.ok(true); +// OK +Assert.ok(false, "it\'s false"); +// error: it's false +``` + +### Assert.equal(actual, expected[, message]) +* `actual`: \ +* `expected`: \ +* `message`: \ + +Tests if `actual` & `expected` values are same. `message` is returned in case of failure. + +Examples: +``` +Assert.equal(string("a"), "a"); +// OK +Assert.equal(uint(100), 100); +// OK +foo.set(200) +Assert.equal(foo.get(), 200); +// OK +Assert.equal(foo.get(), 100, "value should be 200"); +// error: value should be 200 +``` + +### Assert.notEqual(actual, expected[, message]) +* `actual`: \ +* `expected`: \ +* `message`: \ + +Tests if `actual` & `expected` values are not same. `message` is returned in case of failure. + +Examples: +``` +Assert.notEqual(string("a"), "b"); +// OK +foo.set(200) +Assert.notEqual(foo.get(), 200, "value should not be 200"); +// error: value should not be 200 +``` + +### Assert.greaterThan(value1, value2[, message]) +* `value1`: \ +* `value2`: \ +* `message`: \ + +Tests if `value1` is greater than `value2`. `message` is returned in case of failure. + +Examples: +``` +Assert.greaterThan(uint(2), uint(1)); +// OK +Assert.greaterThan(uint(-2), uint(1)); +// OK +Assert.greaterThan(int(2), int(1)); +// OK +Assert.greaterThan(int(-2), int(-1), "-2 is not greater than -1"); +// error: -2 is not greater than -1 +``` + +### Assert.lesserThan(value1, value2[, message]) +* `value1`: \ +* `value2`: \ +* `message`: \ + +Tests if `value1` is lesser than `value2`. `message` is returned in case of failure. + +Examples: +``` +Assert.lesserThan(int(-2), int(-1)); +// OK +Assert.lesserThan(int(2), int(1), "2 is not lesser than 1"); +// error: 2 is not greater than 1 +``` \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index adc9ae3688..132412df5f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -65,6 +65,7 @@ Useful links: remix_commands remixd unittesting + assert_library .. toctree:: :maxdepth: 2 diff --git a/docs/unittesting.md b/docs/unittesting.md index 9c88a05c6f..9da5334609 100644 --- a/docs/unittesting.md +++ b/docs/unittesting.md @@ -3,49 +3,46 @@ Unit Testing Click the ![double check](images/a-user-testing-icon.png) - icon to get to the "Solidity Unit Testing" plugin. If you don't see this icon, go to the plugin manager (by click the ![plug](images/a-plug.png) icon) and load up the unit testing plugin. + icon to get to the "Solidity Unit Testing" plugin. + + If you haven't used this plugin before and are not seeing `double check` icon, you have to activate it from Remix plugin manager. + +Go to the plugin manager (by click the ![plug](images/a-plug.png) icon) and load up the unit testing plugin. ![](images/a-unit-testing-from-pm.png) +Now `double check` icon will appear on the left side icon bar. Clicking on icon will load the unit testing module in the side panel. + ![](images/a-unit-testing-feature.png) -Generating Test File +Generate Test File ------------------ -Click the button "Generate test file" to create a new solidity file in the current folder. -This create a new solidity file suffixed with `_test`. -This file contains the minimum you need for running unit testing. +Click the button `Generate test file` to create a new solidity file in the current folder suffixed with `_test`. This file contains the minimum you need for running unit testing. + +Write Tests +----------- +Write tests to check the functionality of your contract. Remix injects a built-in `assert` library which can be used for testing. Visit the library documentation [here](./assert_library). + +Apart from this, Remix allows usage of some special functions to make testing more structural. They are: -Running Tests +* `beforeEach()` - Runs before each test +* `beforeAll()` - Runs before all tests +* `afterEach()` - Runs after each test +* `afterAll()` - Runs after all tests + +To get started, see [this](https://github.com/ethereum/remix/blob/master/remix-tests/tests/examples_4/SafeMath_test.sol) for sample implementation. + +Run Tests ------------------ Click the button "Run tests" to executes all tests whose box has been checked below (by default all). The execution is run in a separate environment and the result is displayed below. ![](images/a-unit-testing-run-result.png) -Here is a list of functions and their supported types that you can use to write your testcases: - -```eval_rst -+ -----------------------+--------------------------------------------------------+ -| 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` | -+------------------------+--------------------------------------------------------+ -``` - -Click [here](https://github.com/ethereum/remix/blob/master/remix-tests/tests/examples_4/SafeMath_test.sol) for a test file example - 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) +remix-tests is also a CLI, it can be used in a continuous integration environment which support node.js. +Please find more information in the [remix-tests repository](https://github.com/ethereum/remix/tree/master/remix-tests) See also: example [Su Squares contract](https://github.com/su-squares/ethereum-contract/tree/e542f37d4f8f6c7b07d90a6554424268384a4186) and [Travis build](https://travis-ci.org/su-squares/ethereum-contract/builds/446186067) that uses remix-tests for continuous integration testing.