Skip to content
Snippets Groups Projects
Select Git revision
  • cebb493d6a662a2d7060fe4f0f1e83e9bb8d9ca2
  • parser default protected
2 results

test_deserializable.py

Blame
  • Hugo Hörnquist's avatar
    Hugo Hörnquist authored
    Extend DocStringTag to include "all" known types, along with document
    how each of them behaves. This also forced a few changes to its
    behaviour, which is reflected in the updated tests. The biggest change
    is that we now barely test for valid "tag_name" values, since a valid
    tag should always have a valid (albeit unknown) tag name.
    4d8814c7
    History
    test_deserializable.py 1.49 KiB
    import pytest
    from muppet.puppet.strings import (
        DocStringTag,
        DocStringParamTag,
        DocString,
        DataTypeAlias,
    )
    
    
    def test_deserialize_from_json():
        """
        Test deserializing of tag objects.
    
        Of note here is that DocStringTag have many subclasses, and should
        dynamically find the correct one based on 'tag_name'.
    
        Calling ``.to_json()`` directly on a `DocStringTag` (or on of its
        subclasses) is ratnher uninteresting, since the dynamic choice
        isn't available there.
        """
        data = {
            'tag_name': 'param',
            'text': 'Contents of tagname',
            'types': ['String'],
            'name': 'x',
        }
    
        assert [DocStringParamTag(**data)] \
            == DocString.from_json({
                'text': '',
                'tags': [data],
                }).tags
    
    
    def test_deserialize_nested():
        """
        Test a nested parse.
    
        This test is slightly redundant.
        """
        text = 'A'
        tn = 'B'
        ttext = 'C'
    
        assert DocString(text=text,
                         tags=[DocStringTag(tag_name=tn,
                                            text=ttext)]) \
            == DocString.from_json({
                'text': text,
                'tags': [
                    {
                        'tag_name': tn,
                        'text': ttext,
                        }
                    ]
                })
    
    
    def test_deserialize_from_json_error():
        with pytest.raises(AssertionError):
            DocString.from_json({
                'text': 'text',
                'tags': [
                    'no',
                ]
            })