diff --git a/src/yul/parser/target/llvm/attributes.rs b/src/yul/parser/target/llvm/attributes.rs index 279fabc2..62119145 100644 --- a/src/yul/parser/target/llvm/attributes.rs +++ b/src/yul/parser/target/llvm/attributes.rs @@ -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 + ) + }); + assert_eq!( + format!("{:?}", result.into_iter().collect::>()), + 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 + ) + }); + assert_eq!( + format!("{:?}", result.into_iter().collect::>()), + 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) + } +}