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:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2504,7 +2504,9 @@ dependencies = [
|
|||||||
"rustyline",
|
"rustyline",
|
||||||
"ryu",
|
"ryu",
|
||||||
"select",
|
"select",
|
||||||
|
"serde",
|
||||||
"serde-wasm-bindgen",
|
"serde-wasm-bindgen",
|
||||||
|
"serde_json",
|
||||||
"serial_test",
|
"serial_test",
|
||||||
"sha3 0.8.2",
|
"sha3 0.8.2",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ dashu = "0.4.0"
|
|||||||
num-order = { version = "1.2.0" }
|
num-order = { version = "1.2.0" }
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
||||||
|
serde_json = "1.0.95"
|
||||||
|
serde = "1.0.159"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
libffi = { version = "3.2.0", optional = true }
|
libffi = { version = "3.2.0", optional = true }
|
||||||
hostname = { version = "0.3.1", optional = true }
|
hostname = { version = "0.3.1", optional = true }
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -455,6 +455,7 @@ mod tests {
|
|||||||
let blocks = code.split("=====");
|
let blocks = code.split("=====");
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
let mut last_result: Option<_> = None;
|
||||||
// Iterate over the blocks
|
// Iterate over the blocks
|
||||||
for block in blocks {
|
for block in blocks {
|
||||||
// Trim the block to remove any leading or trailing whitespace
|
// Trim the block to remove any leading or trailing whitespace
|
||||||
@@ -471,16 +472,24 @@ mod tests {
|
|||||||
println!("query #{}: {}", i, query);
|
println!("query #{}: {}", i, query);
|
||||||
// Parse and execute the query
|
// Parse and execute the query
|
||||||
let result = machine.run_query(query.to_string());
|
let result = machine.run_query(query.to_string());
|
||||||
|
|
||||||
assert!(result.is_ok());
|
|
||||||
|
|
||||||
// Print the result
|
// Print the result
|
||||||
println!("{:?}", result);
|
println!("{:?}", result);
|
||||||
|
assert!(result.is_ok());
|
||||||
|
|
||||||
|
last_result = Some(result);
|
||||||
} else if let Some(code) = block.strip_prefix("consult") {
|
} else if let Some(code) = block.strip_prefix("consult") {
|
||||||
println!("load code: {}", code);
|
println!("load code: {}", code);
|
||||||
|
|
||||||
// Load the code into the machine
|
// Load the code into the machine
|
||||||
machine.consult_module_string("facts", code.to_string());
|
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(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,79 @@ pub enum QueryResolution {
|
|||||||
Matches(Vec<QueryMatch>),
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct QueryMatch {
|
pub struct QueryMatch {
|
||||||
pub bindings: BTreeMap<String, Value>,
|
pub bindings: BTreeMap<String, Value>,
|
||||||
|
|||||||
Reference in New Issue
Block a user