#![cfg(test)] use indoc::indoc; use melody_compiler::compiler; #[test] fn quantifier_test() { let output = compiler(indoc! { r#" 5 of "A"; "# }); assert_eq!(output.unwrap(), "A{5}"); } #[test] fn capture_test() { let output = compiler(indoc! { r#" capture { 5 of "A"; 0 to 9; } "# }); assert_eq!(output.unwrap(), "(A{5}[0-9])"); } #[test] fn named_capture_test() { let output = compiler(indoc! { r#" capture name { 5 of "A"; 0 to 9; } "#, }); assert_eq!(output.unwrap(), "(?A{5}[0-9])"); } #[test] fn number_quantifier_range_test() { let output = compiler(indoc! { r#" 1 to 5 of "A"; "#, }); assert_eq!(output.unwrap(), "A{1,5}"); } #[test] fn uppercase_range_test() { let output = compiler(indoc! { r#" A to Z; 7 of A to Z; "#, }); assert_eq!(output.unwrap(), "[A-Z][A-Z]{7}"); } #[test] fn lowercase_range_test() { let output = compiler(indoc! { r#" a to z; 8 of a to z; "#, }); assert_eq!(output.unwrap(), "[a-z][a-z]{8}"); } #[test] fn open_range_expression_test() { let output = compiler(indoc! { r#" over 4 of "a"; "#, }); assert_eq!(output.unwrap(), "a{5,}"); } #[test] fn start_end_test() { let output = compiler(indoc! { r#" ; "a"; ; "#, }); assert_eq!(output.unwrap(), "^a$"); } #[test] fn symbol_test() { let output = compiler(indoc! { r#" ; ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; not ; ; "#, }); assert_eq!( output.unwrap(), r"^.\s\S\n[^\n]\t[^\t]\r[^\r]\f[^\f]\0[^\0]\d\D\w\W\v[^\v][a-zA-Z][^a-zA-Z][a-zA-Z0-9][^a-zA-Z0-9] [^ ]\b\B[\b][^\b]$" ); } #[test] fn symbol_unicode_category_test() { let output = compiler(indoc! { r#" ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 5 of ; "#, }); assert_eq!( output.unwrap(), r"\p{L&}\p{Pe}\p{Pc}\p{Cc}\p{Sc}\p{Pd}\p{Nd}\p{Me}\p{Pf}\p{Cf}\p{Pi}\p{Nl}\p{L}\p{Zl}\p{Ll}\p{M}\p{Sm}\p{Lm}\p{Sk}\p{Mn}\p{N}\p{Ps}\p{Lo}\p{No}\p{Po}\p{So}\p{C}\p{Zp}\p{Co}\p{P}\p{Z}\p{Zs}\p{Mc}\p{Cs}\p{S}\p{Lt}\p{Cn}\p{Lu}\p{L&}{5}" ); } #[test] fn symbol_unicode_category_negative_test() { let output = compiler(indoc! { r#" not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; not ; 5 of not ; "#, }); assert_eq!( output.unwrap(), r"\P{L&}\P{Pe}\P{Pc}\P{Cc}\P{Sc}\P{Pd}\P{Nd}\P{Me}\P{Pf}\P{Cf}\P{Pi}\P{Nl}\P{L}\P{Zl}\P{Ll}\P{M}\P{Sm}\P{Lm}\P{Sk}\P{Mn}\P{N}\P{Ps}\P{Lo}\P{No}\P{Po}\P{So}\P{C}\P{Zp}\P{Co}\P{P}\P{Z}\P{Zs}\P{Mc}\P{Cs}\P{S}\P{Lt}\P{Cn}\P{Lu}\P{L&}{5}" ); } #[test] fn match_test() { let output = compiler(indoc! { r#" 3 of match { 5 of "A"; 0 to 9; } "#, }); assert_eq!(output.unwrap(), "(?:A{5}[0-9]){3}"); } #[test] fn comment_test() { let output = compiler(indoc! { r#" /* a single digit in the range of 0 to 5 */ // other comment 0 to 5; match { "x"; } "#, }); assert_eq!(output.unwrap(), "[0-5](?:x)"); } #[test] fn char_test() { let output = compiler(indoc! { r#" 3 of ; "#, }); assert_eq!(output.unwrap(), ".{3}"); } #[test] fn negative_range_test() { let output = compiler(indoc! { r#" not 3 to 5; not a to z; "#, }); assert_eq!(output.unwrap(), "[^3-5][^a-z]"); } #[test] fn some_test() { let single_output = compiler(indoc! { r#" some of ; "#, }); assert_eq!(single_output.unwrap(), ".+"); let multiple_output = compiler(indoc! { r#" some of "ABC"; "#, }); assert_eq!(multiple_output.unwrap(), "(?:ABC)+"); } #[test] fn option_test() { let single_output = compiler(indoc! { r#" option of ; "#, }); assert_eq!(single_output.unwrap(), ".?"); let multiple_output = compiler(indoc! { r#" option of "ABC"; "#, }); assert_eq!(multiple_output.unwrap(), "(?:ABC)?"); } #[test] fn either_test() { let output = compiler(indoc! { r#" either { "first"; "second"; a to z; } either { "first"; "second"; } "#, }); assert_eq!(output.unwrap(), "(?:first|second|[a-z])(?:first|second)"); } #[test] fn any_test() { let single_output = compiler(indoc! { r#" any of ; "#, }); assert_eq!(single_output.unwrap(), ".*"); let multiple_output = compiler(indoc! { r#" any of "ABC"; "#, }); assert_eq!(multiple_output.unwrap(), "(?:ABC)*"); } #[test] fn directly_quantifiable() { let single_output = compiler(indoc! { r#" 5 of ; "#, }); assert_eq!(single_output.unwrap(), r"\w{5}"); } #[test] fn raw_test() { let output = compiler(indoc! { r#" 5 of `.*\``; "#, }); assert_eq!(output.unwrap(), "(?:.*`){5}"); } #[test] fn assertion_test() { let output = compiler(indoc! { r#" ahead { "a"; } behind { "a"; } not ahead { "a"; } not behind { "a"; } "#, }); assert_eq!(output.unwrap(), "(?=a)(?<=a)(?!a)(?