Skip to content

Commit 9cb2b44

Browse files
committed
rename --as-test to --as-config as it's more accurate, added unit tests, fixed nested array search
1 parent ee53f45 commit 9cb2b44

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

dsc/src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub enum ConfigSubCommand {
105105
#[clap(long, hide = true)]
106106
as_get: bool,
107107
#[clap(long, hide = true)]
108-
as_test: bool,
108+
as_config: bool,
109109
},
110110
#[clap(name = "validate", about = "Validate the current configuration", hide = true)]
111111
Validate {

dsc/src/subcommand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ pub fn config_set(configurator: &mut Configurator, format: &Option<OutputFormat>
104104
}
105105
}
106106

107-
pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool, as_get: &bool, as_test: &bool)
107+
pub fn config_test(configurator: &mut Configurator, format: &Option<OutputFormat>, as_group: &bool, as_get: &bool, as_config: &bool)
108108
{
109109
match configurator.invoke_test() {
110110
Ok(result) => {
111111
if *as_group {
112-
let json = if *as_test {
112+
let json = if *as_config {
113113
let mut result_configuration = Configuration::new();
114114
result_configuration.resources = Vec::new();
115115
for test_result in result.results {
@@ -341,8 +341,8 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option<String>, mounte
341341
ConfigSubCommand::Set { format, .. } => {
342342
config_set(&mut configurator, format, as_group);
343343
},
344-
ConfigSubCommand::Test { format, as_get, as_test, .. } => {
345-
config_test(&mut configurator, format, as_group, as_get, as_test);
344+
ConfigSubCommand::Test { format, as_get, as_config, .. } => {
345+
config_test(&mut configurator, format, as_group, as_get, as_config);
346346
},
347347
ConfigSubCommand::Validate { document, path, format} => {
348348
let mut result = ValidateResult {

dsc_lib/src/dscresources/dscresource.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,74 @@ fn array_contains(array: &Vec<Value>, find: &Value) -> bool {
467467
}
468468
}
469469

470-
if find.is_array() && item.is_array() && array_contains(item.as_array().unwrap(), find) {
470+
if find.is_array() && item.is_array() && is_same_array(item.as_array().unwrap(), find.as_array().unwrap()) {
471471
return true;
472472
}
473473
}
474474

475475
false
476476
}
477+
478+
#[test]
479+
fn same_array() {
480+
use serde_json::json;
481+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(null)];
482+
let array_two = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(null)];
483+
assert_eq!(is_same_array(&array_one, &array_two), true);
484+
}
485+
486+
#[test]
487+
fn same_array_out_of_order() {
488+
use serde_json::json;
489+
let array_one = vec![json!("a"), json!(true), json!(r#"{"a":"b"}"#)];
490+
let array_two = vec![json!(r#"{"a":"b"}"#), json!("a"), json!(true)];
491+
assert_eq!(is_same_array(&array_one, &array_two), true);
492+
}
493+
494+
#[test]
495+
fn different_array() {
496+
use serde_json::json;
497+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#)];
498+
let array_two = vec![json!(r#"{"a":"b"}"#), json!("a"), json!(2)];
499+
assert_eq!(is_same_array(&array_one, &array_two), false);
500+
}
501+
502+
#[test]
503+
fn different_array_sizes() {
504+
use serde_json::json;
505+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#)];
506+
let array_two = vec![json!(r#"{"a":"b"}"#), json!("a")];
507+
assert_eq!(is_same_array(&array_one, &array_two), false);
508+
}
509+
510+
#[test]
511+
fn array_with_multiple_objects_with_superset() {
512+
use serde_json::json;
513+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(r#"{"c":"d"}"#)];
514+
let array_two = vec![json!("a"), json!(1), json!(r#"{"a":"b", "c":"d"}"#), json!(r#"{"c":"d"}"#)];
515+
assert_eq!(is_same_array(&array_one, &array_two), false);
516+
}
517+
518+
#[test]
519+
fn array_with_duplicates_out_of_order() {
520+
use serde_json::json;
521+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(r#"{"a":"b"}"#)];
522+
let array_two = vec![json!(r#"{"a":"b"}"#), json!("a"), json!(1), json!(r#"{"a":"b"}"#)];
523+
assert_eq!(is_same_array(&array_one, &array_two), true);
524+
}
525+
526+
#[test]
527+
fn same_array_with_nested_array() {
528+
use serde_json::json;
529+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(vec![json!("a"), json!(1)])];
530+
let array_two = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(vec![json!("a"), json!(1)])];
531+
assert_eq!(is_same_array(&array_one, &array_two), true);
532+
}
533+
534+
#[test]
535+
fn different_array_with_nested_array() {
536+
use serde_json::json;
537+
let array_one = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(vec![json!("a"), json!(1)])];
538+
let array_two = vec![json!("a"), json!(1), json!(r#"{"a":"b"}"#), json!(vec![json!("a"), json!(2)])];
539+
assert_eq!(is_same_array(&array_one, &array_two), false);
540+
}

0 commit comments

Comments
 (0)