Skip to content

Commit

Permalink
Add FromStr impl for Hotkey and run tests on CI (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored May 27, 2024
1 parent ad8fa57 commit a0edec7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ jobs:
- name: Lint
run: cargo make lint

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get toolchain
id: toolchain
uses: ./.github/actions/get-toolchain
- name: Setup Rust
uses: hecrj/setup-rust-action@v2
with:
rust-version: ${{ steps.toolchain.outputs.channel }}
targets: wasm32-unknown-unknown
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}-${{ hashFiles('**/Cargo.lock') }}-${{ hashFiles('**/Makefile.toml') }}
- name: Test
run: cargo test

build-example:
name: Build example
runs-on: ubuntu-latest
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## Unreleased - [0.2.1]

### Bug fixes

- Fix right/left modifiers not being recognized.

### Enhancements

- Add `FromStr` implementation for `Hotkey` struct.

## _April 18th, 2024_ - [0.2.0]

### Breaking changes
Expand Down Expand Up @@ -36,4 +46,5 @@
- Elevate `leptos` to v0.6.5
- Added `event.preventDefault()`.

[0.2.1]: https://github.com/gaucho-labs/leptos-hotkeys/compare/v0.2.0...main
[0.2.0]: https://github.com/gaucho-labs/leptos-hotkeys/compare/b83afc96...v0.2.0
34 changes: 28 additions & 6 deletions leptos_hotkeys/src/hotkey.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::types::Keys;
use crate::KeyboardModifiers;
use core::str::FromStr;
use std::fmt::{Display, Formatter};

#[derive(Debug, PartialEq, Hash, Eq)]
Expand All @@ -26,6 +27,14 @@ impl Display for Hotkey {

impl Hotkey {
pub fn new(key_combination: &str) -> Self {
key_combination.parse().unwrap()
}
}

impl FromStr for Hotkey {
type Err = ();

fn from_str(key_combination: &str) -> Result<Self, Self::Err> {
let parts = key_combination
.split('+')
.map(str::trim)
Expand Down Expand Up @@ -62,7 +71,7 @@ impl Hotkey {
}
}

Hotkey { modifiers, keys }
Ok(Hotkey { modifiers, keys })
}
}

Expand Down Expand Up @@ -109,9 +118,8 @@ pub(crate) fn is_hotkey_match(
mod tests {
use super::*;

#[test]
fn builds_hotkeys_correctly() {
let test_cases = vec![
fn from_string_test_cases() -> Vec<(String, Hotkey)> {
vec![
(
"shift+r+meta".to_string(),
Hotkey {
Expand Down Expand Up @@ -184,11 +192,25 @@ mod tests {
keys: vec!["k".into()],
},
),
];
]
}

for (input, expected) in test_cases {
#[test]
fn hotkey_constructor() {
for (input, expected) in from_string_test_cases() {
let hotkey = Hotkey::new(&input);
assert_eq!(hotkey, expected);

let hotkey: Hotkey = input.parse().unwrap();
assert_eq!(hotkey, expected);
}
}

#[test]
fn hotkey_from_string() {
for (input, expected) in from_string_test_cases() {
let hotkey: Hotkey = input.parse().unwrap();
assert_eq!(hotkey, expected);
}
}
}

0 comments on commit a0edec7

Please sign in to comment.