Exemple #1
0
def test_frozen():
    """
    FrozenDict is not modifyable
    """
    f = FrozenDict(a=1)
    with pytest.raises(ValueError):
        f['b'] = 5
Exemple #2
0
    def __init__(self,
                 tag_name,
                 attributes=None,
                 style=None,
                 children=None,
                 key=None,
                 schema=None):
        if isinstance(tag_name, dict) or isinstance(tag_name, list):
            # Backwards compatible interface
            warnings.warn('Passing dict to VDOM constructor is deprecated')
            value = tag_name
            vdom_obj = VDOM.from_dict(value)
            tag_name = vdom_obj.tag_name
            style = vdom_obj.style
            attributes = vdom_obj.attributes
            children = vdom_obj.children
            key = vdom_obj.key
        self.tag_name = tag_name
        # Sort attributes so our outputs are predictable
        self.attributes = FrozenDict(sorted(
            attributes.items())) if attributes else FrozenDict()
        self.children = tuple(children) if children else tuple()
        self.key = key
        # Sort attributes so our outputs are predictable
        self.style = FrozenDict(sorted(
            style.items())) if style else FrozenDict()

        # Validate that all children are VDOMs or strings
        if not all(
                isinstance(c, (VDOM, string_types[:])) for c in self.children):
            raise ValueError(
                'Children must be a list of VDOM objects or strings')

        # All style keys & values must be strings
        if not all(
                isinstance(k, string_types) and isinstance(v, string_types)
                for k, v in self.style.items()):
            raise ValueError('Style must be a dict with string keys & values')

        # mark completion of object creation. Object is immutable from now.
        self._frozen = True

        if schema is not None:
            self.validate(schema)
Exemple #3
0
    def __init__(self, tag_name, attributes=None, children=None, key=None, schema=None):
        if isinstance(tag_name, dict) or isinstance(tag_name, list):
            # Backwards compatible interface
            warnings.warn('Passing dict to VDOM constructor is deprecated')
            value = tag_name
            vdom_obj = VDOM.from_dict(value)
            tag_name = vdom_obj.tag_name
            attributes = vdom_obj.attributes
            children = vdom_obj.children
            key = vdom_obj.key
        self.tag_name = tag_name
        self.attributes = FrozenDict(attributes) if attributes else FrozenDict()
        self.children = tuple(children) if children else tuple()
        self.key = key

        # Validate that all children are VDOMs or strings
        if not all([isinstance(c, (VDOM, string_types[:])) for c in self.children]):
            raise ValueError('Children must be a list of VDOM objects or strings')

        # mark completion of object creation. Object is immutable from now.
        self._frozen = True

        if schema is not None:
            self.validate(schema)
Exemple #4
0
def test_ordering():
    """
    FrozenDict should preserve ordering
    """
    f = FrozenDict([('a', 1), ('b', 2)])
    assert list(f.items()) == [('a', 1), ('b', 2)]