Skip to content

Commit fabf52b

Browse files
committed
Unit-test input event deserialization
1 parent 9a264f3 commit fabf52b

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lighthouse-protocol/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ rand = "0.8"
1414
rmpv = { version = "1.0.1", features = ["with-serde"] }
1515
serde = { version = "1.0", features = ["derive"] }
1616
serde_with = "3.4"
17+
18+
[dev-dependencies]
19+
serde_json = "1.0"

lighthouse-protocol/src/input/input_event.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,89 @@ pub enum InputEvent {
1010
Mouse(MouseEvent),
1111
Gamepad(GamepadEvent),
1212
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use serde_json::json;
17+
18+
use crate::{EventSource, GamepadAxisEvent, GamepadButtonEvent, GamepadControlEvent, GamepadEvent, InputEvent, KeyEvent, MouseButton, MouseEvent, Pos};
19+
20+
#[test]
21+
fn key_event() {
22+
assert_eq!(
23+
serde_json::from_value::<InputEvent>(json!({
24+
"type": "key",
25+
"source": 0,
26+
"down": true,
27+
"key": "ArrowUp",
28+
})).unwrap(),
29+
InputEvent::Key(KeyEvent {
30+
source: EventSource::Int(0),
31+
down: true,
32+
key: "ArrowUp".into(),
33+
})
34+
);
35+
}
36+
37+
#[test]
38+
fn mouse_event() {
39+
assert_eq!(
40+
serde_json::from_value::<InputEvent>(json!({
41+
"type": "mouse",
42+
"source": 1,
43+
"button": "left",
44+
"pos": {
45+
"x": 2,
46+
"y": 4,
47+
},
48+
})).unwrap(),
49+
InputEvent::Mouse(MouseEvent {
50+
source: EventSource::Int(1),
51+
button: MouseButton::Left,
52+
pos: Pos::new(2.0, 4.0),
53+
})
54+
);
55+
}
56+
57+
#[test]
58+
fn gamepad_button_event() {
59+
assert_eq!(
60+
serde_json::from_value::<InputEvent>(json!({
61+
"type": "gamepad",
62+
"source": 1,
63+
"control": "button",
64+
"index": 42,
65+
"down": true,
66+
"value": 0.25,
67+
})).unwrap(),
68+
InputEvent::Gamepad(GamepadEvent {
69+
source: EventSource::Int(1),
70+
control: GamepadControlEvent::Button(GamepadButtonEvent {
71+
index: 42,
72+
down: true,
73+
value: 0.25,
74+
}),
75+
})
76+
);
77+
}
78+
79+
#[test]
80+
fn gamepad_axis_event() {
81+
assert_eq!(
82+
serde_json::from_value::<InputEvent>(json!({
83+
"type": "gamepad",
84+
"source": 1,
85+
"control": "axis",
86+
"index": 42,
87+
"value": 0.25,
88+
})).unwrap(),
89+
InputEvent::Gamepad(GamepadEvent {
90+
source: EventSource::Int(1),
91+
control: GamepadControlEvent::Axis(GamepadAxisEvent {
92+
index: 42,
93+
value: 0.25,
94+
}),
95+
})
96+
);
97+
}
98+
}

0 commit comments

Comments
 (0)