Skip to content

Commit 7696273

Browse files
committed
Add Json test
1 parent a40b59c commit 7696273

File tree

1 file changed

+130
-80
lines changed

1 file changed

+130
-80
lines changed

crates/forge/tests/cli/cmd.rs

Lines changed: 130 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,136 @@ contract CounterTest is DSTest {
23902390
);
23912391
});
23922392

2393+
// <https://github.com/foundry-rs/foundry/issues/9115>
2394+
forgetest_init!(gas_report_with_fallback, |prj, cmd| {
2395+
prj.add_test(
2396+
"DelegateProxyTest.sol",
2397+
r#"
2398+
import {Test} from "forge-std/Test.sol";
2399+
2400+
contract ProxiedContract {
2401+
uint256 public amount;
2402+
2403+
function deposit(uint256 aba) external {
2404+
amount = amount * 2;
2405+
}
2406+
2407+
function deposit() external {
2408+
}
2409+
}
2410+
2411+
contract DelegateProxy {
2412+
address internal implementation;
2413+
2414+
constructor(address counter) {
2415+
implementation = counter;
2416+
}
2417+
2418+
function deposit() external {
2419+
}
2420+
2421+
fallback() external payable {
2422+
address addr = implementation;
2423+
2424+
assembly {
2425+
calldatacopy(0, 0, calldatasize())
2426+
let result := delegatecall(gas(), addr, 0, calldatasize(), 0, 0)
2427+
returndatacopy(0, 0, returndatasize())
2428+
switch result
2429+
case 0 { revert(0, returndatasize()) }
2430+
default { return(0, returndatasize()) }
2431+
}
2432+
}
2433+
}
2434+
2435+
contract GasReportFallbackTest is Test {
2436+
function test_fallback_gas_report() public {
2437+
ProxiedContract proxied = ProxiedContract(address(new DelegateProxy(address(new ProxiedContract()))));
2438+
proxied.deposit(100);
2439+
proxied.deposit();
2440+
}
2441+
}
2442+
"#,
2443+
)
2444+
.unwrap();
2445+
2446+
cmd.args(["test", "--mt", "test_fallback_gas_report", "--gas-report"])
2447+
.assert_success()
2448+
.stdout_eq(str![[r#"
2449+
...
2450+
Ran 1 test for test/DelegateProxyTest.sol:GasReportFallbackTest
2451+
[PASS] test_fallback_gas_report() ([GAS])
2452+
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
2453+
| test/DelegateProxyTest.sol:DelegateProxy contract | | | | | |
2454+
|---------------------------------------------------|-----------------|-------|--------|-------|---------|
2455+
| Deployment Cost | Deployment Size | | | | |
2456+
| 108698 | 315 | | | | |
2457+
| Function Name | min | avg | median | max | # calls |
2458+
| deposit | 21160 | 21160 | 21160 | 21160 | 1 |
2459+
| fallback | 29396 | 29396 | 29396 | 29396 | 1 |
2460+
2461+
2462+
| test/DelegateProxyTest.sol:ProxiedContract contract | | | | | |
2463+
|-----------------------------------------------------|-----------------|------|--------|------|---------|
2464+
| Deployment Cost | Deployment Size | | | | |
2465+
| 106511 | 276 | | | | |
2466+
| Function Name | min | avg | median | max | # calls |
2467+
| deposit | 3320 | 3320 | 3320 | 3320 | 1 |
2468+
...
2469+
2470+
"#]]);
2471+
2472+
cmd.forge_fuse()
2473+
.args(["test", "--mt", "test_fallback_gas_report", "--gas-report", "--json"])
2474+
.assert_success()
2475+
.stdout_eq(
2476+
str![[r#"
2477+
[
2478+
{
2479+
"contract": "test/DelegateProxyTest.sol:DelegateProxy",
2480+
"deployment": {
2481+
"gas": 108698,
2482+
"size": 315
2483+
},
2484+
"functions": {
2485+
"deposit()": {
2486+
"calls": 1,
2487+
"min": 21160,
2488+
"mean": 21160,
2489+
"median": 21160,
2490+
"max": 21160
2491+
},
2492+
"fallback()": {
2493+
"calls": 1,
2494+
"min": 29396,
2495+
"mean": 29396,
2496+
"median": 29396,
2497+
"max": 29396
2498+
}
2499+
}
2500+
},
2501+
{
2502+
"contract": "test/DelegateProxyTest.sol:ProxiedContract",
2503+
"deployment": {
2504+
"gas": 106511,
2505+
"size": 276
2506+
},
2507+
"functions": {
2508+
"deposit(uint256)": {
2509+
"calls": 1,
2510+
"min": 3320,
2511+
"mean": 3320,
2512+
"median": 3320,
2513+
"max": 3320
2514+
}
2515+
}
2516+
}
2517+
]
2518+
"#]]
2519+
.is_json(),
2520+
);
2521+
});
2522+
23932523
forgetest_init!(can_use_absolute_imports, |prj, cmd| {
23942524
let remapping = prj.paths().libraries[0].join("myDependency");
23952525
let config = Config {
@@ -2818,83 +2948,3 @@ forgetest_init!(gas_report_include_tests, |prj, cmd| {
28182948
.is_json(),
28192949
);
28202950
});
2821-
2822-
// <https://github.com/foundry-rs/foundry/issues/9115>
2823-
forgetest_init!(gas_report_with_fallback, |prj, cmd| {
2824-
prj.add_test(
2825-
"DelegateProxyTest.sol",
2826-
r#"
2827-
import {Test} from "forge-std/Test.sol";
2828-
2829-
contract ProxiedContract {
2830-
uint256 public amount;
2831-
2832-
function deposit(uint256 aba) external {
2833-
amount = amount * 2;
2834-
}
2835-
2836-
function deposit() external {
2837-
}
2838-
}
2839-
2840-
contract DelegateProxy {
2841-
address internal implementation;
2842-
2843-
constructor(address counter) {
2844-
implementation = counter;
2845-
}
2846-
2847-
function deposit() external {
2848-
}
2849-
2850-
fallback() external payable {
2851-
address addr = implementation;
2852-
2853-
assembly {
2854-
calldatacopy(0, 0, calldatasize())
2855-
let result := delegatecall(gas(), addr, 0, calldatasize(), 0, 0)
2856-
returndatacopy(0, 0, returndatasize())
2857-
switch result
2858-
case 0 { revert(0, returndatasize()) }
2859-
default { return(0, returndatasize()) }
2860-
}
2861-
}
2862-
}
2863-
2864-
contract GasReportFallbackTest is Test {
2865-
function test_fallback_gas_report() public {
2866-
ProxiedContract proxied = ProxiedContract(address(new DelegateProxy(address(new ProxiedContract()))));
2867-
proxied.deposit(100);
2868-
proxied.deposit();
2869-
}
2870-
}
2871-
"#,
2872-
)
2873-
.unwrap();
2874-
2875-
cmd.args(["test", "--mt", "test_fallback_gas_report", "--gas-report"])
2876-
.assert_success()
2877-
.stdout_eq(str![[r#"
2878-
...
2879-
Ran 1 test for test/DelegateProxyTest.sol:GasReportFallbackTest
2880-
[PASS] test_fallback_gas_report() ([GAS])
2881-
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
2882-
| test/DelegateProxyTest.sol:DelegateProxy contract | | | | | |
2883-
|---------------------------------------------------|-----------------|-------|--------|-------|---------|
2884-
| Deployment Cost | Deployment Size | | | | |
2885-
| 108698 | 315 | | | | |
2886-
| Function Name | min | avg | median | max | # calls |
2887-
| deposit | 21160 | 21160 | 21160 | 21160 | 1 |
2888-
| fallback | 29396 | 29396 | 29396 | 29396 | 1 |
2889-
2890-
2891-
| test/DelegateProxyTest.sol:ProxiedContract contract | | | | | |
2892-
|-----------------------------------------------------|-----------------|------|--------|------|---------|
2893-
| Deployment Cost | Deployment Size | | | | |
2894-
| 106511 | 276 | | | | |
2895-
| Function Name | min | avg | median | max | # calls |
2896-
| deposit | 3320 | 3320 | 3320 | 3320 | 1 |
2897-
...
2898-
2899-
"#]]);
2900-
});

0 commit comments

Comments
 (0)