Skip to content

Commit

Permalink
fix: add short-circuiting to key-history (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo authored Nov 11, 2023
1 parent 53c3462 commit abb53c9
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions keyberon/src/action/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ fn evaluate_boolean(
.nth(hkc.how_far_back as usize)
.map(|kc| kc as u16 == hkc.key_code)
.unwrap_or(false);
if matches!((ret, current_op), (true, Or) | (false, And)) {
current_index = current_end_index;
continue;
}
}
OpCodeType::BooleanOp(operator) => {
let res = stack.push_back(OperatorAndEndIndex {
Expand Down Expand Up @@ -661,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);
}

0 comments on commit abb53c9

Please sign in to comment.