-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add tests for spdx "relationshipType": "PACKAGE_OF" #1186
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,90 @@ | ||||||||||||||||||||||||||||||||||||||||
use serde_json::Value; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
pub trait ContainsSubset { | ||||||||||||||||||||||||||||||||||||||||
// Returns true if the value is a subset of the receiver. | ||||||||||||||||||||||||||||||||||||||||
fn contains_subset(&self, value: Value) -> bool; | ||||||||||||||||||||||||||||||||||||||||
// Returns true if the value a deep subset of the receiver. | ||||||||||||||||||||||||||||||||||||||||
fn contains_deep_subset(&self, value: Value) -> bool; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
impl ContainsSubset for Value { | ||||||||||||||||||||||||||||||||||||||||
fn contains_subset(&self, value: Value) -> bool { | ||||||||||||||||||||||||||||||||||||||||
match (self, &value) { | ||||||||||||||||||||||||||||||||||||||||
(Value::Object(src), Value::Object(subset)) => subset | ||||||||||||||||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||||||||||||||||
.all(|(k, v)| src.get(k).is_some_and(|x| x == v)), | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
(Value::Array(src), Value::Array(subset)) => { | ||||||||||||||||||||||||||||||||||||||||
subset.iter().all(|v| src.iter().any(|x| x == v)) | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so that: let actual = json!([{"a":1}]);
actual.contains_subset(json!([{"a":1, "b":2}])); // should return false if we used contains_deep_subset then it would return true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect the absence of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confirmed: let actual = json!([{"a":1}]);
assert!(!actual.contains_subset(json!([{"a":1, "b":2}])));
assert!(!actual.contains_deep_subset(json!([{"a":1, "b":2}]))); Maybe add that to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works as I'd expect, too: let actual = json!([{"a":1, "b": 2}]);
assert!(!actual.contains_subset(json!([{"a":1}])));
assert!(actual.contains_deep_subset(json!([{"a":1}]))); Maybe |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
_ => value == *self, | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
fn contains_deep_subset(&self, subset: Value) -> bool { | ||||||||||||||||||||||||||||||||||||||||
match (self, &subset) { | ||||||||||||||||||||||||||||||||||||||||
(Value::Object(src), Value::Object(tgt)) => tgt.iter().all(|(k, v)| { | ||||||||||||||||||||||||||||||||||||||||
src.get(k) | ||||||||||||||||||||||||||||||||||||||||
.is_some_and(|x| x.contains_deep_subset(v.clone())) | ||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
(Value::Array(src), Value::Array(subset)) => subset | ||||||||||||||||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||||||||||||||||
.all(|v| src.iter().any(|x| x.contains_deep_subset(v.clone()))), | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
_ => subset == *self, | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
#[cfg(test)] | ||||||||||||||||||||||||||||||||||||||||
mod test { | ||||||||||||||||||||||||||||||||||||||||
use crate::subset::ContainsSubset; | ||||||||||||||||||||||||||||||||||||||||
use serde_json::json; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||
fn test_is_subset() { | ||||||||||||||||||||||||||||||||||||||||
// actual can have additional fields | ||||||||||||||||||||||||||||||||||||||||
let actual = json!({ | ||||||||||||||||||||||||||||||||||||||||
"relationship": "PackageOf", | ||||||||||||||||||||||||||||||||||||||||
"other": "test", | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
assert!(actual.contains_subset(json!({ | ||||||||||||||||||||||||||||||||||||||||
"relationship": "PackageOf", | ||||||||||||||||||||||||||||||||||||||||
}))); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// case where an expected field does not match | ||||||||||||||||||||||||||||||||||||||||
let actual = json!({ | ||||||||||||||||||||||||||||||||||||||||
"relationship": "PackageOf", | ||||||||||||||||||||||||||||||||||||||||
"other": "test", | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
assert!(!actual.contains_subset(json!({ | ||||||||||||||||||||||||||||||||||||||||
"relationship": "bad", | ||||||||||||||||||||||||||||||||||||||||
}))); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// case where an expected field is missing | ||||||||||||||||||||||||||||||||||||||||
let actual = json!({ | ||||||||||||||||||||||||||||||||||||||||
"relationship": "PackageOf", | ||||||||||||||||||||||||||||||||||||||||
"other": "test", | ||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||
assert!(!actual.contains_subset(json!({ | ||||||||||||||||||||||||||||||||||||||||
"name": "SATELLITE-6.15-RHEL-8", | ||||||||||||||||||||||||||||||||||||||||
}))); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||||||||||||||
fn test_array_subset() { | ||||||||||||||||||||||||||||||||||||||||
// actual can have additional fields | ||||||||||||||||||||||||||||||||||||||||
let actual = json!([1, 2, 3]); | ||||||||||||||||||||||||||||||||||||||||
assert!(actual.contains_subset(json!([2]))); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// other values can be interleaved. | ||||||||||||||||||||||||||||||||||||||||
let actual = json!([1, 2, 3]); | ||||||||||||||||||||||||||||||||||||||||
assert!(actual.contains_subset(json!([1, 3]))); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// case where a value is missing | ||||||||||||||||||||||||||||||||||||||||
let actual = json!([1, 2, 3]); | ||||||||||||||||||||||||||||||||||||||||
assert!(!actual.contains_subset(json!([0]))); | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+78
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe "strict" or "fuzzy" or "partial" are better descriptors than "deep". The crucial idea is that we need both a way to test for an explicit match and a way to ask "are all of these in the other one?" I think this test is more accurate thusly:
Suggested change
But again, I'm not sure "deep" is the right word. We should always recurse through a recursive structure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I'd be happy with any of those options. |
||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really wish serde's impl of
:#?
usedto_string_pretty
. I can remember the former, but never the latter. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100%