|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::DscError; |
| 5 | +use crate::configure::context::Context; |
| 6 | +use crate::functions::{AcceptedArgKind, Function, FunctionCategory}; |
| 7 | +use rust_i18n::t; |
| 8 | +use serde_json::Value; |
| 9 | +use tracing::debug; |
| 10 | + |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct GreaterOrEquals {} |
| 13 | + |
| 14 | +impl Function for GreaterOrEquals { |
| 15 | + fn description(&self) -> String { |
| 16 | + t!("functions.greaterOrEquals.description").to_string() |
| 17 | + } |
| 18 | + |
| 19 | + fn category(&self) -> FunctionCategory { |
| 20 | + FunctionCategory::Comparison |
| 21 | + } |
| 22 | + |
| 23 | + fn min_args(&self) -> usize { |
| 24 | + 2 |
| 25 | + } |
| 26 | + |
| 27 | + fn max_args(&self) -> usize { |
| 28 | + 2 |
| 29 | + } |
| 30 | + |
| 31 | + fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { |
| 32 | + vec![AcceptedArgKind::Number, AcceptedArgKind::Number] |
| 33 | + } |
| 34 | + |
| 35 | + fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { |
| 36 | + debug!("{}", t!("functions.greaterOrEquals.invoked")); |
| 37 | + |
| 38 | + let num1 = match &args[0] { |
| 39 | + Value::Number(n) => n.as_f64().ok_or_else(|| DscError::Parser(t!("functions.invalidArguments").to_string()))?, |
| 40 | + _ => return Err(DscError::Parser(t!("functions.invalidArguments").to_string())), |
| 41 | + }; |
| 42 | + |
| 43 | + let num2 = match &args[1] { |
| 44 | + Value::Number(n) => n.as_f64().ok_or_else(|| DscError::Parser(t!("functions.invalidArguments").to_string()))?, |
| 45 | + _ => return Err(DscError::Parser(t!("functions.invalidArguments").to_string())), |
| 46 | + }; |
| 47 | + |
| 48 | + Ok(Value::Bool(num1 >= num2)) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[cfg(test)] |
| 53 | +mod tests { |
| 54 | + use super::*; |
| 55 | + use crate::configure::context::Context; |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn test_greater_or_equals() { |
| 59 | + let greater_or_equals = GreaterOrEquals {}; |
| 60 | + let context = Context::new().unwrap(); |
| 61 | + |
| 62 | + let result = greater_or_equals.invoke(&[Value::Number(5.into()), Value::Number(3.into())], &context); |
| 63 | + assert!(result.is_ok()); |
| 64 | + assert_eq!(result.unwrap(), Value::Bool(true)); |
| 65 | + |
| 66 | + let result = greater_or_equals.invoke(&[Value::Number(3.into()), Value::Number(5.into())], &context); |
| 67 | + assert!(result.is_ok()); |
| 68 | + assert_eq!(result.unwrap(), Value::Bool(false)); |
| 69 | + |
| 70 | + let result = greater_or_equals.invoke(&[Value::Number(5.into()), Value::Number(5.into())], &context); |
| 71 | + assert!(result.is_ok()); |
| 72 | + assert_eq!(result.unwrap(), Value::Bool(true)); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_invalid_args() { |
| 77 | + let greater_or_equals = GreaterOrEquals {}; |
| 78 | + let context = Context::new().unwrap(); |
| 79 | + |
| 80 | + let result = greater_or_equals.invoke(&[Value::Number(5.into())], &context); |
| 81 | + assert!(result.is_err()); |
| 82 | + } |
| 83 | +} |
0 commit comments