Skip to content

Commit

Permalink
fix: fix plugin's unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuswhite committed May 31, 2024
1 parent 1922a57 commit b76fb99
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
2 changes: 2 additions & 0 deletions plugins/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ require "scope"

function serial_rx(msg)
scope.disconnect()
scope.sleep(500)
scope.connect('COM1', 115200)
scope.sleep(500)
scope.println('Sent ' .. bytes2str(msg))
scope.eprintln('Timeout')
end
Expand Down
99 changes: 72 additions & 27 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ mod tests {
use crate::plugin::{Plugin, PluginRequest};
use crate::plugin_installer::PluginInstaller;
use std::path::PathBuf;
use std::time::Duration;

#[test]
fn test_echo() -> Result<(), String> {
Expand Down Expand Up @@ -509,13 +510,16 @@ mod tests {
let serial_rx_call = plugin.serial_rx_call(msg.as_bytes().to_vec());
let expected = [
PluginRequest::Disconnect,
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Connect {
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
// PluginRequest::SerialTx {
// msg: msg.as_bytes().to_vec(),
// },
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Println {
msg: format!("Sent {}", msg),
},
Expand Down Expand Up @@ -553,6 +557,14 @@ mod tests {
];
let expected = vec![
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
),
(
PluginRequest::Connect {
port: Some("COM1".to_string()),
Expand All @@ -563,14 +575,14 @@ mod tests {
baud_rate: Some(115200),
},
),
// (
// PluginRequest::SerialTx {
// msg: msg[0].as_bytes().to_vec(),
// },
// PluginRequest::SerialTx {
// msg: msg[1].as_bytes().to_vec(),
// },
// ),
(
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
),
(
PluginRequest::Println {
msg: format!("Sent {}", msg[0]),
Expand Down Expand Up @@ -603,23 +615,32 @@ mod tests {
#[test]
fn test_user_command_iter() -> Result<(), String> {
PluginInstaller.post()?;
let arg_list = vec!["Hello", "World!"]
let arg_list = vec!["Hello"]
.into_iter()
.map(|arg| arg.to_string())
.collect();
let plugin = Plugin::new(PathBuf::from("plugins/test.lua"))?;
let user_command_call = plugin.user_command_call(arg_list);
let expected = [
PluginRequest::Disconnect,
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Connect {
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
// PluginRequest::SerialTx {
// msg: "Hello".as_bytes().to_vec(),
// },
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Println {
msg: "Sending Hello ...".to_string(),
},
PluginRequest::SerialTx {
msg: "Hello".as_bytes().to_vec(),
},
PluginRequest::Println {
msg: "Sent World!".to_string(),
msg: "Sent Hello".to_string(),
},
PluginRequest::Eprintln {
msg: "Timeout".to_string(),
Expand All @@ -645,7 +666,7 @@ mod tests {
#[test]
fn test_2_user_command_iter() -> Result<(), String> {
PluginInstaller.post()?;
let arg_list = [vec!["Hello", "World!"], vec!["Other", "Message"]]
let arg_list = [vec!["Hello"], vec!["Other"]]
.into_iter()
.map(|arg_list| {
arg_list
Expand All @@ -664,6 +685,14 @@ mod tests {
];
let expected = vec![
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
),
(
PluginRequest::Connect {
port: Some("COM1".to_string()),
Expand All @@ -674,20 +703,36 @@ mod tests {
baud_rate: Some(115200),
},
),
// (
// PluginRequest::SerialTx {
// msg: arg_list[0][0].as_bytes().to_vec(),
// },
// PluginRequest::SerialTx {
// msg: arg_list[1][0].as_bytes().to_vec(),
// },
// ),
(
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
PluginRequest::Sleep {
time: Duration::from_millis(500),
},
),
(
PluginRequest::Println {
msg: format!("Sending {} ...", arg_list[0][0]),
},
PluginRequest::Println {
msg: format!("Sending {} ...", arg_list[1][0]),
},
),
(
PluginRequest::SerialTx {
msg: arg_list[0][0].as_bytes().to_vec(),
},
PluginRequest::SerialTx {
msg: arg_list[1][0].as_bytes().to_vec(),
},
),
(
PluginRequest::Println {
msg: format!("Sent {}", arg_list[0][1]),
msg: format!("Sent {}", arg_list[0][0]),
},
PluginRequest::Println {
msg: format!("Sent {}", arg_list[1][1]),
msg: format!("Sent {}", arg_list[1][0]),
},
),
(
Expand Down

0 comments on commit b76fb99

Please sign in to comment.