summary refs log tree commit diff stats
path: root/tests/basic_match.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/basic_match.rs')
-rw-r--r--tests/basic_match.rs51
1 files changed, 25 insertions, 26 deletions
diff --git a/tests/basic_match.rs b/tests/basic_match.rs
index 2f65451..d6f4da3 100644
--- a/tests/basic_match.rs
+++ b/tests/basic_match.rs
@@ -22,6 +22,8 @@ mod common;
 
 use common::Value;
 
+use datafu::RefOwn;
+
 #[test]
 fn test_basic_example() {
     let tree = Value::M(vec![
@@ -30,14 +32,13 @@ fn test_basic_example() {
             ("baz".into(), Value::U(2)),
         ].into_iter().collect())),
     ].into_iter().collect());
-    let pat = datafu::Pattern::<Value>::compile("->X:?$dict->Y", None).ok().unwrap();
+    let preds = vec![("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
+    let pat = datafu::Pattern::<Value>::compile::<&str, &str>("->X:?$dict->Y", Some(preds), None).unwrap();
     let mut matcher = pat.attempt_match(&tree);
-    let m = matcher.next();
-    // TODO
-    todo!();
-    //assert m['X'][0] == 'bar'
-    //assert m['Y'][0] == 'baz'
-    //assert m['Y'][1] == 2
+    let m = matcher.next().unwrap().unwrap();
+    assert_eq!(m["X"].0, RefOwn::Ref(&Value::from("bar")));
+    assert_eq!(m["Y"].0, RefOwn::Ref(&Value::from("baz")));
+    assert_eq!(m["Y"].1, RefOwn::Ref(&Value::U(2)));
 }
 
 #[test]
@@ -53,15 +54,14 @@ fn test_basic_2() {
             ].into_iter().collect())),
         ].into_iter().collect())),
     ].into_iter().collect());
-    let pat = datafu::Pattern::<Value>::compile("->'projects':?$d->P/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$d->U:?$d->B", None).ok().unwrap();
+    let preds = vec![("d", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
+    let pat = datafu::Pattern::<Value>::compile::<&str, &str>("->'projects':?$d->P/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$d->U:?$d->B", Some(preds), None).unwrap();
     let mut matcher = pat.attempt_match(&tree);
-    let m = matcher.next();
-    // TODO
-    todo!();
-    //assert m['P'][0] == "385e734a52e13949a7a5c71827f6de920dbfea43"
-    //assert m['U'][0] == "https://soniex2.autistic.space/git-repos/ganarchy.git"
-    //assert m['B'][0] == "HEAD"
-    //assert m['B'][1] == {"active": True}
+    let m = matcher.next().unwrap().unwrap();
+    assert_eq!(m["P"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
+    assert_eq!(m["U"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
+    assert_eq!(m["B"].0, RefOwn::Ref(&Value::from("HEAD")));
+    assert_eq!(m["B"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
 }
 
 #[test]
@@ -77,16 +77,15 @@ fn test_spaces() {
             ].into_iter().collect())),
         ].into_iter().collect())),
     ].into_iter().collect());
-    let pat = datafu::Pattern::<Value>::compile("-> 'projects'?
-                                                    -> commit /[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/? :?$dict
-                                                       -> url :?$dict
-                                                          -> branch :?$dict", None).ok().unwrap();
+    let preds = vec![("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
+    let pat = datafu::Pattern::<Value>::compile::<_, &str>("-> 'projects'?
+                                                               -> commit /[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/? :?$dict
+                                                                  -> url :?$dict
+                                                                     -> branch :?$dict", Some(preds), None).unwrap();
     let mut matcher = pat.attempt_match(&tree);
-    let m = matcher.next();
-    // TODO
-    todo!();
-    //assert m['commit'][0] == "385e734a52e13949a7a5c71827f6de920dbfea43"
-    //assert m['url'][0] == "https://soniex2.autistic.space/git-repos/ganarchy.git"
-    //assert m['branch'][0] == "HEAD"
-    //assert m['branch'][1] == {"active": True}
+    let m = matcher.next().unwrap().unwrap();
+    assert_eq!(m["P"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
+    assert_eq!(m["U"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
+    assert_eq!(m["B"].0, RefOwn::Ref(&Value::from("HEAD")));
+    assert_eq!(m["B"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
 }