From 8beec0d6e65e9837328a9752551691c940ad3923 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 19 Mar 2020 12:08:38 +0100 Subject: [PATCH] api-macro: tests for optional non-Option parameters Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/tests/options.rs | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 proxmox-api-macro/tests/options.rs diff --git a/proxmox-api-macro/tests/options.rs b/proxmox-api-macro/tests/options.rs new file mode 100644 index 00000000..256326c9 --- /dev/null +++ b/proxmox-api-macro/tests/options.rs @@ -0,0 +1,68 @@ +use proxmox_api_macro::api; + +use failure::Error; +use serde_json::{json, Value}; + +#[api( + input: { + properties: { + value: { + description: "The optional value with default.", + optional: true, + default: false, + } + } + } +)] +/// Print the given message. +/// +/// Returns: the input. +pub fn test_option(value: bool) -> Result { + Ok(value) +} + +struct RpcEnv; +impl proxmox::api::RpcEnvironment for RpcEnv { + fn set_result_attrib(&mut self, name: &str, value: Value) { + let _ = (name, value); + panic!("set_result_attrib called"); + } + + /// Query additional result data. + fn get_result_attrib(&self, name: &str) -> Option<&Value> { + let _ = name; + panic!("get_result_attrib called"); + } + + /// The environment type + fn env_type(&self) -> proxmox::api::RpcEnvironmentType { + panic!("env_type called"); + } + + /// Set user name + fn set_user(&mut self, user: Option) { + let _ = user; + panic!("set_user called"); + } + + /// Get user name + fn get_user(&self) -> Option { + panic!("get_user called"); + } +} + +#[test] +fn test_invocations() { + let mut env = RpcEnv; + let value = api_function_test_option(json!({}), &API_METHOD_TEST_OPTION, &mut env) + .expect("func with option should work"); + assert_eq!(value, false); + + let value = api_function_test_option(json!({"value": true}), &API_METHOD_TEST_OPTION, &mut env) + .expect("func with option should work"); + assert_eq!(value, true); + + let value = api_function_test_option(json!({"value": false}), &API_METHOD_TEST_OPTION, &mut env) + .expect("func with option should work"); + assert_eq!(value, false); +}