Skip to content

Add equals() function #770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions dsc_lib/src/functions/equals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::DscError;
use crate::configure::context::Context;
use crate::functions::AcceptedArgKind;
use super::Function;
use serde_json::Value;

#[derive(Debug, Default)]
pub struct Equals {}

impl Function for Equals {
fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> {
vec![AcceptedArgKind::Number, AcceptedArgKind::String, AcceptedArgKind::Array, AcceptedArgKind::Object]
}

fn min_args(&self) -> usize {
2
}

fn max_args(&self) -> usize {
2
}

fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
Ok(Value::Bool(args[0] == args[1]))
}
}

#[cfg(test)]
mod tests {
use crate::configure::context::Context;
use crate::parser::Statement;
use serde_json::Value;

#[test]
fn int_equal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals(1,1)]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(true));
}

#[test]
fn int_notequal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals(1,2]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(false));
}

#[test]
fn string_equal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals('test','test')]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(true));
}

#[test]
fn string_notequal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals('test','TEST')]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(false));
}

#[test]
fn different_types() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals(1,'string')]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(false));
}

// TODO: Add tests for arrays once `createArray()` is implemented
// TODO: Add tests for objects once `createObject()` is implemented
Comment on lines +72 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, was going to ask based on the docs.

}
2 changes: 2 additions & 0 deletions dsc_lib/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod concat;
pub mod create_array;
pub mod div;
pub mod envvar;
pub mod equals;
pub mod int;
pub mod max;
pub mod min;
Expand Down Expand Up @@ -73,6 +74,7 @@ impl FunctionDispatcher {
functions.insert("createArray".to_string(), Box::new(create_array::CreateArray{}));
functions.insert("div".to_string(), Box::new(div::Div{}));
functions.insert("envvar".to_string(), Box::new(envvar::Envvar{}));
functions.insert("equals".to_string(), Box::new(equals::Equals{}));
functions.insert("int".to_string(), Box::new(int::Int{}));
functions.insert("max".to_string(), Box::new(max::Max{}));
functions.insert("min".to_string(), Box::new(min::Min{}));
Expand Down
Loading