-
Notifications
You must be signed in to change notification settings - Fork 56
Add new comparison operators #990
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3bf5eef
Add new comparison operators
Gijsreyn 953321f
Fix tests
Gijsreyn 326b0a8
Accidentially removed equals
Gijsreyn 1ad9355
Add new comparison operators
Gijsreyn 7620f90
Fix tests
Gijsreyn 21d5047
Accidentially removed equals
Gijsreyn e93b31a
Fix I18N test
Gijsreyn a8d3c74
Fix mistake and add debug
Gijsreyn e82aeff
Merge branch 'comparison-functions' of https://github.com/Gijsreyn/op…
Gijsreyn 5ee2a46
Address feedback
Gijsreyn 0b317f0
Add type mismatch test
Gijsreyn 3a54219
Merge branch 'main' into comparison-functions
Gijsreyn 3d681b7
Add additional tests
Gijsreyn 3d3b0fd
Merge branch 'comparison-functions' of https://github.com/Gijsreyn/op…
Gijsreyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{AcceptedArgKind, Function, FunctionCategory}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Greater {} | ||
|
|
||
| impl Function for Greater { | ||
| fn description(&self) -> String { | ||
| t!("functions.greater.description").to_string() | ||
| } | ||
|
|
||
| fn category(&self) -> FunctionCategory { | ||
| FunctionCategory::Comparison | ||
| } | ||
|
|
||
| fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
| vec![AcceptedArgKind::Number, AcceptedArgKind::String] | ||
| } | ||
|
|
||
| fn min_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn max_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| debug!("{}", t!("functions.greater.invoked")); | ||
|
|
||
| let first = &args[0]; | ||
| let second = &args[1]; | ||
|
|
||
| if let (Some(num1), Some(num2)) = (first.as_i64(), second.as_i64()) { | ||
| return Ok(Value::Bool(num1 > num2)); | ||
| } | ||
|
|
||
| if let (Some(str1), Some(str2)) = (first.as_str(), second.as_str()) { | ||
| return Ok(Value::Bool(str1 > str2)); | ||
| } | ||
|
|
||
| Err(DscError::Parser(t!("functions.typeMismatch").to_string())) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::configure::context::Context; | ||
| use crate::parser::Statement; | ||
|
|
||
| #[test] | ||
| fn number_greater() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greater(2,1)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_not_greater() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greater(1,2)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_equal() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greater(1,1)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn string_greater() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greater('b','a')]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn type_mismatch_string_number() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greater('5', 3)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| assert!(result.unwrap_err().to_string().contains("Arguments must be of the same type")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{AcceptedArgKind, Function, FunctionCategory}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct GreaterOrEquals {} | ||
|
|
||
| impl Function for GreaterOrEquals { | ||
| fn description(&self) -> String { | ||
| t!("functions.greaterOrEquals.description").to_string() | ||
| } | ||
|
|
||
| fn category(&self) -> FunctionCategory { | ||
| FunctionCategory::Comparison | ||
| } | ||
|
|
||
| fn min_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn max_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
| vec![AcceptedArgKind::Number, AcceptedArgKind::String] | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| debug!("{}", t!("functions.greaterOrEquals.invoked")); | ||
|
|
||
| let first = &args[0]; | ||
| let second = &args[1]; | ||
|
|
||
| if let (Some(num1), Some(num2)) = (first.as_i64(), second.as_i64()) { | ||
| return Ok(Value::Bool(num1 >= num2)); | ||
| } | ||
|
|
||
| if let (Some(str1), Some(str2)) = (first.as_str(), second.as_str()) { | ||
| return Ok(Value::Bool(str1 >= str2)); | ||
| } | ||
|
|
||
| Err(DscError::Parser(t!("functions.typeMismatch").to_string())) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::configure::context::Context; | ||
| use crate::parser::Statement; | ||
|
|
||
| #[test] | ||
| fn number_greater_or_equals() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greaterOrEquals(5,3)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_not_greater_or_equals() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greaterOrEquals(3,5)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_equal() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greaterOrEquals(5,5)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn string_greater_or_equals() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greaterOrEquals('b','a')]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn type_mismatch_string_number() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[greaterOrEquals('5', 3)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| assert!(result.unwrap_err().to_string().contains("Arguments must be of the same type")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::DscError; | ||
| use crate::configure::context::Context; | ||
| use crate::functions::{AcceptedArgKind, Function, FunctionCategory}; | ||
| use rust_i18n::t; | ||
| use serde_json::Value; | ||
| use tracing::debug; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct Less {} | ||
|
|
||
| impl Function for Less { | ||
| fn description(&self) -> String { | ||
| t!("functions.less.description").to_string() | ||
| } | ||
|
|
||
| fn category(&self) -> FunctionCategory { | ||
| FunctionCategory::Comparison | ||
| } | ||
|
|
||
| fn min_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn max_args(&self) -> usize { | ||
| 2 | ||
| } | ||
|
|
||
| fn accepted_arg_types(&self) -> Vec<AcceptedArgKind> { | ||
| vec![AcceptedArgKind::Number, AcceptedArgKind::String] | ||
| } | ||
|
|
||
| fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> { | ||
| debug!("{}", t!("functions.less.invoked")); | ||
|
|
||
| let first = &args[0]; | ||
| let second = &args[1]; | ||
|
|
||
| if let (Some(num1), Some(num2)) = (first.as_i64(), second.as_i64()) { | ||
| return Ok(Value::Bool(num1 < num2)); | ||
| } | ||
|
|
||
| if let (Some(str1), Some(str2)) = (first.as_str(), second.as_str()) { | ||
| return Ok(Value::Bool(str1 < str2)); | ||
| } | ||
|
|
||
| Err(DscError::Parser(t!("functions.typeMismatch").to_string())) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::configure::context::Context; | ||
| use crate::parser::Statement; | ||
|
|
||
| #[test] | ||
| fn number_less() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[less(3,5)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_not_less() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[less(5,3)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn number_equal() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[less(5,5)]", &Context::new()).unwrap(); | ||
| assert_eq!(result, false); | ||
| } | ||
|
|
||
| #[test] | ||
| fn string_less() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[less('a','b')]", &Context::new()).unwrap(); | ||
| assert_eq!(result, true); | ||
| } | ||
|
|
||
| fn type_mismatch_string_number() { | ||
| let mut parser = Statement::new().unwrap(); | ||
| let result = parser.parse_and_execute("[lessOrEquals('5', 3)]", &Context::new()); | ||
| assert!(result.is_err()); | ||
| assert!(result.unwrap_err().to_string().contains("Arguments must be of the same type")); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.