From 4495bc40c46fde2bc1c8bd54651c47b7b95165f0 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Wed, 1 May 2024 12:58:31 +0200 Subject: [PATCH] #308 - Make type system iterable - Implement iter method in type system --- cassis/typesystem.py | 3 +++ tests/test_typesystem.py | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/cassis/typesystem.py b/cassis/typesystem.py index ba99f6f..cbc5be3 100644 --- a/cassis/typesystem.py +++ b/cassis/typesystem.py @@ -838,6 +838,9 @@ def __init__(self, add_document_annotation_type: bool = True): if add_document_annotation_type: self._add_document_annotation_type() + def __iter__(self): + return self.get_types() + def contains_type(self, typename: str): """Checks whether this type system contains a type with name `typename`. diff --git a/tests/test_typesystem.py b/tests/test_typesystem.py index 1afc3b7..cc3cfcd 100644 --- a/tests/test_typesystem.py +++ b/tests/test_typesystem.py @@ -511,6 +511,52 @@ def test_is_array(): assert cas.typesystem.is_array(type.name) == type.name.endswith("Array") +def test_get_types(): + cas = Cas() + + assert {type.name for type in cas.typesystem} == {"uima.tcas.DocumentAnnotation"} + assert {type.name for type in cas.typesystem.get_types(built_in=False)} == {"uima.tcas.DocumentAnnotation"} + assert {type.name for type in cas.typesystem.get_types(built_in=True)} == { + "uima.cas.TOP", + "uima.cas.NULL", + "uima.cas.Boolean", + "uima.cas.Byte", + "uima.cas.Short", + "uima.cas.Integer", + "uima.cas.Long", + "uima.cas.Float", + "uima.cas.Double", + "uima.cas.String", + "uima.cas.ArrayBase", + "uima.cas.FSArray", + "uima.cas.BooleanArray", + "uima.cas.ByteArray", + "uima.cas.ShortArray", + "uima.cas.LongArray", + "uima.cas.DoubleArray", + "uima.cas.FloatArray", + "uima.cas.IntegerArray", + "uima.cas.StringArray", + "uima.cas.ListBase", + "uima.cas.FSList", + "uima.cas.EmptyFSList", + "uima.cas.NonEmptyFSList", + "uima.cas.FloatList", + "uima.cas.EmptyFloatList", + "uima.cas.NonEmptyFloatList", + "uima.cas.IntegerList", + "uima.cas.EmptyIntegerList", + "uima.cas.NonEmptyIntegerList", + "uima.cas.StringList", + "uima.cas.EmptyStringList", + "uima.cas.NonEmptyStringList", + "uima.cas.Sofa", + "uima.cas.AnnotationBase", + "uima.tcas.Annotation", + "uima.tcas.DocumentAnnotation", + } + + @pytest.mark.parametrize( "parent_name, child_name, expected", [