Skip to content

Commit c60b8e3

Browse files
lutterclaude
andcommitted
gnd: Add tests for array types in ABI codegen
Add additional tests to cover array-related edge cases in ABI code generation: - test_array_types_in_events: Verifies array parameters in events (address[] and uint256[]) generate correct Array<Address> and Array<BigInt> return types - test_matrix_types_in_functions: Verifies 2D array types (uint256[][]) generate correct Array<Array<BigInt>> return types These tests complement the existing tuple array tests and ensure comprehensive coverage of array-based ABI patterns. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent fd75cb6 commit c60b8e3

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

gnd/src/codegen/abi.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,4 +1782,125 @@ mod tests {
17821782
return_type_str
17831783
);
17841784
}
1785+
1786+
/// Test that array types in events are handled correctly.
1787+
#[test]
1788+
fn test_array_types_in_events() {
1789+
let abi_json = r#"[
1790+
{
1791+
"type": "event",
1792+
"name": "Airdropped",
1793+
"inputs": [
1794+
{"name": "sender", "type": "address", "indexed": true},
1795+
{"name": "recipients", "type": "address[]", "indexed": false},
1796+
{"name": "amounts", "type": "uint256[]", "indexed": false}
1797+
],
1798+
"anonymous": false
1799+
}
1800+
]"#;
1801+
1802+
let contract = parse_abi(abi_json);
1803+
let gen = AbiCodeGenerator::new(contract, "Token");
1804+
let types = gen.generate_types();
1805+
1806+
// Find the params class
1807+
let params_class = types
1808+
.iter()
1809+
.find(|c| c.name == "Airdropped__Params")
1810+
.expect("Should have Airdropped__Params class");
1811+
1812+
// Check that the array getters exist and have correct return types
1813+
let method_names: Vec<&str> = params_class
1814+
.methods
1815+
.iter()
1816+
.map(|m| m.name.as_str())
1817+
.collect();
1818+
1819+
assert!(
1820+
method_names.iter().any(|n| n.contains("recipients")),
1821+
"Should have recipients getter, found: {:?}",
1822+
method_names
1823+
);
1824+
assert!(
1825+
method_names.iter().any(|n| n.contains("amounts")),
1826+
"Should have amounts getter, found: {:?}",
1827+
method_names
1828+
);
1829+
1830+
// Verify the recipients getter returns Array<Address>
1831+
let recipients_getter = params_class
1832+
.methods
1833+
.iter()
1834+
.find(|m| m.name.contains("recipients"))
1835+
.expect("Should have recipients getter");
1836+
let return_type_str = recipients_getter
1837+
.return_type
1838+
.as_ref()
1839+
.expect("Should have return type")
1840+
.to_string();
1841+
assert!(
1842+
return_type_str.contains("Array<Address>"),
1843+
"recipients should return Array<Address>, got: {}",
1844+
return_type_str
1845+
);
1846+
1847+
// Verify the amounts getter returns Array<BigInt>
1848+
let amounts_getter = params_class
1849+
.methods
1850+
.iter()
1851+
.find(|m| m.name.contains("amounts"))
1852+
.expect("Should have amounts getter");
1853+
let amounts_type_str = amounts_getter
1854+
.return_type
1855+
.as_ref()
1856+
.expect("Should have return type")
1857+
.to_string();
1858+
assert!(
1859+
amounts_type_str.contains("Array<BigInt>"),
1860+
"amounts should return Array<BigInt>, got: {}",
1861+
amounts_type_str
1862+
);
1863+
}
1864+
1865+
/// Test that 2D array types (matrices) are handled correctly in functions.
1866+
#[test]
1867+
fn test_matrix_types_in_functions() {
1868+
let abi_json = r#"[
1869+
{
1870+
"type": "function",
1871+
"name": "getMatrix",
1872+
"inputs": [],
1873+
"outputs": [
1874+
{"name": "data", "type": "uint256[][]"}
1875+
],
1876+
"stateMutability": "view"
1877+
}
1878+
]"#;
1879+
1880+
let contract = parse_abi(abi_json);
1881+
let gen = AbiCodeGenerator::new(contract, "TestContract");
1882+
let types = gen.generate_types();
1883+
1884+
// Find the contract class
1885+
let contract_class = types.iter().find(|c| c.name == "TestContract").unwrap();
1886+
1887+
// Find the getMatrix method
1888+
let method = contract_class
1889+
.methods
1890+
.iter()
1891+
.find(|m| m.name == "getMatrix")
1892+
.expect("Should have getMatrix method");
1893+
1894+
// The return type should be Array<Array<BigInt>>
1895+
let return_type = method
1896+
.return_type
1897+
.as_ref()
1898+
.expect("Should have return type");
1899+
let return_type_str = return_type.to_string();
1900+
assert!(
1901+
return_type_str.contains("Array<Array<BigInt>>"),
1902+
"Return type should be Array<Array<BigInt>>, got: {}",
1903+
return_type_str
1904+
);
1905+
}
17851906
}

0 commit comments

Comments
 (0)