From 0e3127cda66376d1eb895b84b17a9acc40df8482 Mon Sep 17 00:00:00 2001 From: Jan Tache Date: Sat, 11 Nov 2023 15:07:11 -0800 Subject: [PATCH] add unit tests --- keyberon/src/action/switch.rs | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/keyberon/src/action/switch.rs b/keyberon/src/action/switch.rs index 6358dc7bc..e6983d160 100644 --- a/keyberon/src/action/switch.rs +++ b/keyberon/src/action/switch.rs @@ -665,3 +665,66 @@ fn switch_historical_1() { false ); } + +#[test] +fn switch_historical_bools() { + let opcodes_true_and = [ + OpCode::new_bool(And, 3), + OpCode::new_key_history(KeyCode::A, 0), + OpCode::new_key_history(KeyCode::B, 1), + ]; + let opcodes_false_and1 = [ + OpCode::new_bool(And, 3), + OpCode::new_key_history(KeyCode::A, 0), + OpCode::new_key_history(KeyCode::B, 2), + ]; + let opcodes_false_and2 = [ + OpCode::new_bool(And, 3), + OpCode::new_key_history(KeyCode::B, 2), + OpCode::new_key_history(KeyCode::A, 0), + ]; + let opcodes_true_or1 = [ + OpCode::new_bool(Or, 3), + OpCode::new_key_history(KeyCode::A, 0), + OpCode::new_key_history(KeyCode::B, 1), + ]; + let opcodes_true_or2 = [ + OpCode::new_bool(Or, 3), + OpCode::new_key_history(KeyCode::A, 0), + OpCode::new_key_history(KeyCode::B, 2), + ]; + let opcodes_true_or3 = [ + OpCode::new_bool(Or, 3), + OpCode::new_key_history(KeyCode::B, 2), + OpCode::new_key_history(KeyCode::A, 0), + ]; + let opcodes_false_or = [ + OpCode::new_bool(Or, 3), + OpCode::new_key_history(KeyCode::A, 1), + OpCode::new_key_history(KeyCode::B, 2), + ]; + let hist_keycodes = [ + KeyCode::A, + KeyCode::B, + KeyCode::C, + KeyCode::D, + KeyCode::E, + KeyCode::F, + KeyCode::G, + KeyCode::H, + ]; + + let test = |opcodes: &[OpCode], expectation: bool| { + assert_eq!( + evaluate_boolean(opcodes, [].iter().copied(), hist_keycodes.iter().copied(),), + expectation + ); + }; + test(&opcodes_true_and, true); + test(&opcodes_true_or1, true); + test(&opcodes_true_or2, true); + test(&opcodes_true_or3, true); + test(&opcodes_false_and1, false); + test(&opcodes_false_and2, false); + test(&opcodes_false_or, false); +}