Repository: viur-framework/logics (language: python)

Issue:
Establishing a test suite
It would be great to have logics-py and logics-js being tested against a test-suite with, to ensure equal results in both implementations.


Discussion / hints:

Fix patch:
diff --git a/LICENSE b/LICENSE
index 05a77d0..b7b176c 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright © 2022 by Jan Max Meyer, Mausbrand Informationssysteme GmbH
+Copyright © 2023 by Jan Max Meyer, Mausbrand Informationssysteme GmbH
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index 2a93d9c..741ae10 100644
--- a/README.md
+++ b/README.md
@@ -53,7 +53,7 @@ Both packages are under recent development and not stable right now. They are ma
 Logics does look like Python, but it isn't Python!
 
 - Expressions can be used with arbitrary whitespace and line-breaks
-- There are no methods on objects, but functions that work on objects
+- There are no methods on objects, but functions that work on values
   - e.g. `dict.keys()` becomes `keys(dict)`
   - `";".join(["a", "b", "c"])` becomes `join(["a", "b", "c"], ";")`
 - No exceptions, access to e.g. invalid index or key just returns `None`
diff --git a/logics-py/logics/__init__.py b/logics-py/logics/__init__.py
index 6d07504..61c7d06 100644
--- a/logics-py/logics/__init__.py
+++ b/logics-py/logics/__init__.py
@@ -1,2 +1,3 @@
 from .logics import Logics
+from .value import Value
 from .parser import LogicsParseException as ParseException
diff --git a/logics-py/logics/value.py b/logics-py/logics/value.py
index 6901a9d..e28108b 100644
--- a/logics-py/logics/value.py
+++ b/logics-py/logics/value.py
@@ -130,10 +130,20 @@ def __bool__(self):
 		return bool(self.value)
 
 	def __int__(self):
-		return parse_int(self.value)
+		if self.type() == "str":
+			return parse_int(self.value)
+		elif self.value is None:
+			return 0
+
+		return int(self.value)
 
 	def __float__(self):
-		return parse_float(self.value)
+		if self.type() == "str":
+			return parse_float(self.value)
+		elif self.value is None:
+			return 0.0
+
+		return float(self.value)
 
 	def __len__(self):
 		if self.type() in ("dict", "list", "str"):
@@ -285,8 +295,3 @@ def __invert__(self):
 		return Value(~int(self))
 
 
-# print(Value({"a": 1} != Value({"a": 1})))
-# print(Value(4) == Value(4))
-# print(Value(4) == 2)
-# print(Value(4) + 4)
-# print(repr(Value("4112", optimize=True)))


Test patch:
diff --git a/logics-py/test.py b/logics-py/test.py
index aed7561..b808711 100644
--- a/logics-py/test.py
+++ b/logics-py/test.py
@@ -1,7 +1,57 @@
-import unittest, logics
+from logics import Logics, Value
+import unittest
+
+
+class ValueTestCase(unittest.TestCase):
+
+    def test_conversion(self):
+        self.assertTrue(Value({"a": 1} != Value({"a": 1})))
+        self.assertTrue(Value(4) == Value(4))
+        self.assertFalse(Value(4) == 2)
+        self.assertEqual(Value(4) + 4, 8)
+        self.assertEqual(repr(Value("4112", optimize=True)), "4112")
+
+    def test_none(self):
+        none = Value(None)
+        self.assertEqual(none, None)
+        self.assertEqual(bool(none), False)
+        self.assertEqual(int(none), 0)
+        self.assertEqual(float(none), 0.0)
+        self.assertEqual(repr(none), "None")
+
+    def test_bool(self):
+        true = Value(True)
+        false = Value(False)
+        self.assertEqual(true, True)
+        self.assertEqual(false, False)
+        self.assertEqual(true, 1)
+        self.assertEqual(false, 0)
+        self.assertEqual(int(true), 1)
+        self.assertEqual(int(false), 0)
+        self.assertEqual(repr(true), "True")
+        self.assertEqual(repr(false), "False")
+
+    def test_int(self):
+        _123 = Value(123)
+        self.assertEqual(_123, 123)
+        self.assertEqual(-_123, -123)
+        self.assertEqual(int(Value(" 123 xix")), 123)
+        self.assertEqual(int(Value(" -123 xix")), -123)
+        self.assertEqual(repr(_123), "123")
+        self.assertEqual(repr(-_123), "-123")
+
+    def test_float(self):
+        _1234 = Value(123.4)
+        self.assertEqual(Value(_1234), 123.4)
+        self.assertEqual(Value(-_1234), -123.4)
+        self.assertEqual(float(Value(" 123.4 xfx")), 123.4)
+        self.assertEqual(float(Value(" -123.4 xfx")), -123.4)
+        self.assertEqual(repr(_1234), "123.4")
+        self.assertEqual(repr(-_1234), "-123.4")
+
 
 class LogicsTestCase(unittest.TestCase):
     def test(self):
-        l = logics.Logics("123")
+        l = Logics("123")
         res = l.run()
         self.assertEqual(res, 123)