aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/tests/command_dispatcher_usages_test.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2023-10-12 20:14:29 -0500
committermat <git@matdoes.dev>2023-10-12 20:14:29 -0500
commit38db231ea8fa0fb223e16637db0b6ec65b2b81ef (patch)
treef8f61b04ad0b498b85bffb133c51af1670a33ddf /azalea-brigadier/tests/command_dispatcher_usages_test.rs
parentd5f424b8c2fba9b3283aef36fe9e1e051636614c (diff)
downloadazalea-drasl-38db231ea8fa0fb223e16637db0b6ec65b2b81ef.tar.xz
brigadier usages
Diffstat (limited to 'azalea-brigadier/tests/command_dispatcher_usages_test.rs')
-rwxr-xr-xazalea-brigadier/tests/command_dispatcher_usages_test.rs142
1 files changed, 142 insertions, 0 deletions
diff --git a/azalea-brigadier/tests/command_dispatcher_usages_test.rs b/azalea-brigadier/tests/command_dispatcher_usages_test.rs
index 8b137891..d7baff89 100755
--- a/azalea-brigadier/tests/command_dispatcher_usages_test.rs
+++ b/azalea-brigadier/tests/command_dispatcher_usages_test.rs
@@ -1 +1,143 @@
+use std::{collections::HashSet, sync::Arc};
+use azalea_brigadier::{prelude::*, tree::CommandNode};
+use parking_lot::RwLock;
+
+fn setup() -> CommandDispatcher<()> {
+ let command = |_: &CommandContext<()>| 0;
+
+ let mut subject = CommandDispatcher::new();
+ subject.register(
+ literal("a")
+ .then(
+ literal("1")
+ .then(literal("i").executes(command))
+ .then(literal("ii").executes(command)),
+ )
+ .then(
+ literal("2")
+ .then(literal("i").executes(command))
+ .then(literal("ii").executes(command)),
+ ),
+ );
+ subject.register(literal("b").then(literal("1").executes(command)));
+ subject.register(literal("c").executes(command));
+ subject.register(literal("d").requires(|_| false).executes(command));
+ subject.register(
+ literal("e").executes(command).then(
+ literal("1")
+ .executes(command)
+ .then(literal("i").executes(command))
+ .then(literal("ii").executes(command)),
+ ),
+ );
+ subject.register(
+ literal("f")
+ .then(
+ literal("1")
+ .then(literal("i").executes(command))
+ .then(literal("ii").executes(command).requires(|_| false)),
+ )
+ .then(
+ literal("2")
+ .then(literal("i").executes(command).requires(|_| false))
+ .then(literal("ii").executes(command)),
+ ),
+ );
+ subject.register(
+ literal("g")
+ .executes(command)
+ .then(literal("1").then(literal("i").executes(command))),
+ );
+ subject.register(
+ literal("h")
+ .executes(command)
+ .then(literal("1").then(literal("i").executes(command)))
+ .then(literal("2").then(literal("i").then(literal("ii").executes(command))))
+ .then(literal("3").executes(command)),
+ );
+ subject.register(
+ literal("i")
+ .executes(command)
+ .then(literal("1").executes(command))
+ .then(literal("2").executes(command)),
+ );
+ subject.register(literal("j").redirect(subject.root.clone()));
+ subject.register(literal("k").redirect(get(&subject, "h")));
+ subject
+}
+
+fn get(subject: &CommandDispatcher<()>, command: &str) -> Arc<RwLock<CommandNode<()>>> {
+ subject
+ .parse(command.into(), ())
+ .context
+ .nodes
+ .last()
+ .unwrap()
+ .node
+ .clone()
+}
+
+#[test]
+fn test_all_usage_no_commands() {
+ let subject = CommandDispatcher::<()>::new();
+ let results = subject.get_all_usage(&subject.root.read(), Arc::new(()), true);
+ assert!(results.is_empty());
+}
+
+#[test]
+fn test_smart_usage_no_commands() {
+ let subject = CommandDispatcher::<()>::new();
+ let results = subject.get_smart_usage(&subject.root.read(), Arc::new(()));
+ assert!(results.is_empty());
+}
+
+#[test]
+fn test_all_usage_root() {
+ let subject = setup();
+ let results = subject.get_all_usage(&subject.root.read(), Arc::new(()), true);
+
+ let actual = results.into_iter().collect::<HashSet<_>>();
+ let expected = vec![
+ "a 1 i", "a 1 ii", "a 2 i", "a 2 ii", "b 1", "c", "e", "e 1", "e 1 i", "e 1 ii", "f 1 i",
+ "f 2 ii", "g", "g 1 i", "h", "h 1 i", "h 2 i ii", "h 3", "i", "i 1", "i 2", "j ...",
+ "k -> h",
+ ]
+ .into_iter()
+ .map(|s| s.to_owned())
+ .collect::<HashSet<_>>();
+ assert_eq!(expected, actual);
+}
+
+#[test]
+fn test_smart_usage_root() {
+ let subject = setup();
+ let results = subject.get_smart_usage(&subject.root.read(), Arc::new(()));
+
+ let actual = results
+ .into_iter()
+ .map(|(k, v)| (k.read().name().to_owned(), v))
+ .collect::<HashSet<_>>();
+
+ let expected = vec![
+ (get(&subject, "a"), "a (1|2)"),
+ (get(&subject, "b"), "b 1"),
+ (get(&subject, "c"), "c"),
+ (get(&subject, "e"), "e [1]"),
+ (get(&subject, "f"), "f (1|2)"),
+ (get(&subject, "g"), "g [1]"),
+ (get(&subject, "h"), "h [1|2|3]"),
+ (get(&subject, "i"), "i [1|2]"),
+ (get(&subject, "j"), "j ..."),
+ (get(&subject, "k"), "k -> h"),
+ ];
+
+ println!("-");
+
+ let expected = expected
+ .into_iter()
+ .map(|(k, v)| (k.read().name().to_owned(), v.to_owned()))
+ .collect::<HashSet<_>>();
+
+ assert_eq!(actual, expected);
+}