Files
e899c10c3c Improved support for foundry tests (#1499)
* improved support for foundry tests

* Extend Foundry support testing (#1512)

* Fix lint failures

* ci: bump python version to 3.10, clone submodules

* some fixes when using foundry mode

* Update hevm to `542986dea3bec4f5898731ff48877a7a21811bf4`

* Fix bugs in Foundry test generation (#1516)

* Fix fallback function bug in Foundry test generation

* Fix null byte in argument bug in Foundry test generation

* Remove unnecessary config file

* Remove unused/redundant imports

* Disable Slither in Foundry test gen tests

* Conditionally run forge-std tests based on the solc version

* Reduce unnecessary test grouping and improve failed assertion messages

---------

Co-authored-by: Emilio López <[email protected]>

* Update hevm to `41e6d1304411749ea8c816d131991663b5dca67a`

* Fix linter and build errors (#1521)

* improve foundry stateful invariant testing

* fix

* Fix foundry test generation linter hints and tests (#1522)

* Foundry updates (#1525)

* Rename dapptest to foundry and support foundry assertX in assertion mode

* Rename basic foundry test contracts dir to `foundry-basic`

* Add testing modes to `README`

* fixes

* fixes

* Add support for Foundry assume (#1528)

* flake: sync hevm version with stack.yaml

* Clarify `optimization` test mode in `README` (#1529)

* IS_TEST fix

---------

Co-authored-by: Radu Bahmata <[email protected]>
Co-authored-by: Emilio López <[email protected]>
Co-authored-by: Emilio López <[email protected]>
2026-03-13 15:49:41 +01:00

100 lines
2.5 KiB
Solidity

// SPDX-License-Identifier: UNLICENSED
pragma abicoder v2;
pragma solidity >=0.7.5;
// Minimal Vm interface (this contract doesn't use forge-std).
interface Vm {
function assume(bool condition) external pure;
}
contract Greeter {
string public greeting;
function greet(string memory _greeting) public {
greeting = _greeting;
}
function gm() public {
greeting = "gm";
}
}
contract GreeterTestSetup {
bool public IS_TEST = true;
Greeter greeter;
function greeting() public view returns (string memory) {
return greeter.greeting();
}
function setUp() public {
greeter = new Greeter();
}
}
contract GreeterTest is GreeterTestSetup {
function greet(string memory _greeting) public {
greeter.greet(_greeting);
}
function testShrinking(uint256 x, uint256 y) public {
require(x * y <= 100, "product greater than 100");
}
function testStringFuzz(string memory myGreeting) public {
greeter.greet(myGreeting);
require(keccak256(abi.encodePacked(greeter.greeting())) == keccak256(abi.encodePacked(myGreeting)), "not equal");
}
function testFuzzFixedArray(uint256[2] memory x) public {
// filter out x[1] == 0 since it'll drain all our gas in the
// division by zero from all future tests
if (x[1] == 0) return;
require(x[1] / x[1] == 0);
}
function testFuzzVariableArray(uint256[] memory x) public {
require(x[0] == x[1]);
}
function testFuzzBytes1(bytes1 x) public {
require(x == 0);
}
function testFuzzBytes14(bytes14 x) public {
require(x == 0);
}
function testFuzzBytes32(bytes32 x) public {
require(x == 0);
}
function testFuzzI256(int256 x) public {
require(x >= 0);
}
struct Foo {
Bar bar;
}
struct Bar {
uint256 baz;
}
function testFuzzAbiCoderV2(Foo memory foo) public {
require(foo.bar.baz < 5);
}
function testGreeting(string memory message) public {
greeter.greet(message);
require(keccak256(abi.encodePacked(greeter.greeting())) == keccak256(abi.encodePacked(message)), "not equal");
}
function testFuzzAssume(uint256 x) public {
// keccak256("hevm cheat code") - standard hevm cheatcode address
// https://github.com/argotorg/hevm/blob/ed90053fa0ed69e658a75ab0ed64d467f5a5448d/src/EVM.hs#L1861
Vm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D).assume(x <= 100);
assert(x <= 100);
}
}