Skip to content

Commit

Permalink
coerce tests and simple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrickTench authored and TeamSPoon committed Dec 8, 2024
1 parent 6b85076 commit dd957ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 1 addition & 2 deletions prolog/metta_lang/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]).
Expand Down
34 changes: 34 additions & 0 deletions tests/baseline_compat/hyperon-mettalog_sanity/coerce_tests.metta
Original file line number Diff line number Diff line change
@@ -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]"))

0 comments on commit dd957ae

Please sign in to comment.