Skip to content

Commit

Permalink
fix(cheatcodes): add repro test for once reported issues around empty…
Browse files Browse the repository at this point in the history
… JSON arrays (#7348)

* add repro case, fortunately issue has been resolved over time

* revert debug line
  • Loading branch information
zerosnacks authored Mar 9, 2024
1 parent 9ec42d6 commit 9f6bb3b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ test_repro!(3753);
// https://github.com/foundry-rs/foundry/issues/3792
test_repro!(3792);

// https://github.com/foundry-rs/foundry/issues/4402
test_repro!(4402);

// https://github.com/foundry-rs/foundry/issues/4586
test_repro!(4586);

Expand Down
4 changes: 4 additions & 0 deletions testdata/fixtures/Json/Issue4402.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tokens": ["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"],
"empty": []
}
2 changes: 2 additions & 0 deletions testdata/fixtures/Toml/Issue4402.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tokens = ["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"]
empty = []
64 changes: 64 additions & 0 deletions testdata/repros/Issue4402.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "../cheats/Vm.sol";

// https://github.com/foundry-rs/foundry/issues/4402
contract Issue4402Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function testReadNonEmptyArray() public {
string memory path = "fixtures/Json/Issue4402.json";
string memory json = vm.readFile(path);
address[] memory tokens = vm.parseJsonAddressArray(json, ".tokens");
assertEq(tokens.length, 1);

path = "fixtures/Toml/Issue4402.toml";
string memory toml = vm.readFile(path);
tokens = vm.parseTomlAddressArray(toml, ".tokens");
assertEq(tokens.length, 1);
}

function testReadEmptyArray() public {
string memory path = "fixtures/Json/Issue4402.json";
string memory json = vm.readFile(path);

// Every one of these used to causes panic
address[] memory emptyAddressArray = vm.parseJsonAddressArray(json, ".empty");
bool[] memory emptyBoolArray = vm.parseJsonBoolArray(json, ".empty");
bytes[] memory emptyBytesArray = vm.parseJsonBytesArray(json, ".empty");
bytes32[] memory emptyBytes32Array = vm.parseJsonBytes32Array(json, ".empty");
string[] memory emptyStringArray = vm.parseJsonStringArray(json, ".empty");
int256[] memory emptyIntArray = vm.parseJsonIntArray(json, ".empty");
uint256[] memory emptyUintArray = vm.parseJsonUintArray(json, ".empty");

assertEq(emptyAddressArray.length, 0);
assertEq(emptyBoolArray.length, 0);
assertEq(emptyBytesArray.length, 0);
assertEq(emptyBytes32Array.length, 0);
assertEq(emptyStringArray.length, 0);
assertEq(emptyIntArray.length, 0);
assertEq(emptyUintArray.length, 0);

path = "fixtures/Toml/Issue4402.toml";
string memory toml = vm.readFile(path);

// Every one of these used to causes panic
emptyAddressArray = vm.parseTomlAddressArray(toml, ".empty");
emptyBoolArray = vm.parseTomlBoolArray(toml, ".empty");
emptyBytesArray = vm.parseTomlBytesArray(toml, ".empty");
emptyBytes32Array = vm.parseTomlBytes32Array(toml, ".empty");
emptyStringArray = vm.parseTomlStringArray(toml, ".empty");
emptyIntArray = vm.parseTomlIntArray(toml, ".empty");
emptyUintArray = vm.parseTomlUintArray(toml, ".empty");

assertEq(emptyAddressArray.length, 0);
assertEq(emptyBoolArray.length, 0);
assertEq(emptyBytesArray.length, 0);
assertEq(emptyBytes32Array.length, 0);
assertEq(emptyStringArray.length, 0);
assertEq(emptyIntArray.length, 0);
assertEq(emptyUintArray.length, 0);
}
}

0 comments on commit 9f6bb3b

Please sign in to comment.