Skip to content

Commit 02c6260

Browse files
committed
Change array and index_of tests
1 parent 5e4344d commit 02c6260

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

dsc/tests/dsc_functions.tests.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,10 @@ Describe 'tests for function expressions' {
342342
}
343343

344344
It 'array function works for: <expression>' -TestCases @(
345-
@{ expression = "[array('hello', 42)]"; expected = @('hello', 42) }
346-
@{ expression = "[array('a', 'b', 'c')]"; expected = @('a', 'b', 'c') }
347-
@{ expression = "[array(1, 2, 3)]"; expected = @(1, 2, 3) }
348-
@{ expression = "[array('string', 123, createObject('key', 'value'))]"; expected = @('string', 123, [pscustomobject]@{ key = 'value' }) }
349-
@{ expression = "[array(createArray('a', 'b'), 'string')]"; expected = @(@('a', 'b'), 'string') }
345+
@{ expression = "[array('hello')]"; expected = @('hello') }
346+
@{ expression = "[array(42)]"; expected = @(42) }
347+
@{ expression = "[array(createObject('key', 'value'))]"; expected = @([pscustomobject]@{ key = 'value' }) }
348+
@{ expression = "[array(createArray('a', 'b'))]"; expected = @(@('a', 'b')) }
350349
) {
351350
param($expression, $expected)
352351

@@ -408,4 +407,4 @@ Describe 'tests for function expressions' {
408407
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
409408
($out.results[0].result.actualState.output | Out-String) | Should -BeExactly ($expected | Out-String)
410409
}
411-
}
410+
}

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ description = "Evaluates if all arguments are true"
218218
invoked = "and function"
219219

220220
[functions.array]
221-
description = "Creates an array from the given elements of mixed types"
221+
description = "Convert the value to an array"
222222
invoked = "array function"
223223
invalidArgType = "Invalid argument type, only int, string, array, or object are accepted"
224224

dsc_lib/src/functions/array.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,25 @@ impl Function for Array {
1818
description: t!("functions.array.description").to_string(),
1919
category: FunctionCategory::Array,
2020
min_args: 1,
21-
max_args: usize::MAX,
22-
accepted_arg_ordered_types: vec![],
23-
remaining_arg_accepted_types: Some(vec![
24-
FunctionArgKind::String,
25-
FunctionArgKind::Number,
26-
FunctionArgKind::Object,
27-
FunctionArgKind::Array,
28-
]),
21+
max_args: 1,
22+
accepted_arg_ordered_types: vec![
23+
vec![FunctionArgKind::String, FunctionArgKind::Number, FunctionArgKind::Object, FunctionArgKind::Array],
24+
],
25+
remaining_arg_accepted_types: None,
2926
return_types: vec![FunctionArgKind::Array],
3027
}
3128
}
3229

3330
fn invoke(&self, args: &[Value], _context: &Context) -> Result<Value, DscError> {
3431
debug!("{}", t!("functions.array.invoked"));
35-
let mut array_result = Vec::<Value>::new();
3632

37-
for value in args {
38-
// Only accept int, string, array, or object as specified
39-
if value.is_number() || value.is_string() || value.is_array() || value.is_object() {
40-
array_result.push(value.clone());
41-
} else {
42-
return Err(DscError::Parser(t!("functions.array.invalidArgType").to_string()));
43-
}
33+
let value = &args[0];
34+
35+
if value.is_number() || value.is_string() || value.is_array() || value.is_object() {
36+
Ok(Value::Array(vec![value.clone()]))
37+
} else {
38+
Err(DscError::Parser(t!("functions.array.invalidArgType").to_string()))
4439
}
45-
46-
Ok(Value::Array(array_result))
4740
}
4841
}
4942

