Skip to content

Commit 8010f0f

Browse files
authored
Merge pull request #87 from ArcanaFramework/pep604-union-types
added tests to check pep604 union types in typed collections
2 parents 85fc309 + a3b428a commit 8010f0f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

fileformats/generic/tests/test_collection.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import typing as ty
23
import pytest
34
from fileformats.generic import Directory, DirectoryOf, SetOf
@@ -70,3 +71,36 @@ def test_set_optional_contents():
7071
sample_set = SetOf[ty.Optional[MyFormatGz]](my_format)
7172
assert sample_set.contents == [my_format]
7273
assert list(sample_set.required_paths()) == [my_format.fspath]
74+
75+
76+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
77+
def test_directory_optional_contents_pep604(tmp_path):
78+
my_format = MyFormatGz.sample(dest_dir=tmp_path)
79+
sample_dir = DirectoryOf[MyFormatGz](tmp_path)
80+
EncodedText.sample(dest_dir=tmp_path)
81+
assert sample_dir.contents == [my_format]
82+
83+
optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir)
84+
assert optional_dir.contents == [my_format]
85+
86+
your_format = YourFormat.sample(dest_dir=tmp_path)
87+
optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir)
88+
assert optional_dir.contents == [my_format, your_format]
89+
90+
91+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher")
92+
def test_set_optional_contents_pep604():
93+
my_format = MyFormatGz.sample()
94+
your_format = YourFormat.sample()
95+
96+
sample_set = SetOf[MyFormatGz, YourFormat | None](my_format)
97+
assert sample_set.contents == [my_format]
98+
assert list(sample_set.required_paths()) == [my_format.fspath]
99+
100+
sample_set = SetOf[MyFormatGz, YourFormat | None](my_format, your_format)
101+
assert sample_set.contents == [my_format, your_format]
102+
assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath}
103+
104+
sample_set = SetOf[MyFormatGz | None](my_format)
105+
assert sample_set.contents == [my_format]
106+
assert list(sample_set.required_paths()) == [my_format.fspath]

0 commit comments

Comments
 (0)