Skip to content

Commit

Permalink
Add Json test
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Nov 8, 2024
1 parent a40b59c commit 7696273
Showing 1 changed file with 130 additions and 80 deletions.
210 changes: 130 additions & 80 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,136 @@ contract CounterTest is DSTest {
);
});

// <https://github.com/foundry-rs/foundry/issues/9115>
forgetest_init!(gas_report_with_fallback, |prj, cmd| {
prj.add_test(
"DelegateProxyTest.sol",
r#"
import {Test} from "forge-std/Test.sol";
contract ProxiedContract {
uint256 public amount;
function deposit(uint256 aba) external {
amount = amount * 2;
}
function deposit() external {
}
}
contract DelegateProxy {
address internal implementation;
constructor(address counter) {
implementation = counter;
}
function deposit() external {
}
fallback() external payable {
address addr = implementation;
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), addr, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
}
contract GasReportFallbackTest is Test {
function test_fallback_gas_report() public {
ProxiedContract proxied = ProxiedContract(address(new DelegateProxy(address(new ProxiedContract()))));
proxied.deposit(100);
proxied.deposit();
}
}
"#,
)
.unwrap();

cmd.args(["test", "--mt", "test_fallback_gas_report", "--gas-report"])
.assert_success()
.stdout_eq(str![[r#"
...
Ran 1 test for test/DelegateProxyTest.sol:GasReportFallbackTest
[PASS] test_fallback_gas_report() ([GAS])
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
| test/DelegateProxyTest.sol:DelegateProxy contract | | | | | |
|---------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 108698 | 315 | | | | |
| Function Name | min | avg | median | max | # calls |
| deposit | 21160 | 21160 | 21160 | 21160 | 1 |
| fallback | 29396 | 29396 | 29396 | 29396 | 1 |
| test/DelegateProxyTest.sol:ProxiedContract contract | | | | | |
|-----------------------------------------------------|-----------------|------|--------|------|---------|
| Deployment Cost | Deployment Size | | | | |
| 106511 | 276 | | | | |
| Function Name | min | avg | median | max | # calls |
| deposit | 3320 | 3320 | 3320 | 3320 | 1 |
...
"#]]);

cmd.forge_fuse()
.args(["test", "--mt", "test_fallback_gas_report", "--gas-report", "--json"])
.assert_success()
.stdout_eq(
str![[r#"
[
{
"contract": "test/DelegateProxyTest.sol:DelegateProxy",
"deployment": {
"gas": 108698,
"size": 315
},
"functions": {
"deposit()": {
"calls": 1,
"min": 21160,
"mean": 21160,
"median": 21160,
"max": 21160
},
"fallback()": {
"calls": 1,
"min": 29396,
"mean": 29396,
"median": 29396,
"max": 29396
}
}
},
{
"contract": "test/DelegateProxyTest.sol:ProxiedContract",
"deployment": {
"gas": 106511,
"size": 276
},
"functions": {
"deposit(uint256)": {
"calls": 1,
"min": 3320,
"mean": 3320,
"median": 3320,
"max": 3320
}
}
}
]
"#]]
.is_json(),
);
});

forgetest_init!(can_use_absolute_imports, |prj, cmd| {
let remapping = prj.paths().libraries[0].join("myDependency");
let config = Config {
Expand Down Expand Up @@ -2818,83 +2948,3 @@ forgetest_init!(gas_report_include_tests, |prj, cmd| {
.is_json(),
);
});

// <https://github.com/foundry-rs/foundry/issues/9115>
forgetest_init!(gas_report_with_fallback, |prj, cmd| {
prj.add_test(
"DelegateProxyTest.sol",
r#"
import {Test} from "forge-std/Test.sol";
contract ProxiedContract {
uint256 public amount;
function deposit(uint256 aba) external {
amount = amount * 2;
}
function deposit() external {
}
}
contract DelegateProxy {
address internal implementation;
constructor(address counter) {
implementation = counter;
}
function deposit() external {
}
fallback() external payable {
address addr = implementation;
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), addr, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}
}
contract GasReportFallbackTest is Test {
function test_fallback_gas_report() public {
ProxiedContract proxied = ProxiedContract(address(new DelegateProxy(address(new ProxiedContract()))));
proxied.deposit(100);
proxied.deposit();
}
}
"#,
)
.unwrap();

cmd.args(["test", "--mt", "test_fallback_gas_report", "--gas-report"])
.assert_success()
.stdout_eq(str![[r#"
...
Ran 1 test for test/DelegateProxyTest.sol:GasReportFallbackTest
[PASS] test_fallback_gas_report() ([GAS])
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
| test/DelegateProxyTest.sol:DelegateProxy contract | | | | | |
|---------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 108698 | 315 | | | | |
| Function Name | min | avg | median | max | # calls |
| deposit | 21160 | 21160 | 21160 | 21160 | 1 |
| fallback | 29396 | 29396 | 29396 | 29396 | 1 |
| test/DelegateProxyTest.sol:ProxiedContract contract | | | | | |
|-----------------------------------------------------|-----------------|------|--------|------|---------|
| Deployment Cost | Deployment Size | | | | |
| 106511 | 276 | | | | |
| Function Name | min | avg | median | max | # calls |
| deposit | 3320 | 3320 | 3320 | 3320 | 1 |
...
"#]]);
});

0 comments on commit 7696273

Please sign in to comment.