mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-08 06:47:56 +00:00
fix infinite loop backtracking for valid branches, cleanup matched tokens type
This commit is contained in:
parent
9122e64a41
commit
f9367ea041
3 changed files with 86 additions and 26 deletions
45
crates/command_parser/tests/parser.rs
Normal file
45
crates/command_parser/tests/parser.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use command_parser::{parse_command, Tree, command::Command, parameter::*, tokens};
|
||||
|
||||
/// this checks if we properly keep track of filtered tokens (eg. branches we failed on)
|
||||
/// when we backtrack. a previous parser bug would cause infinite loops since it did not
|
||||
/// (the parser would "flip-flop" between branches) this is here for reference.
|
||||
#[test]
|
||||
fn test_infinite_loop_repro() {
|
||||
let p1 = Optional(("param1", ParameterKind::OpaqueString));
|
||||
let p2 = Optional(("param2", ParameterKind::OpaqueString));
|
||||
|
||||
let cmd1 = Command::new(tokens!("s", p1, "A"), "cmd1");
|
||||
let cmd2 = Command::new(tokens!("s", p2, "B"), "cmd2");
|
||||
|
||||
let mut tree = Tree::default();
|
||||
tree.register_command(cmd1);
|
||||
tree.register_command(cmd2);
|
||||
|
||||
let input = "s foo C";
|
||||
// this should fail and not loop
|
||||
let result = parse_command(tree, "pk;".to_string(), input.to_string());
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
/// check if we have params from other branches when we trying to match them and they succeeded
|
||||
/// but then we backtracked, making them invalid. this should no longer happen since we just
|
||||
/// extract params from matched tokens when we match the command, but keeping here just for reference.
|
||||
#[test]
|
||||
fn test_dirty_params() {
|
||||
let p1 = Optional(("param1", ParameterKind::OpaqueString));
|
||||
let p2 = Optional(("param2", ParameterKind::OpaqueString));
|
||||
|
||||
let cmd1 = Command::new(tokens!("s", p1, "A"), "cmd1");
|
||||
let cmd2 = Command::new(tokens!("s", p2, "B"), "cmd2");
|
||||
|
||||
let mut tree = Tree::default();
|
||||
tree.register_command(cmd1);
|
||||
tree.register_command(cmd2);
|
||||
|
||||
let input = "s foo B";
|
||||
let result = parse_command(tree, "pk;".to_string(), input.to_string()).unwrap();
|
||||
|
||||
println!("params: {:?}", result.parameters);
|
||||
assert!(!result.parameters.contains_key("param1"), "params should not contain 'param1' from failed branch");
|
||||
assert!(result.parameters.contains_key("param2"), "params should contain 'param2'");
|
||||
}
|
||||
|
|
@ -54,4 +54,4 @@ fn test_typoed_command_with_flags() {
|
|||
assert!(msg.contains("message author"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue