Skip to content

Commit

Permalink
increase coverage for the LLVM attribute parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sayon committed Aug 10, 2024
1 parent 838841e commit 122660a
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/yul/parser/target/llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,76 @@ pub(crate) fn get_llvm_attributes(

Ok(valid_attributes)
}

#[cfg(test)]
mod tests {
use crate::yul::lexer::token::location::Location;
use crate::yul::parser::identifier::Identifier;

use super::get_llvm_attributes;

fn identifier_of(name: &str) -> Identifier {
Identifier {
location: Location { line: 0, column: 0 },
inner: name.to_string(),
r#type: None,
}
}

#[test]
fn parse_single_attribute() {
let input = r#"
$llvm_Hot_llvm$
"#;
let expected = "[Hot]";
let result = get_llvm_attributes(&identifier_of(input)).unwrap_or_else(|_| {
panic!(
"LLVM attribute parser should be able to parse a valid input: \"{}\"",
input
)

Check warning on line 77 in src/yul/parser/target/llvm/attributes.rs

View check run for this annotation

Codecov / codecov/patch

src/yul/parser/target/llvm/attributes.rs#L74-L77

Added lines #L74 - L77 were not covered by tests
});
assert_eq!(
format!("{:?}", result.into_iter().collect::<Vec<_>>()),
expected
)
}

#[test]
fn parse_multiple_attributes() {
let input = r#"
$llvm_Hot_Cold_MinSize_llvm$
"#;
let expected = "[Cold, Hot, MinSize]";
let result = get_llvm_attributes(&identifier_of(input)).unwrap_or_else(|_| {
panic!(
"LLVM attribute parser should be able to parse a valid input: \"{}\"",
input
)

Check warning on line 95 in src/yul/parser/target/llvm/attributes.rs

View check run for this annotation

Codecov / codecov/patch

src/yul/parser/target/llvm/attributes.rs#L92-L95

Added lines #L92 - L95 were not covered by tests
});
assert_eq!(
format!("{:?}", result.into_iter().collect::<Vec<_>>()),
expected
)
}
#[test]
fn parse_malformed_attributes() {
let input = r#"
$llvm____*&@_llvm$
"#;
get_llvm_attributes(&identifier_of(input)).expect_err(&format!(
"LLVM attributes parser should not parse attributes from the malformed input \"{}\"",
input
));
}

#[test]
fn parse_invalid_attributes() {
let input = r#"
$llvm_Hot_Cold_MinSize_BogusAttr_llvm$
"#;
let expected = "Parser(InvalidAttributes { location: Location { line: 0, column: 0 }, values: {\"BogusAttr\"} })";
let result = get_llvm_attributes(&identifier_of(input))
.expect_err("LLVM attributes parser should not mask unknown attributes");
assert_eq!(format!("{:?}", result), expected)
}
}

0 comments on commit 122660a

Please sign in to comment.