forked from ucan-wg/rs-ucan
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
Test delegation store #12
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,7 @@ | ||
# Seeds for failure cases proptest has generated in the past. It is | ||
# automatically read and these particular cases re-run before any | ||
# novel cases are generated. | ||
# | ||
# It is recommended to check this file in to source control so that | ||
# everyone who runs the test benefits from these saved cases. | ||
cc 6496f8ae07f0fa0d57c9bc4d581551bc9940c50fe830880006156471a72e806b # shrinks to data = Newtype(null), more = [ArrayIndex(0)] |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -46,9 +46,9 @@ impl<T> Select<T> { | |
|
||
impl<T: Selectable> Select<T> { | ||
pub fn get(self, ctx: &Ipld) -> Result<T, SelectorError> { | ||
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. The |
||
self.filters | ||
.iter() | ||
.try_fold((ctx.clone(), vec![]), |(ipld, mut seen_ops), op| { | ||
let got = self.filters.iter().try_fold( | ||
(ctx.clone(), vec![], false), | ||
|(ipld, mut seen_ops, is_try), op| { | ||
seen_ops.push(op); | ||
|
||
match op { | ||
|
@@ -57,70 +57,95 @@ impl<T: Selectable> Select<T> { | |
let ipld: Ipld = | ||
Select::<Ipld>::new(vec![op]).get(ctx).unwrap_or(Ipld::Null); | ||
|
||
Ok((ipld, seen_ops)) | ||
Ok((ipld, seen_ops.clone(), true)) | ||
} | ||
Filter::ArrayIndex(i) => { | ||
let result = { | ||
match ipld { | ||
Ipld::List(xs) => { | ||
if i.abs() as usize > xs.len() { | ||
return Err(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::IndexOutOfBounds, | ||
return Err(( | ||
is_try, | ||
SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::IndexOutOfBounds, | ||
), | ||
)); | ||
}; | ||
|
||
xs.get((xs.len() as i32 + *i) as usize) | ||
.ok_or(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::IndexOutOfBounds, | ||
.ok_or(( | ||
is_try, | ||
SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::IndexOutOfBounds, | ||
), | ||
)) | ||
.cloned() | ||
} | ||
// FIXME behaviour on maps? type error | ||
_ => Err(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::NotAList, | ||
_ => Err(( | ||
is_try, | ||
SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::NotAList, | ||
), | ||
)), | ||
} | ||
}; | ||
|
||
Ok((result?, seen_ops)) | ||
Ok((result?, seen_ops.clone(), is_try)) | ||
} | ||
Filter::Field(k) => { | ||
let result = match ipld { | ||
Ipld::Map(xs) => xs | ||
.get(k) | ||
.ok_or(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::KeyNotFound, | ||
.ok_or(( | ||
is_try, | ||
SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::KeyNotFound, | ||
), | ||
)) | ||
.cloned(), | ||
_ => Err(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::NotAMap, | ||
_ => Err(( | ||
is_try, | ||
SelectorError::from_refs(&seen_ops, SelectorErrorReason::NotAMap), | ||
)), | ||
}; | ||
|
||
Ok((result?, seen_ops)) | ||
Ok((result?, seen_ops.clone(), is_try)) | ||
} | ||
Filter::Values => { | ||
let result = match ipld { | ||
Ipld::List(xs) => Ok(Ipld::List(xs)), | ||
Ipld::Map(xs) => Ok(Ipld::List(xs.values().cloned().collect())), | ||
_ => Err(SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::NotACollection, | ||
_ => Err(( | ||
is_try, | ||
SelectorError::from_refs( | ||
&seen_ops, | ||
SelectorErrorReason::NotACollection, | ||
), | ||
)), | ||
}; | ||
|
||
Ok((result?, seen_ops)) | ||
Ok((result?, seen_ops.clone(), is_try)) | ||
} | ||
} | ||
}) | ||
.and_then(|(ipld, ref path)| { | ||
T::try_select(ipld).map_err(|e| SelectorError::from_refs(path, e)) | ||
}) | ||
}, | ||
); | ||
|
||
let (ipld, path) = match got { | ||
Ok((ipld, seen_ops, _)) => Ok((ipld, seen_ops)), | ||
Err((is_try, ref e @ SelectorError { ref selector, .. })) => { | ||
if is_try { | ||
Ok((Ipld::Null, selector.0.iter().map(|x| x).collect::<Vec<_>>())) | ||
} else { | ||
Err(e.clone()) | ||
} | ||
} | ||
}?; | ||
|
||
T::try_select(ipld).map_err(|e| SelectorError::from_refs(&path, e)) | ||
} | ||
} | ||
|
||
|
@@ -166,3 +191,70 @@ impl<T: 'static> Arbitrary for Select<T> { | |
.boxed() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::ipld; | ||
use pretty_assertions as pretty; | ||
use proptest::prelude::*; | ||
use testresult::TestResult; | ||
|
||
mod get { | ||
use super::*; | ||
|
||
fn nested_data() -> Ipld { | ||
Ipld::Map( | ||
vec![ | ||
("name".to_string(), Ipld::String("Alice".to_string())), | ||
("age".to_string(), Ipld::Integer(42)), | ||
( | ||
"friends".to_string(), | ||
Ipld::List(vec![ | ||
Ipld::String("Bob".to_string()), | ||
Ipld::String("Charlie".to_string()), | ||
]), | ||
), | ||
] | ||
.into_iter() | ||
.collect(), | ||
) | ||
} | ||
|
||
proptest! { | ||
#[test_log::test] | ||
fn test_identity(data: ipld::Newtype) { | ||
let selector = Select::<ipld::Newtype>::from_str(".")?; | ||
prop_assert_eq!(selector.get(&data.0)?, data); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_try_missing_is_null(data: ipld::Newtype) { | ||
let selector = Select::<Ipld>::from_str(".foo?")?; | ||
let cleaned_data = match data.0.clone() { | ||
Ipld::Map(mut m) => { | ||
m.remove("foo").map_or(Ipld::Null, |v| v) | ||
} | ||
ipld => ipld | ||
}; | ||
prop_assert_eq!(selector.get(&cleaned_data)?, Ipld::Null); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_try_missing_plus_trailing_is_null(data: ipld::Newtype, more: Vec<Filter>) { | ||
let mut filters = vec![Filter::Try(Box::new(Filter::Field("foo".into())))]; | ||
filters.append(&mut more.clone()); | ||
|
||
let selector: Select<Ipld> = Select::new(filters); | ||
|
||
let cleaned_data = match data.0.clone() { | ||
Ipld::Map(mut m) => { | ||
m.remove("foo").map_or(Ipld::Null, |v| v) | ||
} | ||
ipld => ipld | ||
}; | ||
prop_assert_eq!(selector.get(&cleaned_data)?, Ipld::Null); | ||
} | ||
} | ||
} | ||
} |
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.
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.
The change in the above file using
to_ipld_envelope
was way more subtle than it should have been. basically previously the payload types individually implementedlibipld::Encode<C>
, but that was (evidently) confusing. It took longer than I'd like to admit to find the bug it caused.