From 21735aee8c0522ad5b80022f909fa764a26f7c99 Mon Sep 17 00:00:00 2001 From: technetos Date: Wed, 7 Jul 2021 20:46:41 -0400 Subject: [PATCH 1/2] add unit tests for commands --- src/commands.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/commands.rs b/src/commands.rs index a2fcf66..1f650dc 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -374,3 +374,44 @@ fn key_value_pair(s: &'static str) -> Option<&'static str> { }) .flatten() } + +mod test { + use super::*; + + #[test] + fn test_commands() { + fn auth_check(args: &Args) -> Result { + Ok(true) + } + + macro_rules! params { + ($(($key:literal, $value:literal)),+) => ( + [$(($key, $value)),+].iter().fold( + HashMap::new(), + |mut hash_map, pair| { + hash_map.insert(pair.0, pair.1); + hash_map + }, + ) + ); + } + + let mut cmds = Commands::new(); + + cmds.add_protected("?tags delete {key}", |_: Args| Ok(()), auth_check); + + cmds.state_machine + .process("?tags delete foo") + .map(|matched| { + assert!(params!(("key", "foo")) == matched.params); + }); + + cmds.add_protected("?tags create {key} value...", |_: Args| Ok(()), auth_check); + + cmds.state_machine + .process("?tags create foo foo bar baz") + .map(|matched| { + assert!(params!(("key", "foo"), ("value", "foo bar baz")) == matched.params); + }); + } +} From b6ab7fece66c636aa20a623b45cc150f68ff1fdb Mon Sep 17 00:00:00 2001 From: technetos Date: Thu, 8 Jul 2021 21:31:47 -0400 Subject: [PATCH 2/2] improve unit test --- src/commands.rs | 56 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 1f650dc..a3d7ed5 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -379,11 +379,7 @@ mod test { use super::*; #[test] - fn test_commands() { - fn auth_check(args: &Args) -> Result { - Ok(true) - } - + fn existing_commands_are_parsed_as_expected() { macro_rules! params { ($(($key:literal, $value:literal)),+) => ( [$(($key, $value)),+].iter().fold( @@ -398,20 +394,46 @@ mod test { let mut cmds = Commands::new(); - cmds.add_protected("?tags delete {key}", |_: Args| Ok(()), auth_check); + cmds.add("?tags delete {key}", |_: Args| Ok(())); + cmds.add("?tags create {key} value...", |_: Args| Ok(())); + cmds.add("?tags update {key} value...", |_: Args| Ok(())); + cmds.add("?tag {key}", |_: Args| Ok(())); + cmds.add("?tags", |_: Args| Ok(())); - cmds.state_machine - .process("?tags delete foo") - .map(|matched| { - assert!(params!(("key", "foo")) == matched.params); - }); + cmds.add("?crate query...", |_: Args| Ok(())); - cmds.add_protected("?tags create {key} value...", |_: Args| Ok(()), auth_check); + // tags - cmds.state_machine - .process("?tags create foo foo bar baz") - .map(|matched| { - assert!(params!(("key", "foo"), ("value", "foo bar baz")) == matched.params); - }); + let tags_delete = cmds.state_machine.process("?tags delete foo"); + assert!(tags_delete.is_some()); + tags_delete.map(|matched| assert!(params!(("key", "foo")) == matched.params)); + + let tags_create = cmds.state_machine.process("?tags create foo foo bar baz"); + assert!(tags_create.is_some()); + tags_create.map(|matched| { + assert!(params!(("key", "foo"), ("value", "foo bar baz")) == matched.params); + }); + + let tags_update = cmds.state_machine.process("?tags update foo 123 456 abc"); + assert!(tags_update.is_some()); + tags_update.map(|matched| { + assert!(params!(("key", "foo"), ("value", "123 456 abc")) == matched.params); + }); + + let tag = cmds.state_machine.process("?tag foo"); + assert!(tag.is_some()); + tag.map(|matched| { + assert!(params!(("key", "foo")) == matched.params); + }); + + assert!(cmds.state_machine.process("?tags").is_some()); + + // crates + + let krate = cmds.state_machine.process("?crate 12345abc"); + assert!(krate.is_some()); + krate.map(|matched| { + assert!(params!(("query", "12345abc")) == matched.params); + }); } }