This commit is contained in:
Nicolas Luck
2024-01-29 18:44:20 +01:00
parent 9aacfff35d
commit 7bb9c00356
2 changed files with 28 additions and 49 deletions

View File

@@ -491,12 +491,8 @@ mod tests {
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(),
)
assert_eq!(last_result.to_string().trim(), result.to_string().trim(),)
}
}
}
}
@@ -548,21 +544,13 @@ mod tests {
),
);
let query =
String::from(r#"property_resolve(C, "isLiked"), subject_class("Todo", C)."#);
let query = String::from(r#"property_resolve(C, "isLiked"), subject_class("Todo", C)."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::False)
);
assert_eq!(output, Ok(QueryResolution::False));
let query =
String::from(r#"subject_class("Todo", C), property_resolve(C, "isLiked")."#);
let query = String::from(r#"subject_class("Todo", C), property_resolve(C, "isLiked")."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::False)
);
assert_eq!(output, Ok(QueryResolution::False));
}
#[test]
@@ -579,36 +567,20 @@ mod tests {
),
);
let query =
String::from(r#"a("true for a")."#);
let query = String::from(r#"a("true for a")."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::True)
);
assert_eq!(output, Ok(QueryResolution::True));
let query =
String::from(r#"a("true for a"), b("true for b")."#);
let query = String::from(r#"a("true for a"), b("true for b")."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::True)
);
assert_eq!(output, Ok(QueryResolution::True));
let query =
String::from(r#"a("true for b"), b("true for b")."#);
let query = String::from(r#"a("true for b"), b("true for b")."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::False)
);
assert_eq!(output, Ok(QueryResolution::False));
let query =
String::from(r#"a("true for a"), b("true for a")."#);
let query = String::from(r#"a("true for a"), b("true for a")."#);
let output = machine.run_query(query);
assert_eq!(
output,
Ok(QueryResolution::False)
);
assert_eq!(output, Ok(QueryResolution::False));
}
}

View File

@@ -19,19 +19,22 @@ pub fn prolog_value_to_json_tring(value: Value) -> String {
Value::Float(f) => format!("{}", f),
Value::Rational(r) => format!("{}", r),
Value::Atom(a) => format!("{}", a.as_str()),
Value::String(s) =>
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"))
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() {
@@ -64,7 +67,11 @@ fn prolog_match_to_json_string(query_match: &QueryMatch) -> String {
if i > 0 {
string_result.push_str(",");
}
string_result.push_str(&format!("\"{}\":{}", k, prolog_value_to_json_tring(v.clone())));
string_result.push_str(&format!(
"\"{}\":{}",
k,
prolog_value_to_json_tring(v.clone())
));
}
string_result.push_str("}");
string_result