State Inheritance Testing

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "ds-test/test.sol";
import 'src/SimpleNameRegister.sol';

interface CheatCodes {
    function prank(address) external;
    function expectRevert(bytes calldata) external;
    function startPrank(address) external;
    function stopPrank() external;
}

contract SimpleNameRegisterTest is DSTest {
    
    // declare state var.
    SimpleNameRegister simpleNameRegister;
    CheatCodes cheats;
    address adversary;

    function setUp() public {
        simpleNameRegister = new SimpleNameRegister();
        cheats = CheatCodes(HEVM_ADDRESS);
        adversary = 0xE6A2e85916802210147e366D4431f5ca4dD51a78;
    }

    // user can register an available name
    function testRegisterName(string memory _testString) public {
        simpleNameRegister.registerName(_testString);
        bool success = (address(this) == simpleNameRegister.nameOwner(_testString));
        assertTrue(success);
    }
    
    // user can register an available name and relinquish it
    function testRelinquishName(string memory _testString) public {
        simpleNameRegister.registerName(_testString);   
        simpleNameRegister.relinquishName(_testString);
        bool success = (simpleNameRegister.nameOwner(_testString) == address(0));
        assertTrue(success);
    }

    // user cannot relinquish a name that does not belong to them
    function testRelinquishAsNotOwner(string memory _testString) public {
        simpleNameRegister.registerName(_testString);   
        cheats.startPrank(adversary);
        cheats.expectRevert(bytes("The provided name does not belong to you!"));
        simpleNameRegister.relinquishName(_testString);        
        cheats.stopPrank();
    }
    
    // user cannot register a name that already has an owner
    function testRegisterUnavailableName(string memory _testString) public {
        simpleNameRegister.registerName(_testString);   

        cheats.startPrank(adversary);
        cheats.expectRevert(bytes("The provided name has already been registered!"));
        simpleNameRegister.registerName(_testString);   
        cheats.stopPrank();
    }
}

State setups and transitions will be realized by abstract contracts. Here we have two states.

  1. StateZero -> Inception, nothing has been done.

  2. StateRegistered -> User has registered a name.

In StateZero, we will have to instantiate SimpleNameRegister, to interact with the contract for testing. We opt to instantiate cheats here as well.

abstract contract StateZero is DSTest{}

In StateRegistered, the user will register a name - which we observe in its setup function. Additionally, we instantiate an adversary address necessary for testing in this state.

State Zero: Inception

Create the state (abstract contract):

  • will contain the setUp() for State Zero

  • instantiate SimpleNameRegister

  • instantiate cheats (for use later in StateRegistered)

Create the test contract

  • To execute the required tests in StateZero, create a contract StateZeroTest.

  • This will contain all test functions pertaining to said state.

  • The state will be realized by inheritance (StateZeroTest is StateZero).

By the way of inheritance, the setUp function and state variables contained within StateZero will be executed setting up the environment for test functions belonging to StateZeroTest.

State Transition

State transition occurs by inheritance between the abstract contracts.

A movement from state(0) -> state(1) is reflected as StateRegistered is StateZero.

The initial state is inherited and executed (super.setUp), building upon it, are the necessary actions to evolve into StateRegistered.

In this this case, those actions would be a user registering a name:

  • name = 'whale';

  • simpleNameRegister.register(name);

Now that StateRegistered has been realized, similar to before, we will create a separate contract composing of the test functions pertaining to this state.

Create the test contract for StateRegistered

This approach prizes modularity which could prove useful in complicated testing situations.

Additionally, tests from an earlier state will not be repeated in a subsequent state as test contracts inherit state, not test functions.

Last updated