summaryrefslogtreecommitdiffstats
path: root/src/recite.rs
diff options
context:
space:
mode:
authorRory Dudley2024-02-29 00:25:05 -0700
committerRory Dudley2024-02-29 00:25:05 -0700
commit632b0f2a145df1c8a5d352e5ec91809ea939b1c7 (patch)
treebc506e2c9342868e59886f23af6eca0ff0c91708 /src/recite.rs
parent691444ca356820a0174d46a6cf1db0f65abae521 (diff)
downloaddwarvish-pipes.tar.gz
Parser testspipes
Added twelve different tests for the parser (Poem::read).
Diffstat (limited to 'src/recite.rs')
-rw-r--r--src/recite.rs157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/recite.rs b/src/recite.rs
index a655aa7..bd72b67 100644
--- a/src/recite.rs
+++ b/src/recite.rs
@@ -684,3 +684,160 @@ impl Poem {
Some(Poem::new(verses))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn it_parses_a_verse_with_no_meter() {
+ let poem = Poem::read("cargo build --release".to_string());
+ assert!(poem.is_some());
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.first().unwrap().verb(), "cargo");
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_couplet_meter() {
+ let poem = Poem::read("ls -la | lolcat".to_string());
+ assert!(poem.is_some());
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.first().unwrap().verb(), "ls");
+ assert_eq!(poem.verses.first().unwrap().meter, Meter::Couplet);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_quiet_meter() {
+ let poem = Poem::read("sleep 20 &".to_string());
+ assert!(poem.is_some());
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.verses.first().unwrap().meter, Meter::Quiet);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_and_meter() {
+ let poem = Poem::read("sleep 2 && ls -la".to_string());
+ assert!(poem.is_some());
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.verses.first().unwrap().meter, Meter::And);
+ }
+
+ #[test]
+ fn it_parses_a_verse_with_the_string_meter() {
+ let poem = Poem::read("sleep 2; ls -la".to_string());
+ assert!(poem.is_some());
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.first().unwrap().verb(), "sleep");
+ assert_eq!(poem.verses.first().unwrap().meter, Meter::String);
+ }
+
+ #[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());
+ assert!(poem.is_some());
+ let mut verses = poem.unwrap().verses.into_iter();
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "ls");
+ assert_eq!(verse.clause(), vec!["-la".to_string()]);
+ assert_eq!(verse.meter, Meter::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Meter::And);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "echo");
+ assert_eq!(verse.clause(), vec!["hello".to_string()]);
+ assert_eq!(verse.meter, Meter::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Meter::And);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "sleep");
+ assert_eq!(verse.clause(), vec!["2".to_string()]);
+ assert_eq!(verse.meter, Meter::Quiet);
+ }
+
+ #[test]
+ fn it_parses_the_string_meter_without_a_stanza() {
+ let poem = Poem::read(";;;;;;;".to_string());
+ assert!(poem.is_some());
+ }
+
+ #[test]
+ fn it_errors_if_the_couplet_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("|".to_string());
+ assert!(poem.is_none());
+ }
+
+ #[test]
+ fn it_errors_if_the_quiet_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("&".to_string());
+ assert!(poem.is_none());
+ }
+
+ #[test]
+ fn it_errors_if_the_and_meter_is_used_without_a_stanza() {
+ let poem = Poem::read("&&".to_string());
+ assert!(poem.is_none());
+ }
+
+ #[test]
+ fn it_parses_a_file() {
+ let file = r"
+ ps aux | lolcat
+ sleep 2
+ ";
+
+ let poem = Poem::read(file.to_string());
+ assert!(poem.is_some());
+
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.len(), 3);
+
+ let mut verses = poem.verses.into_iter();
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "ps");
+ assert_eq!(verse.clause(), vec!["aux".to_string()]);
+ assert_eq!(verse.meter, Meter::Couplet);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "lolcat");
+ assert_eq!(verse.meter, Meter::String);
+
+ let verse = verses.next().unwrap();
+ assert_eq!(verse.verb(), "sleep");
+ assert_eq!(verse.clause(), vec!["2".to_string()]);
+ assert_eq!(verse.meter, Meter::String);
+ }
+
+ #[test]
+ fn it_parses_a_longer_file() {
+ let file = r"
+ ps aux | lolcat
+ sleep 2
+ ps aux | lolcat
+ sleep 2
+
+ echo hello there
+ export PATH=$PATH:~/.local/bin
+
+ ps aux | lolcat && lolcat src/main.rs
+ fortune | cowsay | lolcat
+
+ wc -l src/**/*.rs | lolcat; ls -la | grep git
+ ";
+
+ let poem = Poem::read(file.to_string());
+ assert!(poem.is_some());
+
+ let poem = poem.unwrap();
+ assert_eq!(poem.verses.len(), 18);
+ }
+}