2021-12-27 08:29:09 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-05-18 05:02:06 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
import attr
|
|
|
|
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
class TestPatternMatching:
|
2021-05-18 05:02:06 +00:00
|
|
|
"""
|
|
|
|
Pattern matching syntax test cases.
|
|
|
|
"""
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
@pytest.mark.parametrize("dec", [attr.s, attr.define, attr.frozen])
|
|
|
|
def test_simple_match_case(self, dec):
|
2021-05-18 05:02:06 +00:00
|
|
|
"""
|
2021-05-18 09:29:29 +00:00
|
|
|
Simple match case statement works as expected with all class
|
|
|
|
decorators.
|
2021-05-18 05:02:06 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
@dec
|
2022-03-21 07:47:47 +00:00
|
|
|
class C:
|
2021-05-18 05:02:06 +00:00
|
|
|
a = attr.ib()
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
assert ("a",) == C.__match_args__
|
2021-05-18 05:02:06 +00:00
|
|
|
|
|
|
|
matched = False
|
|
|
|
c = C(a=1)
|
|
|
|
match c:
|
|
|
|
case C(a):
|
|
|
|
matched = True
|
|
|
|
|
|
|
|
assert matched
|
2021-12-27 08:32:19 +00:00
|
|
|
assert 1 == a
|
2021-05-18 05:02:06 +00:00
|
|
|
|
|
|
|
def test_explicit_match_args(self):
|
|
|
|
"""
|
2021-05-18 09:29:29 +00:00
|
|
|
Does not overwrite a manually set empty __match_args__.
|
2021-05-18 05:02:06 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
ma = ()
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
@attr.define
|
|
|
|
class C:
|
|
|
|
a = attr.field()
|
2021-05-18 05:02:06 +00:00
|
|
|
__match_args__ = ma
|
|
|
|
|
|
|
|
c = C(a=1)
|
|
|
|
|
|
|
|
msg = r"C\(\) accepts 0 positional sub-patterns \(1 given\)"
|
|
|
|
with pytest.raises(TypeError, match=msg):
|
|
|
|
match c:
|
2021-12-27 08:32:19 +00:00
|
|
|
case C(_):
|
2021-05-18 05:02:06 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_match_args_kw_only(self):
|
|
|
|
"""
|
2021-05-18 09:29:29 +00:00
|
|
|
kw_only classes don't generate __match_args__.
|
|
|
|
kw_only fields are not included in __match_args__.
|
2021-05-18 05:02:06 +00:00
|
|
|
"""
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
@attr.define
|
|
|
|
class C:
|
|
|
|
a = attr.field(kw_only=True)
|
|
|
|
b = attr.field()
|
2021-05-18 05:02:06 +00:00
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
assert ("b",) == C.__match_args__
|
2021-05-18 05:02:06 +00:00
|
|
|
|
|
|
|
c = C(a=1, b=1)
|
|
|
|
msg = r"C\(\) accepts 1 positional sub-pattern \(2 given\)"
|
|
|
|
with pytest.raises(TypeError, match=msg):
|
|
|
|
match c:
|
|
|
|
case C(a, b):
|
|
|
|
pass
|
|
|
|
|
|
|
|
found = False
|
|
|
|
match c:
|
|
|
|
case C(b, a=a):
|
|
|
|
found = True
|
|
|
|
|
|
|
|
assert found
|
|
|
|
|
2021-05-18 09:29:29 +00:00
|
|
|
@attr.define(kw_only=True)
|
|
|
|
class C:
|
|
|
|
a = attr.field()
|
|
|
|
b = attr.field()
|
2021-05-18 05:02:06 +00:00
|
|
|
|
|
|
|
c = C(a=1, b=1)
|
|
|
|
msg = r"C\(\) accepts 0 positional sub-patterns \(2 given\)"
|
|
|
|
with pytest.raises(TypeError, match=msg):
|
|
|
|
match c:
|
|
|
|
case C(a, b):
|
|
|
|
pass
|
|
|
|
|
|
|
|
found = False
|
|
|
|
match c:
|
|
|
|
case C(a=a, b=b):
|
|
|
|
found = True
|
|
|
|
|
|
|
|
assert found
|
2021-12-27 08:32:19 +00:00
|
|
|
assert (1, 1) == (a, b)
|