1
use conjure_core::{
2
    ast::{Atom, Expression},
3
    context::Context,
4
    matrix_expr,
5
};
6
use conjure_oxide::{utils::essence_parser::parse_essence_file_native, Metadata};
7
use pretty_assertions::assert_eq;
8
use project_root::get_project_root;
9
use std::sync::{Arc, RwLock};
10

            
11
#[test]
12
1
fn test_parse_dominance() {
13
1
    let root = get_project_root().unwrap().canonicalize().unwrap();
14
1
    let path = root.join("conjure_oxide/tests/parser_tests");
15
1
    let file = "dominance_simple";
16
1

            
17
1
    let expected_dominance = Some(Expression::DominanceRelation(
18
1
        Metadata::new(),
19
1
        Box::new(Expression::And(
20
1
            Metadata::new(),
21
1
            Box::new(matrix_expr![
22
1
                Expression::And(
23
1
                    Metadata::new(),
24
1
                    Box::new(matrix_expr![
25
1
                        Expression::Leq(
26
1
                            Metadata::new(),
27
1
                            Box::new(Expression::Atomic(Metadata::new(), Atom::new_uref("cost"))),
28
1
                            Box::new(Expression::FromSolution(
29
1
                                Metadata::new(),
30
1
                                Box::new(Expression::Atomic(
31
1
                                    Metadata::new(),
32
1
                                    Atom::new_uref("cost"),
33
1
                                )),
34
1
                            )),
35
1
                        ),
36
1
                        Expression::Leq(
37
1
                            Metadata::new(),
38
1
                            Box::new(Expression::Atomic(
39
1
                                Metadata::new(),
40
1
                                Atom::new_uref("carbon"),
41
1
                            )),
42
1
                            Box::new(Expression::FromSolution(
43
1
                                Metadata::new(),
44
1
                                Box::new(Expression::Atomic(
45
1
                                    Metadata::new(),
46
1
                                    Atom::new_uref("carbon"),
47
1
                                )),
48
1
                            )),
49
1
                        ),
50
1
                    ]),
51
1
                ),
52
1
                Expression::Or(
53
1
                    Metadata::new(),
54
1
                    Box::new(matrix_expr![
55
1
                        Expression::Lt(
56
1
                            Metadata::new(),
57
1
                            Box::new(Expression::Atomic(Metadata::new(), Atom::new_uref("cost"))),
58
1
                            Box::new(Expression::FromSolution(
59
1
                                Metadata::new(),
60
1
                                Box::new(Expression::Atomic(
61
1
                                    Metadata::new(),
62
1
                                    Atom::new_uref("cost"),
63
1
                                )),
64
1
                            )),
65
1
                        ),
66
1
                        Expression::Lt(
67
1
                            Metadata::new(),
68
1
                            Box::new(Expression::Atomic(
69
1
                                Metadata::new(),
70
1
                                Atom::new_uref("carbon"),
71
1
                            )),
72
1
                            Box::new(Expression::FromSolution(
73
1
                                Metadata::new(),
74
1
                                Box::new(Expression::Atomic(
75
1
                                    Metadata::new(),
76
1
                                    Atom::new_uref("carbon"),
77
1
                                )),
78
1
                            )),
79
1
                        ),
80
1
                    ]),
81
1
                ),
82
1
            ]),
83
1
        )),
84
1
    ));
85
1

            
86
1
    let ctx = Arc::new(RwLock::new(Context::default()));
87
1
    let pth = path.to_str().unwrap();
88
1

            
89
1
    let res = parse_essence_file_native(pth, file, "essence", ctx);
90
1
    assert!(res.is_ok());
91

            
92
1
    let model = res.unwrap();
93
1
    assert_eq!(model.dominance, expected_dominance);
94
1
}
95

            
96
#[test]
97
1
fn test_dominance_twice() {
98
1
    let root = get_project_root().unwrap().canonicalize().unwrap();
99
1
    let path = root.join("conjure_oxide/tests/parser_tests");
100
1
    let file = "dominance_twice";
101
1

            
102
1
    let ctx = Arc::new(RwLock::new(Context::default()));
103
1
    let pth = path.to_str().unwrap();
104
1

            
105
1
    let res = parse_essence_file_native(pth, file, "essence", ctx);
106
1
    assert!(res.is_err());
107
1
}
108

            
109
#[test]
110
1
fn test_no_dominance() {
111
1
    let root = get_project_root().unwrap().canonicalize().unwrap();
112
1
    let path = root.join("conjure_oxide/tests/parser_tests");
113
1

            
114
1
    let pth = path.to_str().unwrap();
115
1
    let res_nodom = parse_essence_file_native(
116
1
        pth,
117
1
        "no_dominance",
118
1
        "essence",
119
1
        Arc::new(RwLock::new(Context::default())),
120
1
    );
121
1
    let res_dom = parse_essence_file_native(
122
1
        pth,
123
1
        "dominance_simple",
124
1
        "essence",
125
1
        Arc::new(RwLock::new(Context::default())),
126
1
    );
127
1

            
128
1
    assert!(res_nodom.is_ok());
129
1
    assert!(res_dom.is_ok());
130

            
131
1
    let mod_nodom = res_nodom.unwrap();
132
1
    let mod_dom = res_dom.unwrap();
133
1

            
134
1
    assert_eq!(mod_nodom.as_submodel(), mod_dom.as_submodel());
135
1
    assert!(mod_nodom.dominance.is_none());
136
1
    assert!(mod_dom.dominance.is_some());
137
1
}