summary refs log tree commit diff stats
path: root/tests/basic_match.rs
blob: d6f4da3e458a523754c37fcfee1b58942d78e64b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * 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 datafu;

mod common;

use common::Value;

use datafu::RefOwn;

#[test]
fn test_basic_example() {
    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:?$dict->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)));
}

#[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())));
}

#[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["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())));
}