From dd957ae1e26747be0579087e429fea641ce25655 Mon Sep 17 00:00:00 2001 From: AdrickTench Date: Fri, 6 Dec 2024 16:11:05 -0600 Subject: [PATCH] coerce tests and simple fixes --- prolog/metta_lang/metta_eval.pl | 3 +- .../coerce_tests.metta | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/baseline_compat/hyperon-mettalog_sanity/coerce_tests.metta diff --git a/prolog/metta_lang/metta_eval.pl b/prolog/metta_lang/metta_eval.pl index 3b93675b924..5ebd1de6e39 100755 --- a/prolog/metta_lang/metta_eval.pl +++ b/prolog/metta_lang/metta_eval.pl @@ -94,8 +94,6 @@ coerce('Bool',Value,Result):- Value=0, !, Result='False'. coerce('Bool',Value,Result):- Value='False', !, Result='False'. coerce('Bool',Value,Result):- is_list(Value), length(Value, 0), !, Result='False'. -coerce('Bool',Value,Result):- is_list(Value),!,as_tf(call_true(Value),Result), -set_list_value(Value,Result). coerce('Bool',Value,Result):- !, Result='True'. coerce('Number',Value,Result):- number(Value), !, Value=Result. @@ -104,6 +102,7 @@ coerce('Number',Value,Result):- Value='True', !, Result=1. coerce('Number',Value,Result):- atom(Value), !, atom_number(Value, Result). +coerce('String', Value, Result):- string(Value), !, Value=Result. coerce('String', Value, Result):- term_string(Value,Result). set_list_value(Value,Result):- nb_setarg(1,Value,echo),nb_setarg(1,Value,[Result]). diff --git a/tests/baseline_compat/hyperon-mettalog_sanity/coerce_tests.metta b/tests/baseline_compat/hyperon-mettalog_sanity/coerce_tests.metta new file mode 100644 index 00000000000..3e1d3e0c717 --- /dev/null +++ b/tests/baseline_compat/hyperon-mettalog_sanity/coerce_tests.metta @@ -0,0 +1,34 @@ +;; coerce is no-op when type already matches + +!(assertEqualToResult (coerce Bool True) (True)) +!(assertEqualToResult (coerce Bool False) (False)) +!(assertEqualToResult (coerce Number 1) (1)) +!(assertEqualToResult (coerce Number 1.5) (1.5)) +!(assertEqualToResult (coerce String "string") ("string")) + +;; coerce Bool makes 0 and () False, everything else True + +!(assertEqualToResult (coerce Bool 0) (False)) +!(assertEqualToResult (coerce Bool ()) (False)) +!(assertEqualToResult (coerce Bool 1) (True)) +!(assertEqualToResult (coerce Bool "string") (True)) +!(assertEqualToResult (coerce Bool (1 2 3)) (True)) + +;; coerce Number handles bools, number strings + +!(assertEqualToResult (coerce Number False) (0)) +!(assertEqualToResult (coerce Number True) (1)) +!(assertEqualToResult (coerce Number "1") (1)) +!(assertEqualToResult (coerce Number "1.5") (1.5)) +!(assertEqualToResult (coerce Number "2147483647") (2147483647)) +!(assertEqualToResult (coerce Number "2147483648") (2147483648)) +!(assertEqualToResult (coerce Number "not a number string") ()) + +;; coerce String acts as if printing object + +!(assertEqualToResult (coerce String False) ("'False'")) +!(assertEqualToResult (coerce String True) ("'True'")) +!(assertEqualToResult (coerce String 1) ("1")) +!(assertEqualToResult (coerce String 1.5) ("1.5")) +!(assertEqualToResult (coerce String ()) ("[]")) +!(assertEqualToResult (coerce String (1 2 3)) ("[1,2,3]"))