diff options
author | Rory Dudley | 2024-04-04 22:12:14 -0600 |
---|---|---|
committer | Rory Dudley | 2024-04-04 22:12:14 -0600 |
commit | 1415c8f9b89699000ef8d864ff8f0e1bebca4a5f (patch) | |
tree | 64093c0eded6a6f28105dbd743729b4075d32889 /src/poem.rs | |
parent | badbba41476cd6fd424042c48681acde82227ba6 (diff) | |
download | dwarvish-1415c8f9b89699000ef8d864ff8f0e1bebca4a5f.tar.gz |
Handle aliases in read()
Instead of handling aliases in the recite() function, which requires two
loops to handle properly with the current implementation, offload
checking for aliases to the read() function.
Diffstat (limited to 'src/poem.rs')
-rw-r--r-- | src/poem.rs | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/src/poem.rs b/src/poem.rs index 0eda6b7..a063a14 100644 --- a/src/poem.rs +++ b/src/poem.rs @@ -15,10 +15,11 @@ mod tests { use super::elements::rune::Rune; use super::read::Readable; use super::*; + use crate::compose::Environment; #[test] fn it_parses_a_verse_with_no_meter() { - let poem = Poem::read("cargo build --release".to_string()); + let poem = Poem::read("cargo build --release".to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); assert_eq!(poem.first().unwrap().verb(), "cargo"); @@ -26,7 +27,7 @@ mod tests { #[test] fn it_parses_a_verse_with_the_couplet_meter() { - let poem = Poem::read("ls -la | lolcat".to_string()); + let poem = Poem::read("ls -la | lolcat".to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); assert_eq!(poem.first().unwrap().verb(), "ls"); @@ -35,7 +36,7 @@ mod tests { #[test] fn it_parses_a_verse_with_the_quiet_meter() { - let poem = Poem::read("sleep 20 &".to_string()); + let poem = Poem::read("sleep 20 &".to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); assert_eq!(poem.first().unwrap().verb(), "sleep"); @@ -44,7 +45,7 @@ mod tests { #[test] fn it_parses_a_verse_with_the_and_meter() { - let poem = Poem::read("sleep 2 && ls -la".to_string()); + let poem = Poem::read("sleep 2 && ls -la".to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); assert_eq!(poem.first().unwrap().verb(), "sleep"); @@ -53,7 +54,7 @@ mod tests { #[test] fn it_parses_a_verse_with_the_continue_meter() { - let poem = Poem::read("sleep 2; ls -la".to_string()); + let poem = Poem::read("sleep 2; ls -la".to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); assert_eq!(poem.first().unwrap().verb(), "sleep"); @@ -62,7 +63,7 @@ mod tests { #[test] fn it_parses_a_verse_with_the_read_rune() { - let poem = Poem::read("lolcat < src/main.rs".to_string()); + let poem = Poem::read("lolcat < src/main.rs".to_string(), &Environment::new()); assert!(poem.is_ok()); let mut verses = poem.unwrap().into_iter(); let verse = verses.next().unwrap(); @@ -78,7 +79,10 @@ mod tests { #[test] fn it_parses_a_verse_with_the_write_rune() { - let poem = Poem::read("cat src/main.rs > /dev/null".to_string()); + let poem = Poem::read( + "cat src/main.rs > /dev/null".to_string(), + &Environment::new(), + ); assert!(poem.is_ok()); let mut verses = poem.unwrap().into_iter(); let verse = verses.next().unwrap(); @@ -94,7 +98,10 @@ mod tests { #[test] fn it_parses_a_verse_with_the_addendum_rune() { - let poem = Poem::read("cat src/main.rs >> /dev/null".to_string()); + let poem = Poem::read( + "cat src/main.rs >> /dev/null".to_string(), + &Environment::new(), + ); assert!(poem.is_ok()); let mut verses = poem.unwrap().into_iter(); let verse = verses.next().unwrap(); @@ -110,37 +117,40 @@ mod tests { #[test] fn it_throws_a_parse_error_if_no_files_are_specified_for_the_read_rune() { - let poem = Poem::read("lolcat <".to_string()); + let poem = Poem::read("lolcat <".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("lolcat <;".to_string()); + let poem = Poem::read("lolcat <;".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("lolcat < && ls -la".to_string()); + let poem = Poem::read("lolcat < && ls -la".to_string(), &Environment::new()); assert!(poem.is_err()); } #[test] fn it_throws_a_parse_error_if_no_files_are_specified_for_the_write_rune() { - let poem = Poem::read("cat src/main.rs >".to_string()); + let poem = Poem::read("cat src/main.rs >".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("cat src/main.rs >;".to_string()); + let poem = Poem::read("cat src/main.rs >;".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("cat > && ls -la".to_string()); + let poem = Poem::read("cat > && ls -la".to_string(), &Environment::new()); assert!(poem.is_err()); } #[test] fn it_throws_a_parse_error_if_no_files_are_specified_for_the_addendum_rune() { - let poem = Poem::read("cat src/main.rs >>".to_string()); + let poem = Poem::read("cat src/main.rs >>".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("cat src/main.rs >>;".to_string()); + let poem = Poem::read("cat src/main.rs >>;".to_string(), &Environment::new()); assert!(poem.is_err()); - let poem = Poem::read("cat >> && ls -la".to_string()); + let poem = Poem::read("cat >> && ls -la".to_string(), &Environment::new()); assert!(poem.is_err()); } #[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()); + let poem = Poem::read( + "ls -la | lolcat && echo hello | lolcat && sleep 2 &".to_string(), + &Environment::new(), + ); assert!(poem.is_ok()); let mut verses = poem.unwrap().into_iter(); @@ -170,25 +180,25 @@ mod tests { #[test] fn it_parses_the_continue_meter_without_a_stanza() { - let poem = Poem::read(";;;;;;;".to_string()); + let poem = Poem::read(";;;;;;;".to_string(), &Environment::new()); assert!(poem.is_ok()); } #[test] fn it_errors_if_the_couplet_meter_is_used_without_a_stanza() { - let poem = Poem::read("|".to_string()); + let poem = Poem::read("|".to_string(), &Environment::new()); assert!(poem.is_err()); } #[test] fn it_errors_if_the_quiet_meter_is_used_without_a_stanza() { - let poem = Poem::read("&".to_string()); + let poem = Poem::read("&".to_string(), &Environment::new()); assert!(poem.is_err()); } #[test] fn it_errors_if_the_and_meter_is_used_without_a_stanza() { - let poem = Poem::read("&&".to_string()); + let poem = Poem::read("&&".to_string(), &Environment::new()); assert!(poem.is_err()); } @@ -199,7 +209,7 @@ mod tests { sleep 2 "; - let poem = Poem::read(file.to_string()); + let poem = Poem::read(file.to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); @@ -239,7 +249,7 @@ mod tests { wc -l src/**/*.rs | lolcat; ls -la | grep git "; - let poem = Poem::read(file.to_string()); + let poem = Poem::read(file.to_string(), &Environment::new()); assert!(poem.is_ok()); let poem = poem.unwrap(); @@ -249,36 +259,36 @@ mod tests { #[test] fn it_catches_parser_errors_related_to_invalid_use_of_special_runes() { let poetry = "cat file.txt &&&".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); let poetry = "cat file.txt&&|".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); let poetry = "cat <".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); } #[test] fn it_catches_parser_errors_related_to_strings() { let poetry = "echo 'hello".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); let poetry = "echo \"hello".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); let poetry = "`true".to_string(); - assert_eq!(Poem::read(poetry).is_err(), true); + assert_eq!(Poem::read(poetry, &Environment::new()).is_err(), true); } #[test] fn it_interprets_tilda_as_home() { let poetry = "cd ~".to_string(); - let poem = Poem::read(poetry).unwrap(); + let poem = Poem::read(poetry, &Environment::new()).unwrap(); assert_eq!(poem[0].verb(), "cd"); assert_eq!(poem[0].clause(), Some(vec![env!("HOME").to_string()])); let poetry = "cd ~/Code/dwarvish".to_string(); - let poem = Poem::read(poetry).unwrap(); + let poem = Poem::read(poetry, &Environment::new()).unwrap(); assert_eq!(poem[0].verb(), "cd"); assert_eq!( poem[0].clause(), |