示例#1
0
def test_different_constraints_on_commutative_operation():
    c1 = CustomConstraint(lambda x: len(str(x)) > 1)
    c2 = CustomConstraint(lambda x: len(str(x)) == 1)
    pattern1 = Pattern(f_c(x_), c1)
    pattern2 = Pattern(f_c(x_), c2)
    pattern3 = Pattern(f_c(x_, b), c1)
    pattern4 = Pattern(f_c(x_, b), c2)
    matcher = ManyToOneMatcher(pattern1, pattern2, pattern3, pattern4)

    subject = f_c(a)
    results = list(matcher.match(subject))
    assert len(results) == 1
    assert results[0][0] == pattern2
    assert results[0][1] == {'x': a}

    subject = f_c(Symbol('longer'), b)
    results = sorted(matcher.match(subject))
    assert len(results) == 1
    assert results[0][0] == pattern3
    assert results[0][1] == {'x': Symbol('longer')}

    subject = f_c(a, b)
    results = list(matcher.match(subject))
    assert len(results) == 1
    assert results[0][0] == pattern4
    assert results[0][1] == {'x': a}
示例#2
0
def test_custom_constraint_repeated_renaming():
    c1 = CustomConstraint(lambda x1, y: True)
    assert c1.variables == {'x1', 'y'}
    c2 = c1.with_renamed_vars({'x1': 'x2'})
    assert c2.variables == {'x2', 'y'}
    c3 = c2.with_renamed_vars({'x2': 'x3'})
    assert c3.variables == {'x3', 'y'}
    c4 = c3.with_renamed_vars({'z1': 'z2'})
    assert c4.variables == {'x3', 'y'}
示例#3
0
def test_different_constraints_no_match_on_operation():
    c1 = CustomConstraint(lambda x: x == a)
    c2 = CustomConstraint(lambda x: x == b)
    pattern1 = Pattern(f(x_), c1)
    pattern2 = Pattern(f(x_), c2)
    matcher = ManyToOneMatcher(pattern1, pattern2)

    subject = f(c)
    results = list(matcher.match(subject))
    assert len(results) == 0
示例#4
0
def test_different_pattern_same_constraint(c1, c2):
    constr1 = CustomConstraint(lambda x: c1)
    constr2 = CustomConstraint(lambda x: c2)
    constr3 = CustomConstraint(lambda x: True)
    patterns = [
        Pattern(f2(x_, a), constr3),
        Pattern(f(a, a, x_), constr3),
        Pattern(f(a, x_), constr1),
        Pattern(f(x_, a), constr2),
        Pattern(f(a, x_, b), constr1),
        Pattern(f(x_, a, b), constr1),
    ]
    subject = f(a, a)

    matcher = ManyToOneMatcher(*patterns)
    results = list(matcher.match(subject))

    assert len(results) == int(c1) + int(c2)
示例#5
0
def test_custom_constraint_with_renamed_vars():
    actual_x = None
    actual_y = None

    def constraint(x, y):
        nonlocal actual_x
        nonlocal actual_y
        actual_x = x
        actual_y = y

        return x == y

    c1 = CustomConstraint(constraint)
    c2 = c1.with_renamed_vars({'x': 'z'})

    assert c2({'x': 1, 'z': 2, 'y': 1}) is False
    assert actual_x == 2
    assert actual_y == 1
    assert c2({'x': 1, 'z': 3, 'y': 3}) is True
    assert actual_x == 3
    assert actual_y == 3
示例#6
0
def test_custom_constraint_errors():
    with pytest.raises(ValueError):
        CustomConstraint(lambda *args: True)
    with pytest.raises(ValueError):
        CustomConstraint(lambda **kwargs: True)
示例#7
0
def test_custom_constraint_vars():
    c1 = CustomConstraint(lambda x, y: True)
    assert c1.variables == {'x', 'y'}
示例#8
0
    def __hash__(self):
        return id(self)


C_dummy1 = DummyConstraint()
C_dummy2 = DummyConstraint()

VARIABLE_CONSTRAINTS = [
    EqualVariablesConstraint(),
    EqualVariablesConstraint('x'),
    EqualVariablesConstraint('y'),
    EqualVariablesConstraint('x', 'y'),
]

C_custom1 = CustomConstraint(lambda x: x == 0)
C_custom2 = CustomConstraint(lambda y: y == 0)
C_custom3 = CustomConstraint(lambda x, y: x == y)

CUSTOM_CONSTRAINTS = [C_custom1, C_custom2, C_custom3]



@pytest.mark.parametrize(
    '   variables,      substitution,           expected_result',
    [
        (['x', 'y'],    {'x': 0, 'y': 0},       True),
        (['x', 'y'],    {'x': 0, 'y': 1},       False),
    ]
)  # yapf: disable
def test_equal_variables_constraint_call(variables, substitution,
示例#9
0
# -*- coding: utf-8 -*-
import pytest

from matchpy.expressions.constraints import CustomConstraint
from matchpy.expressions.expressions import Wildcard
from matchpy.matching._common import CommutativePatternsParts
from .common import *

constr1 = CustomConstraint(lambda x, y: x == y)
constr2 = CustomConstraint(lambda x, y: x != y)


class TestCommutativePatternsParts:
    @pytest.mark.parametrize(
        '   expressions,                    constant,           syntactic,      sequence_variables,     fixed_variables,        rest',
        [
            ([],                            [],                 [],             [],                     [],                     []),
            ([a],                           [a],                [],             [],                     [],                     []),
            ([a, b],                        [a, b],             [],             [],                     [],                     []),
            ([x_],                          [],                 [],             [],                     [('x', 1)],             []),
            ([x_, y_],                      [],                 [],             [],                     [('x', 1), ('y', 1)],   []),
          # ([x2],                          [],                 [],             [],                     [('x', 2)],             []),
            ([f(x_)],                       [],                 [f(x_)],        [],                     [],                     []),
            ([f(x_), f(y_)],                [],                 [f(x_), f(y_)], [],                     [],                     []),
            ([f(a)],                        [f(a)],             [],             [],                     [],                     []),
            ([f(x__)],                      [],                 [],             [],                     [],                     [f(x__)]),
            ([f(a), f(b)],                  [f(a), f(b)],       [],             [],                     [],                     []),
            ([x__],                         [],                 [],             [('x', 1)],             [],                     []),
            ([x___],                        [],                 [],             [('x', 0)],             [],                     []),
            ([x__, y___],                   [],                 [],             [('x', 1), ('y', 0)],   [],                     []),
            ([f_c(x_)],                     [],                 [],             [],                     [],                     [f_c(x_)]),