pragma solidity >=0.4.22 <0.7.0; import "remix_tests.sol"; // this import is automatically injected by Remix. import "./AttendanceRegister.sol"; contract AttendanceRegisterTest { AttendanceRegister ar; /// 'beforeAll' runs before all other tests function beforeAll () public { // Create an instance of contract to be tested ar = new AttendanceRegister(); } /// For solidity version greater or equal to 0.6.0, /// See: https://solidity.readthedocs.io/en/v0.6.0/control-structures.html#try-catch /// Test 'add' using try-catch function testAddSuccessUsingTryCatch() public { // This will pass try ar.add(101, 'secondStudent', 11) returns (uint256 r) { Assert.equal(r, 101, 'wrong rollNumber'); } catch Error(string memory /*reason*/) { // This is executed in case // revert was called inside getData // and a reason string was provided. Assert.ok(false, 'failed with reason'); } catch (bytes memory /*lowLevelData*/) { // This is executed in case revert() was used // or there was a failing assertion, division // by zero, etc. inside getData. Assert.ok(false, 'failed unexpected'); } } /// Test failure case of 'add' using try-catch function testAddFailureUsingTryCatch1() public { // This will revert on 'require(class > 0 && class <= 12, "Invalid class");' for class '13' try ar.add(101, 'secondStudent', 13) returns (uint256 r) { Assert.ok(false, 'method execution should fail'); } catch Error(string memory reason) { // Compare failure reason, check if it is as expected Assert.equal(reason, 'Invalid class', 'failed with unexpected reason'); } catch (bytes memory /*lowLevelData*/) { Assert.ok(false, 'failed unexpected'); } } /// Test another failure case of 'add' using try-catch function testAddFailureUsingTryCatch2() public { // This will revert on 'require(register[rollNumber].class == 0, "Roll number not available");' for rollNumber '101' try ar.add(101, 'secondStudent', 11) returns (uint256 r) { Assert.ok(false, 'method execution should fail'); } catch Error(string memory reason) { // Compare failure reason, check if it is as expected Assert.equal(reason, 'Roll number not available', 'failed with unexpected reason'); } catch (bytes memory /*lowLevelData*/) { Assert.ok(false, 'failed unexpected'); } } /// For solidity version less than 0.6.0, low level call can be used /// See: https://solidity.readthedocs.io/en/v0.6.0/units-and-global-variables.html#members-of-address-types /// Test success case of 'add' using low level call function testAddSuccessUsingCall() public { bytes memory methodSign = abi.encodeWithSignature('add(uint256,string,uint256)', 102, 'firstStudent', 10); (bool success, bytes memory data) = address(ar).call(methodSign); // 'success' stores the result in bool, this can be used to check whether method call was successful Assert.equal(success, true, 'execution should be successful'); // 'data' stores the returned data which can be decoded to get the actual result uint rollNumber = abi.decode(data, (uint256)); // check if result is as expected Assert.equal(rollNumber, 102, 'wrong rollNumber'); } /// Test failure case of 'add' using low level call function testAddFailureUsingCall() public { bytes memory methodSign = abi.encodeWithSignature('add(uint256,string,uint256)', 102, 'duplicate', 10); (bool success, bytes memory data) = address(ar).call(methodSign); // 'success' will be false if method execution is not successful Assert.equal(success, false, 'execution should be successful'); } }
0.6.1