Skip to content

Commit 3e0ad55

Browse files
committed
add more tests for deserialization error
1 parent ac122d8 commit 3e0ad55

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
22
name = "keybinds"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
edition = "2021"
55
authors = ["rhysd <[email protected]>"]
6-
description = "Platform&Framework-agnostic key bindings parser and dispatcher"
6+
description = "Platform&Framework-agnostic key binding (keyboard shortcut) parser and dispatcher"
77
license = "MIT"
88
homepage = "https://github.com/rhysd/keybinds-rs#readme"
99
documentation = "https://docs.rs/keybinds"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ keybinds-rs
66
**THIS CRATE IS WORK IN PROGRESS YET. The first beta release is planned as 0.1.0. Until then, this
77
library can be buggy and have arbitrary breaking changes.**
88

9-
[keybinds-rs][crates-io] is a small Rust crate to define/parse/dispatch key bindings.
9+
[keybinds-rs][crates-io] is a small Rust crate to define/parse/dispatch key bindings (keyboard shortcuts).
1010

1111
- Provide the syntax to easily define key bindings in a configuration file like `Ctrl+a`. ([document](./doc/binding_syntax.md))
1212
- Support key sequences like `Ctrl+x Ctrl+s` for complicated key bindings like Vim style.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Overview
22
//!
3-
//! [keybinds-rs][crates-io] is a small Rust crate to define/parse/dispatch key bindings.
3+
//! [keybinds-rs][crates-io] is a small Rust crate to define/parse/dispatch key bindings (keyboard shortcuts).
44
//!
55
//! - Provide a syntax to easily define key bindings in a configuration file like `Ctrl+a`
66
//! - Support key sequences like `Ctrl+x Ctrl+s` for complicated key bindings like Vim style

src/serde.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod tests {
131131
}
132132

133133
#[test]
134-
fn deserialize_key_binds() {
134+
fn deserialize_key_bindings_ok() {
135135
let input = r#"
136136
[bindings]
137137
"j" = "Action1"
@@ -163,16 +163,35 @@ mod tests {
163163
assert_eq!(actual.deref(), &expected);
164164
}
165165

166+
#[test]
167+
fn deserialize_empty_table() {
168+
let _: Keybinds<A> = toml::from_str("").unwrap();
169+
}
170+
171+
#[test]
172+
fn deserialize_key_bindings_error() {
173+
let tests = [
174+
r#""x" = 12"#,
175+
r#""x" = "Action123456""#,
176+
r#""" = "Action1""#,
177+
r#"" " = "Action1""#,
178+
r#""Foooo" = "Action1""#,
179+
r#""Foooo+x" = "Action1""#,
180+
r#""Ctrl+Fooooo" = "Action1""#,
181+
r#""Shift+a" = "Action1""#, // Error because it violates invariant
182+
];
183+
184+
for input in tests {
185+
let _ = toml::from_str::<Keybinds<A>>(input)
186+
.expect_err(&format!("invalid input {input:?}"));
187+
}
188+
}
189+
166190
#[test]
167191
fn deserialize_mod_key_bind() {
168192
let input = r#""Mod+x" = "Action1""#;
169193
let actual: Keybinds<A> = toml::from_str(input).unwrap();
170-
let expected = [
171-
#[cfg(target_os = "macos")]
172-
Keybind::new(KeyInput::new('x', Mods::CMD), A::Action1),
173-
#[cfg(not(target_os = "macos"))]
174-
Keybind::new(KeyInput::new('x', Mods::CTRL), A::Action1),
175-
];
194+
let expected = [Keybind::new(KeyInput::new('x', Mods::MOD), A::Action1)];
176195
assert_eq!(actual.deref(), expected);
177196
}
178197
}

0 commit comments

Comments
 (0)