Home

Awesome

<p align="center"> <img src="https://github.com/user-attachments/assets/036bad22-3b0d-4ea3-9338-faecd017a290" width="200"></a> <br> <a href="https://crates.io/crates/bulloak/"> <img src="https://img.shields.io/crates/v/bulloak?style=flat&labelColor=1C2C2E&color=C96329&logo=Rust&logoColor=white"> </a> <a href="https://codecov.io/gh/alexfertel/bulloak"> <img src="https://codecov.io/github/alexfertel/bulloak/coverage.svg?branch=main"> </a> </p>

bulloak

A Solidity test generator based on the Branching Tree Technique.

[!WARNING] Note that bulloak is still 0.*.*, so breaking changes may occur at any time. If you must depend on bulloak, we recommend pinning to a specific version, i.e., =0.y.z.

Installation

cargo install bulloak

VSCode

The following VSCode extensions are not essential but they are recommended for a better user experience:

Usage

bulloak implements two commands:

Scaffold Solidity Files

Say you have a foo.tree file with the following contents:

FooTest
└── When stuff is called // Comments are supported.
    └── When a condition is met
        └── It should revert.
            └── Because we shouldn't allow it.

You can use bulloak scaffold to generate a Solidity contract containing modifiers and tests that match the spec described in foo.tree. The following will be printed to stdout:

// $ bulloak scaffold foo.tree
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.0;

contract FooTest {
    modifier whenStuffIsCalled() {
        _;
    }

    function test_RevertWhen_AConditionIsMet() external whenStuffIsCalled {
        // It should revert.
        //     Because we shouldn't allow it.
    }
}

You can use the -w option to write the generated contracts to the file system. Say we have a bunch of .tree files in the current working directory. If we run the following:

$ bulloak scaffold -w ./**/*.tree

bulloak will create a .t.sol file per .tree file and write the generated contents to it.

If a .t.sol file's title matches a .tree in the same directory, then bulloak will skip writing to that file. However, you may override this behaviour with the -f flag. This will force bulloak to overwrite the contents of the file.

$ bulloak scaffold -wf ./**/*.tree

Note all tests are showing as passing when their body is empty. To prevent this, you can use the -S (or --vm-skip) option to add a vm.skip(true); at the beginning of each test function. This option will also add an import for forge-std's Test.sol and all test contracts will inherit from it.

You can skip emitting the modifiers by passing the -m (or --skip--modifiers) option. This way, the generated files will only include the test functions.

Check That Your Code And Spec Match

You can use bulloak check to make sure that your Solidity files match your spec. For example, any missing tests will be reported to you.

Say you have the following spec:

HashPairTest
├── It should never revert.
├── When first arg is smaller than second arg
│   └── It should match the result of `keccak256(abi.encodePacked(a,b))`.
└── When first arg is bigger than second arg
    └── It should match the result of `keccak256(abi.encodePacked(b,a))`.

And a matching Solidity file:

pragma solidity 0.8.0;

contract HashPairTest {
  function test_ShouldNeverRevert() external {
    // It should never revert.
  }

  function test_WhenFirstArgIsSmallerThanSecondArg() external {
    // It should match the result of `keccak256(abi.encodePacked(a,b))`.
  }
}

This Solidity file is missing the tests for the branch When first arg is bigger than second arg, which would be reported after running bulloak check tests/scaffold/basic.tree, like so:

warn: function "test_WhenFirstArgIsBiggerThanSecondArg" is missing in .sol
     + fix: run `bulloak check --fix tests/scaffold/basic.tree`
   --> tests/scaffold/basic.tree:5

warn: 1 check failed (run `bulloak check --fix <.tree files>` to apply 1 fix)

As you can see in the above message, bulloak can fix the issue automatically. If we run the command with the --stdout flag, the output is:

--> tests/scaffold/basic.t.sol
pragma solidity 0.8.0;

contract HashPairTest {
    function test_ShouldNeverRevert() external {
        // It should never revert.
    }

    function test_WhenFirstArgIsSmallerThanSecondArg() external {
        // It should match the result of `keccak256(abi.encodePacked(a,b))`.
    }

    function test_WhenFirstArgIsBiggerThanSecondArg() external {
        // It should match the result of `keccak256(abi.encodePacked(b,a))`.
    }
}
<--

success: 1 issue fixed.

Running the command without the --stdout flag will overwrite the contents of the solidity file with the fixes applied. Note that not all issues can be automatically fixed, and bulloak's output will reflect that.

warn: 13 checks failed (run `bulloak check --fix <.tree files>` to apply 11 fixes)

You can skip checking that the modifiers are present by passing the -m (or --skip--modifiers) option. This way, bulloak will not warn when a modifier is missing from the generated file.

Rules

The following rules are currently implemented:

Compiler Errors

Another feature of bulloak is reporting errors in your input trees.

For example, say you have a buggy foo.tree file, which is missing a character. Running bulloak scaffold foo.tree would report the error like this:

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
bulloak error: unexpected `when` keyword

── when the id references a null stream
   ^^^^

--- (line 2, column 4) ---
file: foo.tree

Trees

bulloak scaffold scaffolds Solidity test files based on .tree specifications that follow the Branching Tree Technique.

Currently, there is on-going discussion on how to handle different edge-cases to better empower the Solidity community. This section is a description of the current implementation of the compiler.

Terminology

Spec

Each tree file should describe at least one function under test. Trees follow these rules:

Take the following Solidity function:

function hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
    return a < b ? hash(a, b) : hash(b, a);
}

A reasonable spec for the above function would be:

HashPairTest
├── It should never revert.
├── When first arg is smaller than second arg
│   └── It should match the result of `keccak256(abi.encodePacked(a,b))`.
└── When first arg is bigger than second arg
    └── It should match the result of `keccak256(abi.encodePacked(b,a))`.

There is a top-level action that will generate a test to check the function invariant that it should never revert.

Then, we have the two possible preconditions: a < b and a >= b. Both branches end in an action that will make bulloak scaffold generate the respective test.

Note the following things:

Suppose you have additional Solidity functions that you want to test in the same test contract, say Utils within utils.t.sol:

function min(uint256 a, uint256 b) private pure returns (uint256) {
    return a < b ? a : b;
}

function max(uint256 a, uint256 b) private pure returns (uint256) {
    return a > b ? a : b;
}

The full spec for all the above functions would be:

Utils::hashPair
├── It should never revert.
├── When first arg is smaller than second arg
│   └── It should match the result of `keccak256(abi.encodePacked(a,b))`.
└── When first arg is bigger than second arg
    └── It should match the result of `keccak256(abi.encodePacked(b,a))`.


Utils::min
├── It should never revert.
├── When first arg is smaller than second arg
│   └── It should match the value of `a`.
└── When first arg is bigger than second arg
    └── It should match the value of `b`.


Utils::max
├── It should never revert.
├── When first arg is smaller than second arg
│   └── It should match the value of `b`.
└── When first arg is bigger than second arg
    └── It should match the value of `a`.

Note the following things:

Output

There are a few things to keep in mind about the scaffolded Solidity test:

Examples

You can find practical examples of using BTT here:

Contributing

Please refer to CONTRIBUTING.md.

Publishing

These are the current steps taken to publish bulloak:

Supported By

This project has been possible thanks to the support of:

License

This project is licensed under either of: