]> Repositorios git - scryer-prolog.git/commitdiff
Don’t include unbound variables in results returned from run_query
authorNicolas Luck <[email protected]>
Mon, 4 Dec 2023 19:10:06 +0000 (20:10 +0100)
committerNicolas Luck <[email protected]>
Mon, 4 Dec 2023 19:10:06 +0000 (20:10 +0100)
src/machine/lib_machine.rs

index b0cc880f76d4f395898957d3165f23b2de3ded29..9d166c51818a5378ccd4623ebe43ca41b367175e 100644 (file)
@@ -196,7 +196,9 @@ impl Machine {
                 let output: String = outputter.result();
                 // println!("Result: {} = {}", var_key.to_string(), output);
 
-                bindings.insert(var_key.to_string(), Value::try_from(output).expect("asdfs"));
+                if var_key.to_string() != output {
+                    bindings.insert(var_key.to_string(), Value::try_from(output).expect("Couldn't convert Houtput to Value"));
+                }   
             }
 
             matches.push(QueryResolutionLine::Match(bindings));
@@ -338,6 +340,24 @@ mod tests {
         );
     }
 
+    #[test]
+    fn empty_predicate() {
+        let mut machine = Machine::new_lib();
+        machine.load_module_string(
+            "facts",
+                r#"
+                :- discontiguous(subject_class/2).
+            "#.to_string());
+
+        let result = machine.run_query(String::from(
+            "subject_class(X, _).",
+        ));
+        assert_eq!(
+            result,
+            Ok(QueryResolution::True)
+        );
+    }
+
     #[test]
     fn list_results() {
         let mut machine = Machine::new_lib();
@@ -486,14 +506,12 @@ mod tests {
             output,
             Ok(QueryResolution::Matches(vec![QueryMatch::from(
                 btreemap! {
-                    "Predicate" => Value::from("Predicate"),
                     "Result" => Value::List(
                         Vec::from([
                             Value::List([Value::from("p1"), Value::from("b")].into()),
                             Value::List([Value::from("p2"), Value::from("b")].into()),
                         ])
                     ),
-                    "Target" => Value::from("Target"),
                 }
             ),]))
         );