summary refs log blame commit diff stats
path: root/tests/basic_match.rs
blob: b8d0dd8360a2ec55d366418edd63b953a09f1ed4 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                             
 

                                        
 
                        
 
       
                 
                                                                        
                                                                                         











                                                                        

 

















































































































                                                                                                                                                                   
// Copyright (C) 2021-2022 Soni L.
// SPDX-License-Identifier: MIT OR Apache-2.0

use serde_json::Deserializer as JsonDer;
use serde::Deserialize;

//use datafu::Predicate;

#[test]
fn test_basic() {
    let mut der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#);
    let pat = datafu::PatternBuilder::for_pattern("->[x]:map->[yk]y").compile().unwrap();
    #[derive(Deserialize)]
    struct Values {
        x: String,
        yk: String,
        y: usize,
    }
    // should this error about needing multiple results/Vec requirement?
    let matches: Values = pat.deserialize(&mut der).unwrap();
    assert_eq!(matches.x, "bar");
    assert_eq!(matches.yk, "baz");
    assert_eq!(matches.y, 2);
    assert!(der.end().is_ok());
}

//#[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 der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#)
//    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]
//fn test_basic_2() {
//    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![("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().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!(matcher.next().is_none());
//}
//
//#[test]
//fn test_spaces() {
//    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());
//}
//
//#[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>>),
//        ("str", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
//        ("bool", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::B(_)))) as Box<datafu::Predicate<Value>>),
//        // stubs, we don't particularly need to test these
//        ("commit", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
//        ("uri", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
//    ].into_iter().collect();
//    let pat = datafu::Pattern::<Value>::compile::<_, &str>("
//        ->'projects':$dict
//          ->commit[:?$str:?$commit]:?$dict
//            ->url[:?$str:?$uri]:?$dict
//              ->branch:?$dict
//                (->active'active'?:?$bool)
//                (->federate'federate'?:?$bool)?",
//        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["active"].1, RefOwn::Ref(&Value::B(true)));
//    assert_eq!(m.get("federate"), None);
//    assert!(matcher.next().is_none());
//}