Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 / Fix issues with pattern matching for Rubi #1176

Merged
merged 10 commits into from
Nov 23, 2024
51 changes: 51 additions & 0 deletions test/core/test_patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
"""
Unit tests for mathics pattern matching
"""

import sys
import time
from test.helper import check_evaluation, evaluate

import pytest

@pytest.mark.parametrize(
("str_expr", "msgs", "str_expected", "fail_msg"),
[
# Two default arguments (linear)
("MatchQ[1, a_.+b_.*x_]",None,"True",None),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that would be interesting is to check MatchQ over expressions of the form
A[x_.,B[y_.*z_]]
and check the match with A[1,B[2,y]], A[1,B[y,2]], A[B[y,2],1] for different settings of the attributes of A and B.

Another group of tests should check if variables are properly assigned. To do that, we should test rules using these patterns. For example,

rule=A[x_.,B[y_.*z_]]->{x,y,z}

and then apply it to the previous cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree! This makes sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add them as soon as I have all your and rocky's comments.

Copy link
Member

@rocky rocky Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see more/some unit tests for mathics.core.pattern as its own unit tests, e.g. (not via MatchQ). Other than that, I don't have any further comments.

When everything is in place, I'd like to look over.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see more/some unit tests for mathics.core.pattern as its own unit tests, e.g. (not via MatchQ). Other than that, I don't have any further comments.

When everything is in place, I'd like to look over.

I guess it's always gonna be via MatchQ or rules, as mmatera suggested. Alternate ideas are welcome!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please feel free to add commits to this PR. I think I've enabled the option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see more/some unit tests for mathics.core.pattern as its own unit tests, e.g. (not via MatchQ). Other than that, I don't have any further comments.
When everything is in place, I'd like to look over.

I guess it's always gonna be via MatchQ or rules, as mmatera suggested. Alternate ideas are welcome!

At some point, we should try to add tests at a lower level (using mathics.eval.patterns.match or directly testing the subclasses of mathics.core.pattern.Pattern) that avoids passing through the evaluation loop. But for now I think this is a good starting point.

("MatchQ[x, a_.+b_.*x_]",None,"True",None),
("MatchQ[2*x, a_.+b_.*x_]",None,"True",None),
("MatchQ[1+x, a_.+b_.*x_]",None,"True",None),
("MatchQ[1+2*x, a_.+b_.*x_]",None,"True",None),
# Default argument (power)
("MatchQ[1, x_^m_.]",None,"True",None),
("MatchQ[x, x_^m_.]",None,"True",None),
("MatchQ[x^1, x_^m_.]",None,"True",None),
("MatchQ[x^2, x_^m_.]",None,"True",None),
# Two default arguments (power)
("MatchQ[1, x_.^m_.]",None,"True",None),
("MatchQ[x, x_.^m_.]",None,"True",None),
("MatchQ[x^1, x_.^m_.]",None,"True",None),
("MatchQ[x^2, x_.^m_.]",None,"True",None),
# Two default arguments (no non-head)
("MatchQ[1, a_.+b_.]",None,"True",None),
("MatchQ[x, a_.+b_.]",None,"True",None),
("MatchQ[1+x, a_.+b_.]",None,"True",None),
("MatchQ[1+2*x, a_.+b_.]",None,"True",None),
("MatchQ[1, a_.+b_.]",None,"True",None),
("MatchQ[x, a_.*b_.]",None,"True",None),
("MatchQ[2*x, a_.*b_.]",None,"True",None),
],
)
def test_patterns(str_expr, msgs, str_expected, fail_msg):
"""patterns"""
check_evaluation(
str_expr,
str_expected,
to_string_expr=True,
to_string_expected=True,
hold_expected=True,
failure_message=fail_msg,
expected_messages=msgs,
)
Loading