summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/basic_match.rs51
-rw-r--r--tests/common/mod.rs10
-rw-r--r--tests/parser_prop.rs33
3 files changed, 65 insertions, 29 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())));
 }
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 48153af..9680504 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -24,7 +24,7 @@ use datafu::RefOwn;
 use datafu::PatternTypes;
 use datafu::KVPair;
 
-#[derive(PartialEq, Eq, PartialOrd, Ord)]
+#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
 pub enum Value {
     U(usize),
     B(bool),
@@ -32,7 +32,7 @@ pub enum Value {
     S(Cow<'static, str>),
 }
 
-#[derive(Copy, Clone, Eq, PartialEq)]
+#[derive(Copy, Clone, Eq, PartialEq, Debug)]
 pub enum Dummy {
 }
 
@@ -66,24 +66,28 @@ impl PartialEq<Value> for str {
 
 impl PartialEq<Dummy> for Value {
     fn eq(&self, other: &Dummy) -> bool {
+        let _ = other;
         unreachable!()
     }
 }
 
 impl PartialEq<Value> for Dummy {
     fn eq(&self, other: &Value) -> bool {
+        let _ = other;
         unreachable!()
     }
 }
 
 impl PartialEq<str> for Dummy {
     fn eq(&self, other: &str) -> bool {
+        let _ = other;
         unreachable!()
     }
 }
 
 impl PartialEq<Dummy> for str {
     fn eq(&self, other: &Dummy) -> bool {
+        let _ = other;
         unreachable!()
     }
 }
@@ -158,7 +162,7 @@ impl PatternTypes for Value {
             RefOwn::Ref(Value::M(map)) => {
                 Some(match key {
                     RefOwn::Ref(key) => map.get_key_value(key),
-                    RefOwn::Own(key) => unreachable!(),//map.get_key_value(&Value::U(key)),
+                    RefOwn::Own(_key) => unreachable!(),
                     RefOwn::Str(key) => map.get_key_value(&key as &dyn ValueHelper),
                 }.map(|(k,v)| (k.into(), v.into())))
             },
diff --git a/tests/parser_prop.rs b/tests/parser_prop.rs
new file mode 100644
index 0000000..57976cb
--- /dev/null
+++ b/tests/parser_prop.rs
@@ -0,0 +1,33 @@
+/*
+ * This file is part of Datafu
+ * Copyright (C) 2021  Soni L.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+extern crate proptest;
+extern crate datafu;
+
+mod common;
+
+use common::Value;
+
+use proptest::prelude::*;
+
+proptest! {
+    #[test]
+    fn doesnt_panic(s in "\\PC*") {
+        let _ = datafu::Pattern::<Value>::compile::<&str, &str>(&s, None, None);
+    }
+}