summary refs log tree commit diff stats
path: root/tests/basic_match.rs
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2021-02-11 00:52:25 -0300
committerSoniEx2 <endermoneymod@gmail.com>2021-02-11 00:52:25 -0300
commitabaee464936a821568c3fdbd52b9aadd3adb6d0f (patch)
tree8a8b5c271f0b8b67eeb77e94a68ecf73ffc4e1f9 /tests/basic_match.rs
parent69652efe8ad9738a94fef571c8b81e342f96e7b4 (diff)
Partially implement VM
The following are now implemented:

- [x] Arrow
- [x] StringKey
- [x] RegexKey
- [ ] KeySubtree
- [ ] ValueSubtree
- [x] Ident
- [ ] Param
- [x] ApplyPredicate
- [x] End
Diffstat (limited to 'tests/basic_match.rs')
-rw-r--r--tests/basic_match.rs74
1 files changed, 65 insertions, 9 deletions
diff --git a/tests/basic_match.rs b/tests/basic_match.rs
index d6f4da3..0b5b546 100644
--- a/tests/basic_match.rs
+++ b/tests/basic_match.rs
@@ -25,7 +25,7 @@ use common::Value;
 use datafu::RefOwn;
 
 #[test]
-fn test_basic_example() {
+fn test_basic() {
     let tree = Value::M(vec![
         ("foo".into(), Value::U(1)),
         ("bar".into(), Value::M(vec![
@@ -39,6 +39,25 @@ fn test_basic_example() {
     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)));
+    assert!(matcher.next().is_none());
+}
+
+#[test]
+fn test_str() {
+    let tree = Value::M(vec![
+        ("foo".into(), Value::U(1)),
+        ("bar".into(), Value::M(vec![
+            ("baz".into(), Value::U(2)),
+        ].into_iter().collect())),
+    ].into_iter().collect());
+    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'bar'->Y", Some(preds), None).unwrap();
+    let mut matcher = pat.attempt_match(&tree);
+    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)));
+    assert!(matcher.next().is_none());
 }
 
 #[test]
@@ -62,6 +81,7 @@ fn test_basic_2() {
     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())));
+    assert!(matcher.next().is_none());
 }
 
 #[test]
@@ -78,14 +98,50 @@ fn test_spaces() {
         ].into_iter().collect())),
     ].into_iter().collect());
     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 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().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())));
+    assert_eq!(m["commit"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
+    assert_eq!(m["url"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
+    assert_eq!(m["branch"].0, RefOwn::Ref(&Value::from("HEAD")));
+    assert_eq!(m["branch"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
+    assert!(matcher.next().is_none());
 }
+
+//#[test]
+//fn test_harder() {
+//    let tree = Value::M(vec![
+//        ("projects".into(), Value::M(vec![
+//            ("385e734a52e13949a7a5c71827f6de920dbfea43".into(), Value::M(vec![
+//                ("https://soniex2.autistic.space/git-repos/ganarchy.git".into(), Value::M(vec![
+//                    ("HEAD".into(), Value::M(vec![
+//                        ("active".into(), Value::B(true)),
+//                    ].into_iter().collect())),
+//                ].into_iter().collect())),
+//            ].into_iter().collect())),
+//        ].into_iter().collect())),
+//    ].into_iter().collect());
+//    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().unwrap().unwrap();
+//    assert_eq!(m["commit"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
+//    assert_eq!(m["url"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
+//    assert_eq!(m["branch"].0, RefOwn::Ref(&Value::from("HEAD")));
+//    assert_eq!(m["branch"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
+//    assert!(matcher.next().is_none());
+//}