Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/windowing/winit/event_loop/key_events_tests.rs
StratoSDK / crates / strato-ui-renderer / src / windowing / winit / event_loop / key_events_tests.rs
1use super::get_input_key;
2use winit::keyboard::{Key::Character, SmolStr};
3 
4#[test]
5fn test_get_input_key() {
6 // Tests all visible ASCII characters
7 // TODO: it would be nice to test the following:
8 // - non-Character keys (ex: named keys, dead keys)
9 // - non-ascii characters to ensure shift behavior is appropriate
10 for ascii_code in 32u8..127u8 {
11 let input = ascii_code as char;
12 let key = Character(SmolStr::from(input.to_string()));
13 
14 for shift in [false, true] {
15 match get_input_key(&key, shift) {
16 Character(new_value) => {
17 let new_char = new_value
18 .chars()
19 .next()
20 .expect("string should be non-empty");
21 
22 let expected = match (input, shift) {
23 ('A'..='Z', false) => input
24 .to_lowercase()
25 .next()
26 .expect("string should be non-empty"),
27 // Case 2: a lower case letter when shift is true
28 // Should turn into upper case version
29 ('a'..='z', true) => input
30 .to_uppercase()
31 .next()
32 .expect("string should be non-empty"),
33 // Case 3: a character that should be unchanged by caps lock
34 // - An upper-case letter when shift is true
35 // - A lower-case letter when shift is false,
36 // - A non-alpha character
37 _ => input,
38 };
39 assert_eq!(
40 expected, new_char,
41 "Expected '{input}' -> '{expected}' when shift={shift}, but got '{new_char}'"
42 )
43 }
44 unexpected => {
45 panic!("Key '{key:?}' somehow became non-character {unexpected:?}")
46 }
47 }
48 }
49 }
50}
51