From c2ce134cd393f397cb8b1e584949020c2c030f8d Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sun, 3 Mar 2024 01:41:21 -0700 Subject: Add unit tests for the new IO meters Added six new units tests related to the IO meters (Read, Write, and Addendum). The first three tests check the meters under normal operation , and the last three tests ensures that the parser throws an error, unless one or more files where specified for the operation. --- src/recite.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) (limited to 'src') diff --git a/src/recite.rs b/src/recite.rs index 12c98ae..05e49f3 100644 --- a/src/recite.rs +++ b/src/recite.rs @@ -1039,6 +1039,80 @@ mod tests { assert_eq!(poem.verses.first().unwrap().meter, Meter::String); } + #[test] + fn it_parses_verse_with_the_read_meter() { + let poem = Poem::read("lolcat < src/main.rs".to_string()); + assert!(poem.is_some()); + let mut verses = poem.unwrap().verses.into_iter(); + + let verse = verses.next().unwrap(); + assert_eq!(verse.verb(), "lolcat"); + assert_eq!(verse.meter, Meter::Read); + + let verse = verses.next().unwrap(); + assert_eq!(verse.stanza(), vec!["src/main.rs".to_string()]); + } + + #[test] + fn it_parses_verse_with_the_write_meter() { + let poem = Poem::read("cat src/main.rs > /dev/null".to_string()); + assert!(poem.is_some()); + let mut verses = poem.unwrap().verses.into_iter(); + + let verse = verses.next().unwrap(); + assert_eq!(verse.verb(), "cat"); + assert_eq!(verse.clause(), vec!["src/main.rs".to_string()]); + assert_eq!(verse.meter, Meter::Write); + + let verse = verses.next().unwrap(); + assert_eq!(verse.stanza(), vec!["/dev/null".to_string()]); + } + + #[test] + fn it_parses_verse_with_the_addenum_meter() { + let poem = Poem::read("cat src/main.rs >> /dev/null".to_string()); + assert!(poem.is_some()); + let mut verses = poem.unwrap().verses.into_iter(); + + let verse = verses.next().unwrap(); + assert_eq!(verse.verb(), "cat"); + assert_eq!(verse.clause(), vec!["src/main.rs".to_string()]); + assert_eq!(verse.meter, Meter::Addendum); + + let verse = verses.next().unwrap(); + assert_eq!(verse.stanza(), vec!["/dev/null".to_string()]); + } + + #[test] + fn it_throws_a_parse_error_if_no_files_are_specified_for_the_read_meter() { + let poem = Poem::read("lolcat <".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("lolcat <;".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("lolcat < && ls -la".to_string()); + assert!(poem.is_none()); + } + + #[test] + fn it_throws_a_parse_error_if_no_files_are_specified_for_the_write_meter() { + let poem = Poem::read("cat src/main.rs >".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("cat src/main.rs >;".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("cat > && ls -la".to_string()); + assert!(poem.is_none()); + } + + #[test] + fn it_throws_a_parse_error_if_no_files_are_specified_for_the_addendum_meter() { + let poem = Poem::read("cat src/main.rs >>".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("cat src/main.rs >>;".to_string()); + assert!(poem.is_none()); + let poem = Poem::read("cat >> && ls -la".to_string()); + assert!(poem.is_none()); + } + #[test] fn it_parses_a_complex_verse_with_lots_of_different_meters() { let poem = Poem::read("ls -la | lolcat && echo hello | lolcat && sleep 2 &".to_string()); -- cgit v1.2.3