def test_custom_type():

    class ShoutType(StringType):
        def to_primitive(self, value, context):
            return StringType.convert(self, value) + '!!!'

    class QuestionType(StringType):
        def to_primitive(self, value, context):
            return StringType.convert(self, value) + '???'

    class FancyStringType(UnionType):
        types = (ShoutType, QuestionType)
        def resolve(self, value, context):
            if value.endswith('?'):
                return QuestionType
            elif value.endswith('!'):
                return ShoutType

    assert FancyStringType().to_primitive("Hello, world!") == "Hello, world!!!!"
    assert FancyStringType().to_primitive("Who's a good boy?") == "Who's a good boy????"

    def resolve(value, context):
        if value.endswith('?'):
            return QuestionType
        elif value.endswith('!'):
            return ShoutType

    assert UnionType((ShoutType, QuestionType), resolver=resolve).to_primitive("Hello, world!") \
        == "Hello, world!!!!"

    with pytest.raises(ValidationError):
        FancyStringType(max_length=20).validate("What is the meaning of life?")
dataset/ETHPy150Open schematics/schematics/tests/test_union_type.py/test_custom_type/original