Skip to content

Commit

Permalink
fix(config): inline config intermingled (#7496)
Browse files Browse the repository at this point in the history
fix(config): inline config intermingled when multiple contracts in same file
  • Loading branch information
grandizzy authored Mar 26, 2024
1 parent 563e062 commit b0698bb
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions crates/config/src/inline/natspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ impl SolangParser {
}

let Ok((pt, comments)) = solang_parser::parse(src, 0) else { return };
let mut prev_end = 0;
for item in &pt.0 {
let pt::SourceUnitPart::ContractDefinition(c) = item else { continue };
let Some(id) = c.name.as_ref() else { continue };
if id.name != contract_name {
continue
};
let mut prev_end = c.loc.start();
for part in &c.parts {
let pt::ContractPart::FunctionDefinition(f) = part else { continue };
let start = f.loc.start();
Expand All @@ -215,7 +215,6 @@ impl SolangParser {
}
prev_end = f.loc.end();
}
prev_end = c.loc.end();
}
}
}
Expand Down Expand Up @@ -401,4 +400,52 @@ contract FuzzInlineConf is DSTest {
docs: conf.to_string(),
}
}

#[test]
fn parse_solang_multiple_contracts_from_same_file() {
let src = r#"
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
import "ds-test/test.sol";
contract FuzzInlineConf is DSTest {
/// forge-config: default.fuzz.runs = 1
function testInlineConfFuzz1() {}
}
contract FuzzInlineConf2 is DSTest {
/// forge-config: default.fuzz.runs = 2
function testInlineConfFuzz2() {}
}
"#;
let mut natspecs = vec![];
let solang = SolangParser::new();
let id = || "inline/FuzzInlineConf.t.sol:FuzzInlineConf".to_string();
let default_line = || "0:0:0".to_string();
solang.parse(&mut natspecs, src, &id(), "FuzzInlineConf");
assert_eq!(
natspecs,
[NatSpec {
contract: id(),
function: "testInlineConfFuzz1".to_string(),
line: default_line(),
docs: "forge-config: default.fuzz.runs = 1".to_string(),
},]
);

let mut natspecs = vec![];
let id = || "inline/FuzzInlineConf2.t.sol:FuzzInlineConf2".to_string();
solang.parse(&mut natspecs, src, &id(), "FuzzInlineConf2");
assert_eq!(
natspecs,
[NatSpec {
contract: id(),
function: "testInlineConfFuzz2".to_string(),
line: default_line(),
// should not get config from previous contract
docs: "forge-config: default.fuzz.runs = 2".to_string(),
},]
);
}
}

0 comments on commit b0698bb

Please sign in to comment.