Skip to content

Commit

Permalink
Custom matchers for different completenesses
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Oct 18, 2024
1 parent 97427c6 commit 1a13df5
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions scopegraphs/src/resolve/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,20 @@ where

#[cfg(test)]
mod tests {
use std::future::Future;

use scopegraphs_macros::label_order;

use crate::{
add_scope,
completeness::{ExplicitClose, FutureCompleteness, ImplicitClose, UncheckedCompleteness},
completeness::{
self, Completeness, EdgeClosedError, ExplicitClose, FutureCompleteness, ImplicitClose,
UncheckedCompleteness,
},
containers::{EnvContainer, PathContainer, ScopeContainer},
future_wrapper::FutureWrapper,
query_regex,
render::Edge,
resolve::{DataWellformedness, Resolve, ResolvedPath},
storage::Storage,
Label, ScopeGraph,
Expand Down Expand Up @@ -404,7 +411,17 @@ mod tests {
}
}

fn matcher(n: &'a str) -> impl DataWellformedness<Self> {
fn matcher(n: &'a str) -> impl (for<'b> Fn(&'b Self) -> bool) {
|data: &Self| data.matches(n)
}

fn matcher_res(
n: &'a str,
) -> impl (for<'b> Fn(&'b Self) -> Result<bool, EdgeClosedError<Lbl>>) {
|data: &Self| Result::Ok(data.matches(n))
}

fn matcher_fut(n: &'a str) -> impl (for<'b> Fn(&'b Self) -> FutureWrapper<bool>) {
|data: &Self| {
let matches = data.matches(n);
return FutureWrapper::new(async move { return matches });
Expand All @@ -425,8 +442,9 @@ mod tests {
fn test_label_order_async() {
futures::executor::block_on(async {
let storage = Storage::new();
let completeness = FutureCompleteness::default();
let scope_graph: ScopeGraph<Lbl, TData, FutureCompleteness<Lbl>> =
ScopeGraph::new(&storage, FutureCompleteness::default());
ScopeGraph::new(&storage, completeness);

let s0 = scope_graph.add_scope_default_closed();
let (s_with, with_lex, with_imp) = add_scope!(&scope_graph, [Lex, Imp]);
Expand Down Expand Up @@ -460,7 +478,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex* Imp? Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, FutureWrapper<bool>>(TData::matcher_fut("x"))
.with_label_order(label_order!(Lbl: Def < Imp < Lex))
.resolve(s_let)
.await;
Expand All @@ -471,7 +489,7 @@ mod tests {
let path: &ResolvedPath<Lbl, TData> = &env_vec[0];
assert!(matches!(path.data(), &Data { name: "x", data: 2 }));

// todo!("assert the correct edges are closed!")
// todo!("assert the correct edges are closed!") */
});
}

Expand All @@ -487,7 +505,7 @@ mod tests {
let env = scope_graph.query();
let query = env
.with_path_wellformedness(query_regex!(Lbl: Def))
.with_data_wellformedness(TData::matcher("x"));
.with_data_wellformedness::<_, bool>(TData::matcher("x"));
let env = query.resolve(s0);

let env_vec = env.into_iter().collect::<Vec<_>>();
Expand All @@ -509,7 +527,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Def))
.with_data_wellformedness(TData::matcher("y"))
.with_data_wellformedness::<_, bool>(TData::matcher("y"))
.resolve(s0);

let env_vec = env.into_iter().collect::<Vec<_>>();
Expand All @@ -531,7 +549,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
.resolve(s0);

let env_vec = env.into_iter().collect::<Vec<_>>();
Expand All @@ -555,7 +573,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
.resolve(s0);

let env_vec = env.into_iter().collect::<Vec<_>>();
Expand All @@ -579,7 +597,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
.with_label_order(label_order!(Lbl: Lex < Imp))
.resolve(s0);

Expand Down Expand Up @@ -611,7 +629,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex* Imp? Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
.with_label_order(label_order!(Lbl: Def < Imp < Lex))
.resolve(s_let);

Expand Down Expand Up @@ -653,7 +671,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex* Imp? Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
.with_label_order(label_order!(Lbl: Def < Imp < Lex))
.resolve(s_let);

Expand Down Expand Up @@ -703,7 +721,9 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex* Imp? Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, Result<bool, EdgeClosedError<Lbl>>>(TData::matcher_res(
"x",
))
.with_label_order(label_order!(Lbl: Def < Imp < Lex))
.resolve(s_let)
.unwrap();
Expand Down Expand Up @@ -738,7 +758,7 @@ mod tests {
let env = scope_graph
.query()
.with_path_wellformedness(query_regex!(Lbl: Lex* Imp? Def))
.with_data_wellformedness(TData::matcher("x"))
.with_data_wellformedness::<_, bool>(TData::matcher("x"))
// Because of the label order, `Def` will be in the smaller set of both Lex and Imp
// Due to the caching, it will be reused rather than recomputed.
// This can be seen when running this test with RUST_LOG=info.
Expand Down

0 comments on commit 1a13df5

Please sign in to comment.