summaryrefslogtreecommitdiffstats
path: root/tests/oneof_pattern_matching.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-29 09:40:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-29 09:40:12 +0000
commit14b40ec77a4bf8605789cc3aff0eb87625510a41 (patch)
tree4064d27144d6deaabfcd96df01bd996baa8b51a0 /tests/oneof_pattern_matching.py
parentInitial commit. (diff)
downloadpython-aristaproto-upstream.tar.xz
python-aristaproto-upstream.zip
Adding upstream version 1.2+20240521.upstream/1.2+20240521upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/oneof_pattern_matching.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/oneof_pattern_matching.py b/tests/oneof_pattern_matching.py
new file mode 100644
index 0000000..2c5e797
--- /dev/null
+++ b/tests/oneof_pattern_matching.py
@@ -0,0 +1,46 @@
+from dataclasses import dataclass
+
+import pytest
+
+import aristaproto
+
+
+def test_oneof_pattern_matching():
+ @dataclass
+ class Sub(aristaproto.Message):
+ val: int = aristaproto.int32_field(1)
+
+ @dataclass
+ class Foo(aristaproto.Message):
+ bar: int = aristaproto.int32_field(1, group="group1")
+ baz: str = aristaproto.string_field(2, group="group1")
+ sub: Sub = aristaproto.message_field(3, group="group2")
+ abc: str = aristaproto.string_field(4, group="group2")
+
+ foo = Foo(baz="test1", abc="test2")
+
+ match foo:
+ case Foo(bar=_):
+ pytest.fail("Matched 'bar' instead of 'baz'")
+ case Foo(baz=v):
+ assert v == "test1"
+ case _:
+ pytest.fail("Matched neither 'bar' nor 'baz'")
+
+ match foo:
+ case Foo(sub=_):
+ pytest.fail("Matched 'sub' instead of 'abc'")
+ case Foo(abc=v):
+ assert v == "test2"
+ case _:
+ pytest.fail("Matched neither 'sub' nor 'abc'")
+
+ foo.sub = Sub(val=1)
+
+ match foo:
+ case Foo(sub=Sub(val=v)):
+ assert v == 1
+ case Foo(abc=v):
+ pytest.fail("Matched 'abc' instead of 'sub'")
+ case _:
+ pytest.fail("Matched neither 'sub' nor 'abc'")