1
use std::env;
2
use std::error::Error;
3
use std::path::Path;
4
use std::sync::Arc;
5
use std::sync::RwLock;
6

            
7
use conjure_core::context::Context;
8
use conjure_oxide::rule_engine::resolve_rule_sets;
9
use conjure_oxide::rule_engine::rewrite_model;
10
use conjure_oxide::utils::conjure::{get_minion_solutions, parse_essence_file};
11
use conjure_oxide::utils::testing::save_stats_json;
12
use conjure_oxide::utils::testing::{
13
    read_minion_solutions_json, read_model_json, save_minion_solutions_json, save_model_json,
14
};
15
use conjure_oxide::SolverFamily;
16

            
17
fn main() {
18
    let file_path = Path::new("/path/to/your/file.txt");
19
    let base_name = file_path.file_stem().and_then(|stem| stem.to_str());
20

            
21
    match base_name {
22
        Some(name) => println!("Base name: {}", name),
23
        None => println!("Could not extract the base name"),
24
    }
25
}
26

            
27
#[allow(clippy::unwrap_used)]
28
fn integration_test(path: &str, essence_base: &str) -> Result<(), Box<dyn Error>> {
29
    let context: Arc<RwLock<Context<'static>>> = Default::default();
30
    let accept = env::var("ACCEPT").unwrap_or("false".to_string()) == "true";
31
    let verbose = env::var("VERBOSE").unwrap_or("false".to_string()) == "true";
32

            
33
    if verbose {
34
        println!(
35
            "Running integration test for {}/{}, ACCEPT={}",
36
            path, essence_base, accept
37
        );
38
    }
39

            
40
    // Stage 1: Read the essence file and check that the model is parsed correctly
41
    let model = parse_essence_file(path, essence_base, context.clone())?;
42
    if verbose {
43
        println!("Parsed model: {:#?}", model)
44
    }
45

            
46
    context.as_ref().write().unwrap().file_name = Some(format!("{path}/{essence_base}.essence"));
47

            
48
    save_model_json(&model, path, essence_base, "parse", accept)?;
49
    let expected_model = read_model_json(path, essence_base, "expected", "parse")?;
50
    if verbose {
51
        println!("Expected model: {:#?}", expected_model)
52
    }
53

            
54
    assert_eq!(model, expected_model);
55

            
56
    // Stage 2: Rewrite the model using the rule engine and check that the result is as expected
57
    let rule_sets = resolve_rule_sets(
58
        SolverFamily::Minion,
59
        &vec!["Constant".to_string(), "Bubble".to_string()],
60
    )?;
61
    let model = rewrite_model(&model, &rule_sets)?;
62
    if verbose {
63
        println!("Rewritten model: {:#?}", model)
64
    }
65

            
66
    save_model_json(&model, path, essence_base, "rewrite", accept)?;
67
    let expected_model = read_model_json(path, essence_base, "expected", "rewrite")?;
68
    if verbose {
69
        println!("Expected model: {:#?}", expected_model)
70
    }
71

            
72
    assert_eq!(model, expected_model);
73

            
74
    // Stage 3: Run the model through the Minion solver and check that the solutions are as expected
75
    let solutions = get_minion_solutions(model)?;
76
    let solutions_json = save_minion_solutions_json(&solutions, path, essence_base, accept)?;
77
    if verbose {
78
        println!("Minion solutions: {:#?}", solutions_json)
79
    }
80

            
81
    let expected_solutions_json = read_minion_solutions_json(path, essence_base, "expected")?;
82
    if verbose {
83
        println!("Expected solutions: {:#?}", expected_solutions_json)
84
    }
85

            
86
    assert_eq!(solutions_json, expected_solutions_json);
87

            
88
    save_stats_json(context, path, essence_base)?;
89

            
90
    Ok(())
91
}
92

            
93
#[test]
94
fn assert_conjure_present() {
95
    conjure_oxide::find_conjure::conjure_executable().unwrap();
96
}
97

            
98
include!(concat!(env!("OUT_DIR"), "/gen_tests.rs"));