From b76fb99322085747c243e579eec55e3be57fe41c Mon Sep 17 00:00:00 2001 From: "Matheus T. dos Santos" Date: Fri, 31 May 2024 16:49:55 -0300 Subject: [PATCH] fix: fix plugin's unit tests --- plugins/test.lua | 2 + src/plugin.rs | 99 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 74 insertions(+), 27 deletions(-) diff --git a/plugins/test.lua b/plugins/test.lua index 6b8b1f6..bb1c8f4 100644 --- a/plugins/test.lua +++ b/plugins/test.lua @@ -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 diff --git a/src/plugin.rs b/src/plugin.rs index ba74610..c1667d9 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -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> { @@ -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), }, @@ -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()), @@ -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]), @@ -603,7 +615,7 @@ 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(); @@ -611,15 +623,24 @@ mod tests { 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(), @@ -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 @@ -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()), @@ -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]), }, ), (