Skip to content

Commit

Permalink
Disallow comments without assertions in binding assertions input files
Browse files Browse the repository at this point in the history
Also, count the number of skipped assertions (due to not fulfilling the version
requirements)
  • Loading branch information
ggiraldez committed Aug 16, 2024
1 parent 2177bf2 commit 635facd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub enum AssertionError {
#[error("Duplicate assertion definition {0}")]
DuplicateDefinition(String),

#[error("No assertions found in line {0}")]
NoAssertionsFound(usize),

#[error("Failed {failed} of {total} bindings assertions:\n{errors:#?}")]
FailedAssertions {
failed: usize,
Expand Down Expand Up @@ -122,7 +125,7 @@ impl<'a> fmt::Display for DisplayCursor<'a> {
}

/// Collects bindings assertions in comments in the parsed source code
/// accessible through the given cursor. The definitionns take the following form:
/// accessible through the given cursor. The definitions take the following form:
///
/// uint x;
/// // ^def:1
Expand All @@ -149,12 +152,16 @@ impl<'a> fmt::Display for DisplayCursor<'a> {
/// // ^ref:2
/// //<ref:1
///
/// All single line comments should contain an assertion. It is considered an
/// error if they don't.
///
pub fn collect_assertions_into<'a>(
assertions: &mut Assertions<'a>,
mut cursor: Cursor,
file: &'a str,
version: &Version,
) -> Result<(), AssertionError> {
) -> Result<usize, AssertionError> {
let mut skipped = 0;
loop {
if cursor
.node()
Expand All @@ -167,7 +174,13 @@ pub fn collect_assertions_into<'a>(
Some(Assertion::Reference(assertion)) => {
assertions.insert_reference_assertion(assertion);
}
None => (),
Some(Assertion::Skipped) => {
skipped += 1;
}
None => {
let line = cursor.text_offset().line + 1;
return Err(AssertionError::NoAssertionsFound(line));
}
}
}

Expand All @@ -176,13 +189,14 @@ pub fn collect_assertions_into<'a>(
}
}

Ok(())
Ok(skipped)
}

#[derive(Clone, Debug, PartialEq)]
enum Assertion<'a> {
Definition(DefinitionAssertion<'a>),
Reference(ReferenceAssertion<'a>),
Skipped,
}

static ASSERTION_REGEX: Lazy<Regex> = Lazy::new(|| {
Expand Down Expand Up @@ -253,7 +267,7 @@ fn find_assertion_in_comment<'a>(
// Assertion target may not be parseable with the current version
if let Some(version_req) = version_req {
if !version_req.matches(version) {
return Ok(None);
return Ok(Some(Assertion::Skipped));
}
}
Err(AssertionError::InvalidAssertion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn check_assertions_with_version(version: &Version, contents: &str) -> Result<()
let mut bindings =
bindings::create_with_resolver(version.clone(), Arc::new(TestsPathResolver {}));
let mut assertions = Assertions::new();
let mut skipped = 0;

let parts = split_multi_file(contents);

Expand All @@ -50,7 +51,7 @@ fn check_assertions_with_version(version: &Version, contents: &str) -> Result<()
}

bindings.add_file(file_path, parse_output.create_tree_cursor());
collect_assertions_into(
skipped += collect_assertions_into(
&mut assertions,
parse_output.create_tree_cursor(),
file_path,
Expand All @@ -63,7 +64,7 @@ fn check_assertions_with_version(version: &Version, contents: &str) -> Result<()
match result {
Ok(count) => {
assert!(count > 0, "No assertions found with version {version}");
println!("Version {version}, {count} assertions OK");
println!("Version {version}, {count} assertions OK, {skipped} skipped");
}
Err(err) => {
panic!("Failed bindings assertions in version {version}:\n{err}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ contract Second {
// ^ref:5
}
function get_first_choice() public returns (First.Choice) {
// Cannot access a member function through the contract type
return First.get_choice();
// ^ref:1
// ^ref:!
// ^ref:! -- cannot access a member function through the contract type
}
function other_choice() public returns (First.Choice) {
// Cannot access a state variable in another contract
return First.choice;
// ^ref:1
// ^ref:!
// ^ref:! -- cannot access a state variable in another contract
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Before 0.5.0 Solidity hoists all variable definitions
contract Foo {
function bar() returns (int x) {
// ^def:1
Expand Down

0 comments on commit 635facd

Please sign in to comment.