diff --git a/fileformats/generic/tests/test_collection.py b/fileformats/generic/tests/test_collection.py index 2ba751d..59d3168 100644 --- a/fileformats/generic/tests/test_collection.py +++ b/fileformats/generic/tests/test_collection.py @@ -1,3 +1,4 @@ +import sys import typing as ty import pytest from fileformats.generic import Directory, DirectoryOf, SetOf @@ -70,3 +71,36 @@ def test_set_optional_contents(): sample_set = SetOf[ty.Optional[MyFormatGz]](my_format) assert sample_set.contents == [my_format] assert list(sample_set.required_paths()) == [my_format.fspath] + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher") +def test_directory_optional_contents_pep604(tmp_path): + my_format = MyFormatGz.sample(dest_dir=tmp_path) + sample_dir = DirectoryOf[MyFormatGz](tmp_path) + EncodedText.sample(dest_dir=tmp_path) + assert sample_dir.contents == [my_format] + + optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir) + assert optional_dir.contents == [my_format] + + your_format = YourFormat.sample(dest_dir=tmp_path) + optional_dir = DirectoryOf[MyFormatGz, YourFormat | None](sample_dir) + assert optional_dir.contents == [my_format, your_format] + + +@pytest.mark.skipif(sys.version_info < (3, 10), reason="requires python3.10 or higher") +def test_set_optional_contents_pep604(): + my_format = MyFormatGz.sample() + your_format = YourFormat.sample() + + sample_set = SetOf[MyFormatGz, YourFormat | None](my_format) + assert sample_set.contents == [my_format] + assert list(sample_set.required_paths()) == [my_format.fspath] + + sample_set = SetOf[MyFormatGz, YourFormat | None](my_format, your_format) + assert sample_set.contents == [my_format, your_format] + assert set(sample_set.required_paths()) == {my_format.fspath, your_format.fspath} + + sample_set = SetOf[MyFormatGz | None](my_format) + assert sample_set.contents == [my_format] + assert list(sample_set.required_paths()) == [my_format.fspath]