Skip to content

add unit tests for commands #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,66 @@ fn key_value_pair(s: &'static str) -> Option<&'static str> {
})
.flatten()
}

mod test {
use super::*;

#[test]
fn existing_commands_are_parsed_as_expected() {
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("?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.add("?crate query...", |_: Args| Ok(()));

// tags

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);
});
}
}