From 1679f296f9925f3d1323ce02686fef63557fb7bd Mon Sep 17 00:00:00 2001 From: mmatera Date: Sun, 15 Oct 2023 23:07:04 -0300 Subject: [PATCH] moving private doctests to pytest for testing_expressions --- .../equality_inequality.py | 27 ---- .../testing_expressions/list_oriented.py | 25 ---- mathics/builtin/testing_expressions/logic.py | 23 --- .../numerical_properties.py | 20 --- test/builtin/test_testing_expressions.py | 137 ++++++++++++++++++ 5 files changed, 137 insertions(+), 95 deletions(-) create mode 100644 test/builtin/test_testing_expressions.py diff --git a/mathics/builtin/testing_expressions/equality_inequality.py b/mathics/builtin/testing_expressions/equality_inequality.py index eca27a151..bf339dcc7 100644 --- a/mathics/builtin/testing_expressions/equality_inequality.py +++ b/mathics/builtin/testing_expressions/equality_inequality.py @@ -337,12 +337,6 @@ class BooleanQ(Builtin): >> BooleanQ[1 < 2] = True - - #> BooleanQ["string"] - = False - - #> BooleanQ[Together[x/y + y/x]] - = False """ rules = { @@ -668,8 +662,6 @@ class Max(_MinMax): 'Max' does not compare strings or symbols: >> Max[-1.37, 2, "a", b] = Max[2, a, b] - #> Max[x] - = x """ sense = 1 @@ -704,9 +696,6 @@ class Min(_MinMax): With no arguments, 'Min' gives 'Infinity': >> Min[] = Infinity - - #> Min[x] - = x """ sense = -1 @@ -850,22 +839,6 @@ class Unequal(_EqualityOperator, _SympyComparison): >> "a" != "a" = False - #> Pi != N[Pi] - = False - - #> a_ != b_ - = a_ != b_ - - #> Clear[a, b]; - #> a != a != a - = False - #> "abc" != "def" != "abc" - = False - - ## Reproduce strange MMA behaviour - #> a != b != a - = a != b != a - 'Unequal' using an empty parameter or list, or a list with one element is True. This is the same as 'Equal". >> {Unequal[], Unequal[x], Unequal[1]} diff --git a/mathics/builtin/testing_expressions/list_oriented.py b/mathics/builtin/testing_expressions/list_oriented.py index 2032f8961..b99fe2040 100644 --- a/mathics/builtin/testing_expressions/list_oriented.py +++ b/mathics/builtin/testing_expressions/list_oriented.py @@ -298,31 +298,6 @@ class SubsetQ(Builtin): Every list is a subset of itself: >> SubsetQ[{1, 2, 3}, {1, 2, 3}] = True - - #> SubsetQ[{1, 2, 3}, {0, 1}] - = False - - #> SubsetQ[{1, 2, 3}, {1, 2, 3, 4}] - = False - - #> SubsetQ[{1, 2, 3}] - : SubsetQ called with 1 argument; 2 arguments are expected. - = SubsetQ[{1, 2, 3}] - - #> SubsetQ[{1, 2, 3}, {1, 2}, {3}] - : SubsetQ called with 3 arguments; 2 arguments are expected. - = SubsetQ[{1, 2, 3}, {1, 2}, {3}] - - #> SubsetQ[a + b + c, {1}] - : Heads Plus and List at positions 1 and 2 are expected to be the same. - = SubsetQ[a + b + c, {1}] - - #> SubsetQ[{1, 2, 3}, n] - : Nonatomic expression expected at position 2 in SubsetQ[{1, 2, 3}, n]. - = SubsetQ[{1, 2, 3}, n] - - #> SubsetQ[f[a, b, c], f[a]] - = True """ messages = { diff --git a/mathics/builtin/testing_expressions/logic.py b/mathics/builtin/testing_expressions/logic.py index f7809ebe0..9ffc80118 100644 --- a/mathics/builtin/testing_expressions/logic.py +++ b/mathics/builtin/testing_expressions/logic.py @@ -142,9 +142,6 @@ class AnyTrue(_ManyTrue): >> AnyTrue[{1, 4, 5}, EvenQ] = True - - #> AnyTrue[{}, EvenQ] - = False """ summary_text = "some of the elements are True" @@ -175,9 +172,6 @@ class AllTrue(_ManyTrue): >> AllTrue[{2, 4, 7}, EvenQ] = False - - #> AllTrue[{}, EvenQ] - = True """ summary_text = "all the elements are True" @@ -214,10 +208,6 @@ class Equivalent(BinaryOperator): Otherwise, 'Equivalent' returns a result in DNF >> Equivalent[a, b, True, c] = a && b && c - #> Equivalent[] - = True - #> Equivalent[a] - = True """ attributes = A_ORDERLESS | A_PROTECTED @@ -330,9 +320,6 @@ class NoneTrue(_ManyTrue): >> NoneTrue[{1, 4, 5}, EvenQ] = False - - #> NoneTrue[{}, EvenQ] - = True """ summary_text = "all the elements are False" @@ -505,16 +492,6 @@ class Xor(BinaryOperator): returns a result in symbolic form: >> Xor[a, False, b] = a \\[Xor] b - #> Xor[] - = False - #> Xor[a] - = a - #> Xor[False] - = False - #> Xor[True] - = True - #> Xor[a, b] - = a \\[Xor] b """ attributes = A_FLAT | A_ONE_IDENTITY | A_ORDERLESS | A_PROTECTED diff --git a/mathics/builtin/testing_expressions/numerical_properties.py b/mathics/builtin/testing_expressions/numerical_properties.py index 63cb07a37..ce16aa491 100644 --- a/mathics/builtin/testing_expressions/numerical_properties.py +++ b/mathics/builtin/testing_expressions/numerical_properties.py @@ -223,10 +223,6 @@ class MachineNumberQ(Test): = True >> MachineNumberQ[2.71828182845904524 + 3.14159265358979324 I] = False - #> MachineNumberQ[1.5 + 3.14159265358979324 I] - = True - #> MachineNumberQ[1.5 + 5 I] - = True """ summary_text = "test if expression is a machine precision real or complex number" @@ -253,10 +249,6 @@ class Negative(Builtin): = False >> Negative[a + b] = Negative[a + b] - #> Negative[-E] - = True - #> Negative[Sin[{11, 14}]] - = {True, False} """ attributes = A_LISTABLE | A_PROTECTED @@ -506,13 +498,6 @@ class Positive(Builtin): = False >> Positive[1 + 2 I] = False - - #> Positive[Pi] - = True - #> Positive[x] - = Positive[x] - #> Positive[Sin[{11, 14}]] - = {False, True} """ attributes = A_LISTABLE | A_PROTECTED @@ -547,11 +532,6 @@ class PrimeQ(SympyFunction): >> PrimeQ[2 ^ 127 - 1] = True - #> PrimeQ[1] - = False - #> PrimeQ[2 ^ 255 - 1] - = False - All prime numbers between 1 and 100: >> Select[Range[100], PrimeQ] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97} diff --git a/test/builtin/test_testing_expressions.py b/test/builtin/test_testing_expressions.py new file mode 100644 index 000000000..fa4c1cfa4 --- /dev/null +++ b/test/builtin/test_testing_expressions.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +""" +Unit tests for mathics.builtin.testing_expressions +""" + +import sys +import time +from test.helper import check_evaluation, evaluate + +import pytest + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ("AnyTrue[{}, EvenQ]", None, "False", None), + ("AllTrue[{}, EvenQ]", None, "True", None), + ("Equivalent[]", None, "True", None), + ("Equivalent[a]", None, "True", None), + ("NoneTrue[{}, EvenQ]", None, "True", None), + ("Xor[]", None, "False", None), + ("Xor[a]", None, "a", None), + ("Xor[False]", None, "False", None), + ("Xor[True]", None, "True", None), + ("Xor[a, b]", None, "a \\[Xor] b", None), + ], +) +def test_private_doctests_logic(str_expr, msgs, str_expected, fail_msg): + """text_expressions.logic""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ("SubsetQ[{1, 2, 3}, {0, 1}]", None, "False", None), + ("SubsetQ[{1, 2, 3}, {1, 2, 3, 4}]", None, "False", None), + ( + "SubsetQ[{1, 2, 3}]", + ("SubsetQ called with 1 argument; 2 arguments are expected.",), + "SubsetQ[{1, 2, 3}]", + None, + ), + ( + "SubsetQ[{1, 2, 3}, {1, 2}, {3}]", + ("SubsetQ called with 3 arguments; 2 arguments are expected.",), + "SubsetQ[{1, 2, 3}, {1, 2}, {3}]", + None, + ), + ( + "SubsetQ[a + b + c, {1}]", + ("Heads Plus and List at positions 1 and 2 are expected to be the same.",), + "SubsetQ[a + b + c, {1}]", + None, + ), + ( + "SubsetQ[{1, 2, 3}, n]", + ("Nonatomic expression expected at position 2 in SubsetQ[{1, 2, 3}, n].",), + "SubsetQ[{1, 2, 3}, n]", + None, + ), + ("SubsetQ[f[a, b, c], f[a]]", None, "True", None), + ], +) +def test_private_doctests_list_oriented(str_expr, msgs, str_expected, fail_msg): + """text_expressions.logic""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ('BooleanQ["string"]', None, "False", None), + ("BooleanQ[Together[x/y + y/x]]", None, "False", None), + ("Max[x]", None, "x", None), + ("Min[x]", None, "x", None), + ("Pi != N[Pi]", None, "False", None), + ("a_ != b_", None, "a_ != b_", None), + ("Clear[a, b];a != a != a", None, "False", None), + ('"abc" != "def" != "abc"', None, "False", None), + ("a != b != a", None, "a != b != a", "Reproduce strange MMA behaviour"), + ], +) +def test_private_doctests_equality_inequality(str_expr, msgs, str_expected, fail_msg): + """text_expressions.logic""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + ) + + +@pytest.mark.parametrize( + ("str_expr", "msgs", "str_expected", "fail_msg"), + [ + ("MachineNumberQ[1.5 + 3.14159265358979324 I]", None, "True", None), + ("MachineNumberQ[1.5 + 5 I]", None, "True", None), + ("Negative[-E]", None, "True", None), + ("Negative[Sin[{11, 14}]]", None, "{True, False}", None), + ("Positive[Pi]", None, "True", None), + ("Positive[x]", None, "Positive[x]", None), + ("Positive[Sin[{11, 14}]]", None, "{False, True}", None), + ("PrimeQ[1]", None, "False", None), + ("PrimeQ[2 ^ 255 - 1]", None, "False", None), + ], +) +def test_private_doctests_numerical_properties(str_expr, msgs, str_expected, fail_msg): + """text_expressions.numerical_properties""" + check_evaluation( + str_expr, + str_expected, + to_string_expr=True, + to_string_expected=True, + hold_expected=True, + failure_message=fail_msg, + expected_messages=msgs, + )