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:
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user