Skip to content
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: proptests for parsing array indices queries #142

Merged
merged 2 commits into from
May 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions crates/rsonpath-lib/tests/query_parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ mod transform_json_escape_sequences_tests {
mod proptests {
use super::*;
use proptest::prelude::*;
use rsonpath_lib::query::NonNegativeArrayIndex;

/* Approach: we generate a sequence of Selectors, each having its generated string
* and a tag describing what selector it represents, and, optionally, what string is attached.
Expand All @@ -217,6 +218,8 @@ mod proptests {
Child(String),
WildcardDescendant,
Descendant(String),
ArrayIndexChild(NonNegativeArrayIndex),
ArrayIndexDescendant(NonNegativeArrayIndex),
}

#[derive(Debug, Clone)]
Expand All @@ -232,6 +235,8 @@ mod proptests {
any_child(),
any_wildcard_descendant(),
any_descendant(),
any_array_index_child(),
any_array_index_descendant(),
]
}

Expand All @@ -253,25 +258,39 @@ mod proptests {

// .label or ['label']
fn any_child() -> impl Strategy<Value = Selector> {
prop_oneof![any_member().prop_map(|x| (format!(".{x}"), x)), any_index(),].prop_map(|(s, l)| Selector {
prop_oneof![any_member().prop_map(|x| (format!(".{x}"), x)), any_name(),].prop_map(|(s, l)| Selector {
string: s,
tag: SelectorTag::Child(l),
})
}

// ..label or ..['label']
fn any_descendant() -> impl Strategy<Value = Selector> {
prop_oneof![any_member().prop_map(|x| (x.clone(), x)), any_index(),].prop_map(|(x, l)| Selector {
prop_oneof![any_member().prop_map(|x| (x.clone(), x)), any_name(),].prop_map(|(x, l)| Selector {
string: format!("..{x}"),
tag: SelectorTag::Descendant(l),
})
}

fn any_array_index_child() -> impl Strategy<Value = Selector> {
any_non_negative_array_index().prop_map(|i| Selector {
string: format!("[{}]", i.get_index()),
V0ldek marked this conversation as resolved.
Show resolved Hide resolved
tag: SelectorTag::ArrayIndexChild(i),
})
}

fn any_array_index_descendant() -> impl Strategy<Value = Selector> {
any_non_negative_array_index().prop_map(|i| Selector {
string: format!("..[{}]", i.get_index()),
tag: SelectorTag::ArrayIndexDescendant(i),
})
}

fn any_member() -> impl Strategy<Value = String> {
r#"([A-Za-z]|_|[^\u0000-\u007F])([A-Za-z0-9]|_|[^\u0000-\u007F])*"#
}

fn any_index() -> impl Strategy<Value = (String, String)> {
fn any_name() -> impl Strategy<Value = (String, String)> {
any_quoted_member().prop_map(|(s, l)| (format!("[{s}]"), l))
}

Expand All @@ -289,6 +308,11 @@ mod proptests {
fn any_double_quoted_member() -> impl Strategy<Value = String> {
r#"([^'"\\\u0000-\u001F]|(\\[btnfr/\\])|[']|(\\"))*"#
}

fn any_non_negative_array_index() -> impl Strategy<Value = NonNegativeArrayIndex> {
const MAX: u64 = (1 << 53) - 1;
(0..MAX).prop_map(NonNegativeArrayIndex::new)
}
// Cspell: enable

prop_compose! {
Expand All @@ -308,6 +332,8 @@ mod proptests {
SelectorTag::Child(name) => query.child(JsonString::new(&transform_json_escape_sequences(name))),
SelectorTag::WildcardDescendant => query.any_descendant(),
SelectorTag::Descendant(name) => query.descendant(JsonString::new(&transform_json_escape_sequences(name))),
SelectorTag::ArrayIndexChild(idx) => query.array_index_child(idx),
SelectorTag::ArrayIndexDescendant(idx) => query.array_index_descendant(idx)
};
}

Expand Down