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, extension: &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, extension, context.clone())?;
42
    if verbose {
43
        println!("Parsed model: {:#?}", model)
44
    }
45

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

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

            
55
    assert_eq!(model, expected_model);
56

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

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

            
73
    assert_eq!(model, expected_model);
74

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

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

            
87
    assert_eq!(solutions_json, expected_solutions_json);
88

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

            
91
    Ok(())
92
}
93

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

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