@@ -53,31 +46,31 @@ mod tests {
5346
use crate::parser::Statement;
5447

5548
#[test]
56-
fn mixed_types() {
49+
fn single_string() {
5750
let mut parser = Statement::new().unwrap();
58-
let result = parser.parse_and_execute("[array('hello', 42)]", &Context::new()).unwrap();
59-
assert_eq!(result.to_string(), r#"["hello",42]"#);
51+
let result = parser.parse_and_execute("[array('hello')]", &Context::new()).unwrap();
52+
assert_eq!(result.to_string(), r#"["hello"]"#);
6053
}
6154

6255
#[test]
63-
fn strings_only() {
56+
fn single_number() {
6457
let mut parser = Statement::new().unwrap();
65-
let result = parser.parse_and_execute("[array('a', 'b', 'c')]", &Context::new()).unwrap();
66-
assert_eq!(result.to_string(), r#"["a","b","c"]"#);
58+
let result = parser.parse_and_execute("[array(42)]", &Context::new()).unwrap();
59+
assert_eq!(result.to_string(), "[42]");
6760
}
6861

6962
#[test]
70-
fn numbers_only() {
63+
fn single_object() {
7164
let mut parser = Statement::new().unwrap();
72-
let result = parser.parse_and_execute("[array(1, 2, 3)]", &Context::new()).unwrap();
73-
assert_eq!(result.to_string(), "[1,2,3]");
65+
let result = parser.parse_and_execute("[array(createObject('key', 'value'))]", &Context::new()).unwrap();
66+
assert_eq!(result.to_string(), r#"[{"key":"value"}]"#);
7467
}
7568

7669
#[test]
77-
fn arrays_and_objects() {
70+
fn single_array() {
7871
let mut parser = Statement::new().unwrap();
79-
let result = parser.parse_and_execute("[array(createArray('a','b'), createObject('key', 'value'))]", &Context::new()).unwrap();
80-
assert_eq!(result.to_string(), r#"[["a","b"],{"key":"value"}]"#);
72+
let result = parser.parse_and_execute("[array(createArray('a','b'))]", &Context::new()).unwrap();
73+
assert_eq!(result.to_string(), r#"[["a","b"]]"#);
8174
}
8275

8376
#[test]
@@ -87,6 +80,13 @@ mod tests {
8780
assert!(result.is_err());
8881
}
8982

83+
#[test]
84+
fn multiple_args_not_allowed() {
85+
let mut parser = Statement::new().unwrap();
86+
let result = parser.parse_and_execute("[array('hello', 42)]", &Context::new());
87+
assert!(result.is_err());
88+
}
89+
9090
#[test]
9191
fn invalid_type_boolean() {
9292
let mut parser = Statement::new().unwrap();

dsc_lib/src/functions/index_of.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,56 +60,56 @@ mod tests {
6060
fn find_string_in_array() {
6161
let mut parser = Statement::new().unwrap();
6262
let result = parser.parse_and_execute("[indexOf(createArray('apple', 'banana', 'cherry'), 'banana')]", &Context::new()).unwrap();
63-
assert_eq!(result.to_string(), "1");
63+
assert_eq!(result, 1);
6464
}
6565

6666
#[test]
6767
fn find_number_in_array() {
6868
let mut parser = Statement::new().unwrap();
6969
let result = parser.parse_and_execute("[indexOf(createArray(10, 20, 30), 20)]", &Context::new()).unwrap();
70-
assert_eq!(result.to_string(), "1");
70+
assert_eq!(result, 1);
7171
}
7272

7373
#[test]
7474
fn find_first_occurrence() {
7575
let mut parser = Statement::new().unwrap();
7676
let result = parser.parse_and_execute("[indexOf(createArray('a', 'b', 'a', 'c'), 'a')]", &Context::new()).unwrap();
77-
assert_eq!(result.to_string(), "0");
77+
assert_eq!(result, 0);
7878
}
7979

8080
#[test]
8181
fn item_not_found() {
8282
let mut parser = Statement::new().unwrap();
8383
let result = parser.parse_and_execute("[indexOf(createArray('apple', 'banana'), 'orange')]", &Context::new()).unwrap();
84-
assert_eq!(result.to_string(), "-1");
84+
assert_eq!(result, -1);
8585
}
8686

8787
#[test]
8888
fn case_sensitive_string() {
8989
let mut parser = Statement::new().unwrap();
9090
let result = parser.parse_and_execute("[indexOf(createArray('Apple', 'Banana'), 'apple')]", &Context::new()).unwrap();
91-
assert_eq!(result.to_string(), "-1");
91+
assert_eq!(result, -1);
9292
}
9393

9494
#[test]
9595
fn find_array_in_array() {
9696
let mut parser = Statement::new().unwrap();
9797
let result = parser.parse_and_execute("[indexOf(array(createArray('a', 'b'), createArray('c', 'd')), createArray('c', 'd'))]", &Context::new()).unwrap();
98-
assert_eq!(result.to_string(), "1");
98+
assert_eq!(result, 1);
9999
}
100100

101101
#[test]
102102
fn find_object_in_array() {
103103
let mut parser = Statement::new().unwrap();
104104
let result = parser.parse_and_execute("[indexOf(array(createObject('name', 'John'), createObject('name', 'Jane')), createObject('name', 'Jane'))]", &Context::new()).unwrap();
105-
assert_eq!(result.to_string(), "1");
105+
assert_eq!(result, 1);
106106
}
107107

108108
#[test]
109109
fn empty_array() {
110110
let mut parser = Statement::new().unwrap();
111111
let result = parser.parse_and_execute("[indexOf(createArray(), 'test')]", &Context::new()).unwrap();
112-
assert_eq!(result.to_string(), "-1");
112+
assert_eq!(result, -1);
113113
}
114114

115115
#[test]

0 commit comments

Comments
 (0)