Add expected results to integration test

Results are logs of what we get with old toplevel-based version of lib_machine. These are also congruent with what our tests logged out based on SWI.
This commit is contained in:
Nicolas Luck
2024-01-26 17:18:57 +01:00
parent 77ce5a9586
commit cb014095ad
5 changed files with 11102 additions and 65 deletions

File diff suppressed because one or more lines are too long

View File

@@ -455,6 +455,7 @@ mod tests {
let blocks = code.split("=====");
let mut i = 0;
let mut last_result: Option<_> = None;
// Iterate over the blocks
for block in blocks {
// Trim the block to remove any leading or trailing whitespace
@@ -471,16 +472,24 @@ mod tests {
println!("query #{}: {}", i, query);
// Parse and execute the query
let result = machine.run_query(query.to_string());
assert!(result.is_ok());
// Print the result
println!("{:?}", result);
assert!(result.is_ok());
last_result = Some(result);
} else if let Some(code) = block.strip_prefix("consult") {
println!("load code: {}", code);
// Load the code into the machine
machine.consult_module_string("facts", code.to_string());
} else if let Some(result) = block.strip_prefix("result") {
if let Some(Ok(ref last_result)) = last_result {
assert_eq!(
last_result.to_string().trim(),
result.to_string().trim(),
)
}
}
}
}

View File

@@ -13,6 +13,79 @@ pub enum QueryResolution {
Matches(Vec<QueryMatch>),
}
pub fn prolog_value_to_json_tring(value: Value) -> String {
match value {
Value::Integer(i) => format!("{}", i),
Value::Float(f) => format!("{}", f),
Value::Rational(r) => format!("{}", r),
Value::Atom(a) => format!("{}", a.as_str()),
Value::String(s) =>
if let Err(_e) = serde_json::from_str::<serde_json::Value>(s.as_str()) {
//treat as string literal
//escape double quotes
format!("\"{}\"", s
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("\r", "\\r"))
} else {
//return valid json string
s
},
Value::List(l) => {
let mut string_result = "[".to_string();
for (i, v) in l.iter().enumerate() {
if i > 0 {
string_result.push_str(",");
}
string_result.push_str(&prolog_value_to_json_tring(v.clone()));
}
string_result.push_str("]");
string_result
}
Value::Structure(s, l) => {
let mut string_result = format!("\"{}\":[", s.as_str());
for (i, v) in l.iter().enumerate() {
if i > 0 {
string_result.push_str(",");
}
string_result.push_str(&prolog_value_to_json_tring(v.clone()));
}
string_result.push_str("]");
string_result
}
_ => "null".to_string(),
}
}
fn prolog_match_to_json_string(query_match: &QueryMatch) -> String {
let mut string_result = "{".to_string();
for (i, (k, v)) in query_match.bindings.iter().enumerate() {
if i > 0 {
string_result.push_str(",");
}
string_result.push_str(&format!("\"{}\":{}", k, prolog_value_to_json_tring(v.clone())));
}
string_result.push_str("}");
string_result
}
impl ToString for QueryResolution {
fn to_string(&self) -> String {
match self {
QueryResolution::True => "true".to_string(),
QueryResolution::False => "false".to_string(),
QueryResolution::Matches(matches) => {
let matches_json: Vec<String> = matches
.iter()
.map(|m| prolog_match_to_json_string(m))
.collect();
format!("[{}]", matches_json.join(","))
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryMatch {
pub bindings: BTreeMap<String, Value>,