#[cfg(test)]
#[macro_use]
extern crate mockall;
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use serde_json::Value;
use new_home_application::application::commands::help_command::HelpCommand;
use new_home_application::method::method_callable::*;
use new_home_application::method::method_manager::*;
use new_home_application::method::method_structure::*;
mock! {
TestMethodManager {}
trait MethodManager {
fn add_method(&mut self, method: Method, callable: Box<dyn MethodCallable>) -> ();
fn get_method(&self, name: &String) -> Option<Method>;
fn get_method_callback(&self, name: &String) -> Option<&'static Box<dyn MethodCallable>>;
fn get_methods(&self) -> HashMap<String, Method>;
}
}
#[test]
fn test_help_command_self_register() {
let mut method_manager = MockTestMethodManager::new();
method_manager.expect_get_methods()
.times(1)
.returning(|| HashMap::new());
method_manager.expect_add_method()
.times(1)
.returning(|method: Method, _callable| {
assert_eq!(method.arguments.len(), 1);
assert_eq!(method.name, "help");
assert!(method.description.len() > 0);
assert!(method.help.len() > 0);
});
let mut manager_box: Box<dyn MethodManager> = Box::new(method_manager);
HelpCommand::self_register(&mut manager_box);
}
#[test]
fn test_help_command_call_no_arguments() {
let mut methods = HashMap::new();
methods.insert(String::from("info"), Method {
name: String::from("info"),
description: String::from("shows application info"),
help: String::new(),
arguments: vec![],
});
let command = HelpCommand::new(methods);
let arguments = HashMap::new();
let res = command.call(MethodArguments::new(arguments));
assert_eq!(res.code, 0);
assert_eq!(
res.message.unwrap()
.as_array().unwrap()
.get(1).unwrap()
.as_object().unwrap()
.get("list").unwrap()
.as_array().unwrap()
.get(0).unwrap()
.as_str().unwrap(),
"info"
)
}
#[test]
fn test_help_command_call_invalid_method_argument_type() {
let methods = HashMap::new();
let command = HelpCommand::new(methods);
let mut arguments = HashMap::new();
arguments.insert(String::from("method"), Value::from(0777));
let res = command.call(MethodArguments::new(arguments));
assert_eq!(res.code, 1);
assert_eq!(
res.message.unwrap()
.as_array().unwrap()
.get(0).unwrap()
.as_object().unwrap()
.get("error").unwrap()
.as_str().unwrap(),
"Argument 'method' has to be of type String"
)
}
#[test]
fn test_help_command_call_invalid_method() {
let methods = HashMap::new();
let command = HelpCommand::new(methods);
let mut arguments = HashMap::new();
arguments.insert(String::from("method"), Value::from("invalid"));
let res = command.call(MethodArguments::new(arguments));
assert_eq!(res.code, 1);
assert_eq!(
res.message.unwrap()
.as_array().unwrap()
.get(0).unwrap()
.as_object().unwrap()
.get("error").unwrap()
.as_str().unwrap(),
"Method not found"
)
}
#[test]
fn test_help_command_call_valid_method() {
let mut methods = HashMap::new();
methods.insert(String::from("info"), Method {
name: String::from("info"),
description: String::from("shows application info"),
help: String::new(),
arguments: vec![
MethodArgument {
name: String::from("test_argument"),
description: String::from("test_description"),
help: String::from("test_argument_description"),
value: Some(Value::from("string")),
required: false,
}
],
});
let command = HelpCommand::new(methods);
let mut arguments = HashMap::new();
arguments.insert(String::from("method"), Value::from("info"));
let res = command.call(MethodArguments::new(arguments));
let method_data = res.message.as_ref().unwrap()
.as_array().unwrap()
.get(0).unwrap()
.as_object().unwrap()
.get("table").unwrap()
.as_object().unwrap();
let test_argument = method_data.get("arguments").unwrap()
.as_array().unwrap()
.get(0).unwrap()
.as_object().unwrap();
assert_eq!(res.code, 1);
assert_eq!(method_data.get("name").unwrap(), "info");
assert_eq!(method_data.get("description").unwrap(), "shows application info");
assert_eq!(test_argument.get("name").unwrap().as_str().unwrap(), "test_argument");
assert_eq!(test_argument.get("description").unwrap().as_str().unwrap(), "test_description");
assert_eq!(test_argument.get("help").unwrap().as_str().unwrap(), "test_argument_description");
assert_eq!(test_argument.get("value").unwrap().as_str().unwrap(), "string");
assert_eq!(test_argument.get("required").unwrap().as_bool().unwrap(), false);
}
}