forked from conjure-cp/conjure-oxide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model example library for testing (conjure-cp#157)
* Eliminated example/ dir to integrate with conjure_oxide/tests/integration dir * init attempt for generate_custom.rs for example custom string input * Added comprehension and enumerate essence file examples * generate_custom.rs temporary fix for walkdir filter filename [DOESN'T COMPILE] * added finite given [set] tests * fix for generate_custom.rs get_example_model: only filename and correct walkdir filter * added 'interesting tests' to integration test dir * fix dependencies in main.rs for custom_example * (ignore) playing around with code coverage sample yml file * final changes custom return model function * changed main.rs cfg tests to xyz instead of bool (no current support) * eliminated yaml code-coverage file for PR test passing * debug boolean 01/02/03 integration test with cargo test * removed conjure-output for basic/01 bool tests folder
- Loading branch information
Showing
32 changed files
with
563 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// generate_custom.rs with get_example_model function | ||
|
||
// dependencies | ||
use crate::parse::model_from_json; | ||
use conjure_core::ast::Model; | ||
use std::env; | ||
use std::error::Error; | ||
use std::fs::{copy, read_to_string, File}; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use walkdir::WalkDir; | ||
|
||
use serde_json::Value; | ||
|
||
/// Searches recursively in `../tests/integration` folder for an `.essence` file matching the given filename, | ||
/// then uses conjure to process it into astjson, and returns the parsed model. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `filename` - A string slice that holds filename without extension | ||
/// | ||
/// # Returns | ||
/// | ||
/// Function returns a `Result<Value, Box<dyn Error>>`, where `Value` is the parsed model | ||
pub fn get_example_model(filename: &str) -> Result<Model, Box<dyn Error>> { | ||
// define relative path -> integration tests dir | ||
let base_dir = "tests/integration"; | ||
let mut essence_path = PathBuf::new(); | ||
|
||
// walk through directory tree recursively starting at base | ||
for entry in WalkDir::new(base_dir).into_iter().filter_map(|e| e.ok()) { | ||
let path = entry.path(); | ||
if path.is_file() | ||
&& path.extension().map_or(false, |e| e == "essence") | ||
&& path.file_stem() == Some(std::ffi::OsStr::new(filename)) | ||
{ | ||
essence_path = path.to_path_buf(); | ||
break; | ||
} | ||
} | ||
|
||
println!("PATH TO FILE: {}", essence_path.display()); | ||
|
||
// return error if file not found | ||
if essence_path.as_os_str().is_empty() { | ||
return Err(Box::new(std::io::Error::new( | ||
std::io::ErrorKind::NotFound, | ||
"ERROR: File not found in any subdirectory", | ||
))); | ||
} | ||
|
||
// let path = PathBuf::from(format!("../tests/integration/basic/comprehension{}.essence", filename)); | ||
let mut cmd = std::process::Command::new("conjure"); | ||
let output = cmd | ||
.arg("pretty") | ||
.arg("--output-format=astjson") | ||
.arg(essence_path) | ||
.output()?; | ||
|
||
// convert Conjure's stdout from bytes to string | ||
let astjson = String::from_utf8(output.stdout)?; | ||
|
||
println!("ASTJSON: {}", astjson); | ||
|
||
// parse AST JSON from desired Model format | ||
let generated_mdl = model_from_json(&astjson)?; | ||
|
||
Ok(generated_mdl) | ||
} | ||
|
||
/// Searches for an `.essence` file at the given filepath, | ||
/// then uses conjure to process it into astjson, and returns the parsed model. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `filepath` - A string slice that holds the full file path | ||
/// | ||
/// # Returns | ||
/// | ||
/// Function returns a `Result<Value, Box<dyn Error>>`, where `Value` is the parsed model | ||
pub fn get_example_model_by_path(filepath: &str) -> Result<Model, Box<dyn Error>> { | ||
let essence_path = PathBuf::from(filepath); | ||
|
||
// return error if file not found | ||
if essence_path.as_os_str().is_empty() { | ||
return Err(Box::new(std::io::Error::new( | ||
std::io::ErrorKind::NotFound, | ||
"ERROR: File not found in any subdirectory", | ||
))); | ||
} | ||
|
||
println!("PATH TO FILE: {}", essence_path.display()); | ||
|
||
// Command execution using 'conjure' CLI tool with provided path | ||
let mut cmd = std::process::Command::new("conjure"); | ||
let output = cmd | ||
.arg("pretty") | ||
.arg("--output-format=astjson") | ||
.arg(&essence_path) | ||
.output()?; | ||
|
||
// convert Conjure's stdout from bytes to string | ||
let astjson = String::from_utf8(output.stdout)?; | ||
|
||
println!("ASTJSON: {}", astjson); | ||
|
||
// parse AST JSON into the desired Model format | ||
let generated_model = model_from_json(&astjson)?; | ||
|
||
Ok(generated_model) | ||
} | ||
|
||
/// Recursively sorts the keys of all JSON objects within the provided JSON value. | ||
/// | ||
/// serde_json will output JSON objects in an arbitrary key order. | ||
/// this is normally fine, except in our use case we wouldn't want to update the expected output again and again. | ||
/// so a consistent (sorted) ordering of the keys is desirable. | ||
fn sort_json_object(value: &Value) -> Value { | ||
match value { | ||
Value::Object(obj) => { | ||
let mut ordered: Vec<(String, Value)> = obj | ||
.iter() | ||
.map(|(k, v)| { | ||
if k == "variables" { | ||
(k.clone(), sort_json_variables(v)) | ||
} else { | ||
(k.clone(), sort_json_object(v)) | ||
} | ||
}) | ||
// .map(|(k, v)| (k.clone(), sort_json_object(v))) | ||
.collect(); | ||
ordered.sort_by(|a, b| a.0.cmp(&b.0)); | ||
|
||
Value::Object(ordered.into_iter().collect()) | ||
} | ||
Value::Array(arr) => Value::Array(arr.iter().map(sort_json_object).collect()), | ||
_ => value.clone(), | ||
} | ||
} | ||
|
||
/// Sort the "variables" field by name. | ||
/// We have to do this separately becasue that field is not a JSON object, instead it's an array of tuples. | ||
fn sort_json_variables(value: &Value) -> Value { | ||
match value { | ||
Value::Array(vars) => { | ||
let mut vars_sorted = vars.clone(); | ||
vars_sorted.sort_by(|a, b| { | ||
let a_obj = &a.as_array().unwrap()[0]; | ||
let a_name: crate::ast::Name = serde_json::from_value(a_obj.clone()).unwrap(); | ||
|
||
let b_obj = &b.as_array().unwrap()[0]; | ||
let b_name: crate::ast::Name = serde_json::from_value(b_obj.clone()).unwrap(); | ||
|
||
a_name.cmp(&b_name) | ||
}); | ||
Value::Array(vars_sorted) | ||
} | ||
_ => value.clone(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
conjure_oxide/tests/integration/basic/comprehension-01-1/comprehension-01-1.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
such that x = sum([ 1 | i : set (size 2) of int(7..9) ]) |
7 changes: 7 additions & 0 deletions
7
conjure_oxide/tests/integration/basic/comprehension-01-2/comprehension-01-2.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
find y : int(7,8) | ||
such that x = sum([ 1 | i : set (size 2) of int(7..9) | ||
, y in i | ||
]) |
4 changes: 4 additions & 0 deletions
4
conjure_oxide/tests/integration/basic/comprehension-02-1/comprehension-02-1.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
such that x = sum([ j | i : set (minSize 1, maxSize 2) of int(7..8), j <- i ]) |
8 changes: 8 additions & 0 deletions
8
conjure_oxide/tests/integration/basic/comprehension-02-2/comprehension-02-2.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
find y : int(7,8) | ||
such that x = sum([ j | i : set (minSize 1, maxSize 2) of int(7..8) | ||
, y in i | ||
, j <- i | ||
]) |
4 changes: 4 additions & 0 deletions
4
conjure_oxide/tests/integration/basic/comprehension-03-1/comprehension-03-1.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
such that x = sum([ 1 | i : set (minSize 1, maxSize 2) of int(7..9) ]) |
7 changes: 7 additions & 0 deletions
7
conjure_oxide/tests/integration/basic/comprehension-03-2/comprehension-03-2.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
find y : int(7,8) | ||
such that x = sum([ 1 | i : set (minSize 1, maxSize 2) of int(7..9) | ||
, y in i | ||
]) |
4 changes: 4 additions & 0 deletions
4
conjure_oxide/tests/integration/basic/comprehension-04-1/comprehension-04-1.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
such that x = sum([ 1 | i : set (minSize 1, maxSize 2) of (int(7..9), bool) ]) |
8 changes: 8 additions & 0 deletions
8
conjure_oxide/tests/integration/basic/comprehension-04-2/comprehension-04-2.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language Essence 1.3 | ||
|
||
find x : int(0..1000) | ||
find y : int(7,8) | ||
find z : bool | ||
such that x = sum([ 1 | i : set (minSize 1, maxSize 2) of (int(7..9), bool) | ||
, (y,z) in i | ||
]) |
11 changes: 11 additions & 0 deletions
11
conjure_oxide/tests/integration/basic/enum-01/enum-01.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language Essence 1.3 | ||
|
||
given E1 new type enum | ||
find x : E1 | ||
|
||
letting E2 be new type enum {a,b,c,d} | ||
find y : E2 | ||
find z : E2(a..c) | ||
find t : E2(b,d) | ||
|
||
such that y = z, z = t |
8 changes: 8 additions & 0 deletions
8
conjure_oxide/tests/integration/basic/enum-02/enum-02.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language Essence 1.3 | ||
|
||
given E1 new type enum | ||
letting E2 be new type enum {a,b,c,d} | ||
|
||
find x : (E1, E2) | ||
|
||
such that x[2] = a |
9 changes: 9 additions & 0 deletions
9
conjure_oxide/tests/integration/basic/enum-03/enum-03.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language Essence 1.3 | ||
|
||
given E1 new type enum | ||
find x : E1 | ||
such that forAll i : E1 . x <= i | ||
|
||
letting E2 be new type enum {a,b,c,d} | ||
find y : E2 | ||
such that forAll j : E2 . y <= j |
7 changes: 7 additions & 0 deletions
7
...re_oxide/tests/integration/basic/finite-givens-set01/finite-givens-set01.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language Essence 1.3 | ||
|
||
given a : set of int | ||
find x,y,z : int(0..100) | ||
such that x = |a| | ||
such that y = min(a) | ||
such that z = max(a) |
4 changes: 4 additions & 0 deletions
4
...e_oxide/tests/integration/basic/finite-givens-set02/finite-givens-set-02.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language Essence 1.3 | ||
|
||
given a : set of int | ||
find x : int(|a|) |
9 changes: 9 additions & 0 deletions
9
...re_oxide/tests/integration/basic/finite-givens-set03/finite-givens-set03.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language Essence 1.3 | ||
|
||
given a : set of int | ||
find x,y,z,t : int(0..100) | ||
such that x = |a| | ||
such that y = min(a) | ||
such that z = max(a) | ||
such that allDiff([x,y,z,t]) | ||
such that t in a $ refer to a |
5 changes: 5 additions & 0 deletions
5
...re_oxide/tests/integration/basic/finite-givens-set04/finite-givens-set04.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language Essence 1.3 | ||
|
||
given a : set of set of int | ||
find x : int(-1000..1000) | ||
such that x = max([ max(i) | i <- a ]) |
5 changes: 5 additions & 0 deletions
5
...re_oxide/tests/integration/basic/finite-givens-set05/finite-givens-set05.essence.disabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language Essence 1.3 | ||
|
||
given s: set of set (minSize 0) of int(2..5, 4) | ||
find x : int(0..9) | ||
such that exists s' in s . x in s' |
7 changes: 7 additions & 0 deletions
7
conjure_oxide/tests/integration/mildly-interesting/cyclic-graph/cyc1.param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language Essence 1.3 | ||
|
||
letting n be 5 | ||
letting g be relation | ||
( (1,2) | ||
, (2,1) | ||
) |
8 changes: 8 additions & 0 deletions
8
conjure_oxide/tests/integration/mildly-interesting/cyclic-graph/cyc2.param
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language Essence 1.3 | ||
|
||
letting n be 5 | ||
letting g be relation | ||
( (1,2) | ||
, (2,3) | ||
, (3,1) | ||
) |
Oops, something went wrong.