From 69652efe8ad9738a94fef571c8b81e342f96e7b4 Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Sun, 7 Feb 2021 22:19:21 -0300 Subject: Finish porting parser --- tests/basic_match.rs | 51 +++++++++++++++++++++++++-------------------------- tests/common/mod.rs | 10 +++++++--- tests/parser_prop.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 tests/parser_prop.rs (limited to 'tests') 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::::compile("->X:?$dict->Y", None).ok().unwrap(); + let preds = vec![("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box>)].into_iter().collect(); + let pat = datafu::Pattern::::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::::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>)].into_iter().collect(); + let pat = datafu::Pattern::::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::::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>)].into_iter().collect(); + let pat = datafu::Pattern::::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 for str { impl PartialEq for Value { fn eq(&self, other: &Dummy) -> bool { + let _ = other; unreachable!() } } impl PartialEq for Dummy { fn eq(&self, other: &Value) -> bool { + let _ = other; unreachable!() } } impl PartialEq for Dummy { fn eq(&self, other: &str) -> bool { + let _ = other; unreachable!() } } impl PartialEq 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 . + */ + +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::::compile::<&str, &str>(&s, None, None); + } +} -- cgit 1.4.1