refactor of testing code

This commit is contained in:
Michael 2023-02-19 01:40:12 +00:00
parent 2995f6e48b
commit ffa987e00a
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C

View File

@ -108,15 +108,15 @@ impl Sway {
let n = self.stream.read(&mut buffer)?; let n = self.stream.read(&mut buffer)?;
match serde_json::from_slice::<T>(&buffer[14..n]) { match serde_json::from_slice::<T>(&buffer[14..n]) {
Ok(result) => return Ok(result), Ok(result) => Ok(result),
Err(e) => return Err(e.into()), Err(e) => Err(e.into()),
} }
} }
fn parse_events(&mut self, events: Vec<&str>) -> Result<String> { fn parse_events(&mut self, events: Vec<&str>) -> Result<String> {
match serde_json::to_string(&events) { match serde_json::to_string(&events) {
Ok(json) => Ok(json), Ok(json) => Ok(json),
Err(e) => return Err(e.into()), Err(e) => Err(e.into()),
} }
} }
} }
@ -136,50 +136,31 @@ mod tests {
#[test] #[test]
fn run_command() { fn run_command() {
match sway_ipc().run_command("reload") { assert_eq!(sway_ipc().run_command("reload").unwrap()[0].success, true);
Ok(response) => assert_eq!(response.into_iter().nth(0).unwrap().success, true),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_workspaces() { fn get_workspaces() {
match sway_ipc().get_workspaces() { for workspace in sway_ipc().get_workspaces().unwrap() {
Ok(response) => {
for workspace in response {
assert_eq!(workspace._type, "workspace"); assert_eq!(workspace._type, "workspace");
} }
},
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn subscribe() { fn subscribe() {
match sway_ipc().subscribe(vec!(EVENT_WORKSPACE, EVENT_MODE)) { assert_eq!(sway_ipc().subscribe(vec!(EVENT_WORKSPACE, EVENT_MODE)).unwrap().success, true);
Ok(response) => assert_eq!(response.success, true),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_outputs() { fn get_outputs() {
match sway_ipc().get_outputs() { for output in sway_ipc().get_outputs().unwrap() {
Ok(response) => {
for output in response {
assert_eq!(output._type, "output"); assert_eq!(output._type, "output");
} }
},
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_tree() { fn get_tree() {
match sway_ipc().get_tree() { assert_eq!(sway_ipc().get_tree().unwrap().id, 1);
Ok(response) => assert_eq!(response.id, 1),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
@ -187,106 +168,68 @@ mod tests {
let mut sway = sway_ipc(); let mut sway = sway_ipc();
sway.run_command("unmark").unwrap(); sway.run_command("unmark").unwrap();
sway.run_command("mark test").unwrap(); sway.run_command("mark test").unwrap();
match sway.get_marks() { assert_eq!(sway.get_marks().unwrap()[0], "test");
Ok(response) => assert_eq!(response.into_iter().nth(0).unwrap(), "test"),
Err(e) => panic!("{}", e.to_string()),
}
sway.run_command("unmark").unwrap(); sway.run_command("unmark").unwrap();
} }
#[test] #[test]
fn get_bar_config() { fn get_bar_config() {
match sway_ipc().get_bar_config("bar-0") { assert_eq!(sway_ipc().get_bar_config("bar-0").unwrap().id, "bar-0");
Ok(response) => assert_eq!(response.id, "bar-0"),
Err(e) => panic!("{}", e.to_string())
}
} }
#[test] #[test]
fn get_version() { fn get_version() {
match sway_ipc().get_version() { assert_eq!(sway_ipc().get_version().unwrap().major, 1);
Ok(response) => assert_eq!(response.major, 1),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_binding_modes() { fn get_binding_modes() {
match sway_ipc().get_binding_modes() { assert_eq!(sway_ipc().get_binding_modes().unwrap()[0], "default");
Ok(response) => {
assert_eq!(response.into_iter().nth(0).unwrap(), "default")
},
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_config() { fn get_config() {
match sway_ipc().get_config() { sway_ipc().get_config().unwrap().config;
Ok(_) => {},
Err(e) => panic!("{}", e.to_string())
}
} }
#[test] #[test]
fn send_tick() { fn send_tick() {
match sway_ipc().send_tick("") { assert_eq!(sway_ipc().send_tick("").unwrap().success, true);
Ok(response) => assert_eq!(response.success, true),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn sync() { fn sync() {
match sway_ipc().sync() { assert_eq!(sway_ipc().sync().unwrap().success, false);
Ok(response) => assert_eq!(response.success, false),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_binding_state() { fn get_binding_state() {
match sway_ipc().get_binding_state() { assert_eq!(sway_ipc().get_binding_state().unwrap().name, "default");
Ok(response) => assert_eq!(response.name, "default"),
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_inputs() { fn get_inputs() {
match sway_ipc().get_inputs() { for input in sway_ipc().get_inputs().unwrap() {
Ok(response) => {
for input in response {
if !(input._type.eq("keyboard") | input._type.eq("pointer")) { if !(input._type.eq("keyboard") | input._type.eq("pointer")) {
panic!("invalid input: {}", input._type); panic!("invalid input: {}", input._type);
} }
} }
},
Err(e) => panic!("{}", e.to_string()),
}
} }
#[test] #[test]
fn get_seats() { fn get_seats() {
match sway_ipc().get_seats() { assert_eq!(sway_ipc().get_seats().unwrap()[0].name, "seat0");
Ok(response) => assert_eq!(response.into_iter().nth(0).unwrap().name, "seat0"),
Err(e) => panic!("{}", e.to_string())
}
} }
#[test] #[test]
fn construct_packet() { fn construct_packet() {
match I3msg::new("reload", IPC_RUN_COMMAND).construct_packet() { let packet = I3msg::new("reload", IPC_RUN_COMMAND).construct_packet().unwrap();
Ok(buffer) => { assert_eq!(packet, vec![
assert_eq!(buffer, vec![
0x69, 0x33, 0x2D, 0x69, 0x69, 0x33, 0x2D, 0x69,
0x70, 0x63, 0x06, 0x00, 0x70, 0x63, 0x06, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x72, 0x65, 0x00, 0x00, 0x72, 0x65,
0x6C, 0x6F, 0x61, 0x64, 0x6C, 0x6F, 0x61, 0x64,
]); ]);
},
Err(e) => panic!("{}", e.to_string()),
}
} }
} }