Skip to content

Commit dc2088d

Browse files
committed
Add tests for prefix support
1 parent a95bea9 commit dc2088d

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tests/numeric_prefix.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
2+
3+
fn is_valid_numeric_prefix_char(c: char, current_prefix: &str) -> bool {
4+
c.is_ascii_digit() && (c != '0' || !current_prefix.is_empty())
5+
}
6+
7+
fn simulate_numeric_input(inputs: &[char]) -> String {
8+
let mut prefix = String::new();
9+
10+
for &c in inputs {
11+
if is_valid_numeric_prefix_char(c, &prefix) {
12+
prefix.push(c);
13+
} else {
14+
break;
15+
}
16+
}
17+
18+
prefix
19+
}
20+
21+
#[test]
22+
fn test_numeric_prefix_single_digit() {
23+
assert_eq!(simulate_numeric_input(&['5']), "5");
24+
assert_eq!(simulate_numeric_input(&['1']), "1");
25+
assert_eq!(simulate_numeric_input(&['9']), "9");
26+
}
27+
28+
#[test]
29+
fn test_numeric_prefix_multiple_digits() {
30+
assert_eq!(simulate_numeric_input(&['1', '2', '3']), "123");
31+
assert_eq!(simulate_numeric_input(&['4', '2']), "42");
32+
assert_eq!(simulate_numeric_input(&['9', '9', '9']), "999");
33+
}
34+
35+
#[test]
36+
fn test_numeric_prefix_zero_rules() {
37+
// Zero should not be accepted as the first character
38+
assert_eq!(simulate_numeric_input(&['0']), "");
39+
40+
// But zero should be accepted after other digits
41+
assert_eq!(simulate_numeric_input(&['1', '0']), "10");
42+
assert_eq!(simulate_numeric_input(&['2', '0', '5']), "205");
43+
}
44+
45+
#[test]
46+
fn test_numeric_prefix_stops_at_non_digit() {
47+
assert_eq!(simulate_numeric_input(&['5', 'j']), "5");
48+
assert_eq!(simulate_numeric_input(&['1', '2', 'k']), "12");
49+
assert_eq!(simulate_numeric_input(&['3', ' ']), "3");
50+
}
51+
52+
#[test]
53+
fn test_numeric_prefix_empty_for_non_numeric_start() {
54+
assert_eq!(simulate_numeric_input(&['j']), "");
55+
assert_eq!(simulate_numeric_input(&['k']), "");
56+
assert_eq!(simulate_numeric_input(&[' ']), "");
57+
}
58+
59+
#[test]
60+
fn test_numeric_prefix_parsing() {
61+
assert_eq!("5".parse::<usize>().unwrap_or(1), 5);
62+
assert_eq!("42".parse::<usize>().unwrap_or(1), 42);
63+
assert_eq!("999".parse::<usize>().unwrap_or(1), 999);
64+
assert_eq!("".parse::<usize>().unwrap_or(1), 1);
65+
assert_eq!("abc".parse::<usize>().unwrap_or(1), 1);
66+
}
67+
68+
#[test]
69+
fn test_key_event_digit_detection() {
70+
let digit_events = [
71+
KeyEvent::new(KeyCode::Char('0'), KeyModifiers::empty()),
72+
KeyEvent::new(KeyCode::Char('1'), KeyModifiers::empty()),
73+
KeyEvent::new(KeyCode::Char('5'), KeyModifiers::empty()),
74+
KeyEvent::new(KeyCode::Char('9'), KeyModifiers::empty()),
75+
];
76+
77+
for event in digit_events {
78+
if let KeyCode::Char(c) = event.code {
79+
assert!(c.is_ascii_digit());
80+
}
81+
}
82+
83+
let non_digit_events = [
84+
KeyEvent::new(KeyCode::Char('j'), KeyModifiers::empty()),
85+
KeyEvent::new(KeyCode::Char('k'), KeyModifiers::empty()),
86+
KeyEvent::new(KeyCode::Enter, KeyModifiers::empty()),
87+
KeyEvent::new(KeyCode::Esc, KeyModifiers::empty()),
88+
];
89+
90+
for event in non_digit_events {
91+
if let KeyCode::Char(c) = event.code {
92+
assert!(!c.is_ascii_digit())
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)