Skip to content

Commit

Permalink
moving private doctests to pytest for testing_expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatera committed Oct 16, 2023
1 parent 67e9b85 commit 1679f29
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 95 deletions.
27 changes: 0 additions & 27 deletions mathics/builtin/testing_expressions/equality_inequality.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,6 @@ class BooleanQ(Builtin):
>> BooleanQ[1 < 2]
= True
#> BooleanQ["string"]
= False
#> BooleanQ[Together[x/y + y/x]]
= False
"""

rules = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -704,9 +696,6 @@ class Min(_MinMax):
With no arguments, 'Min' gives 'Infinity':
>> Min[]
= Infinity
#> Min[x]
= x
"""

sense = -1
Expand Down Expand Up @@ -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]}
Expand Down
25 changes: 0 additions & 25 deletions mathics/builtin/testing_expressions/list_oriented.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
23 changes: 0 additions & 23 deletions mathics/builtin/testing_expressions/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ class AnyTrue(_ManyTrue):
>> AnyTrue[{1, 4, 5}, EvenQ]
= True
#> AnyTrue[{}, EvenQ]
= False
"""

summary_text = "some of the elements are True"
Expand Down Expand Up @@ -175,9 +172,6 @@ class AllTrue(_ManyTrue):
>> AllTrue[{2, 4, 7}, EvenQ]
= False
#> AllTrue[{}, EvenQ]
= True
"""

summary_text = "all the elements are True"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -330,9 +320,6 @@ class NoneTrue(_ManyTrue):
>> NoneTrue[{1, 4, 5}, EvenQ]
= False
#> NoneTrue[{}, EvenQ]
= True
"""

summary_text = "all the elements are False"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions mathics/builtin/testing_expressions/numerical_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
137 changes: 137 additions & 0 deletions test/builtin/test_testing_expressions.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 1679f29

Please sign in to comment.