Ejemplo n.º 1
0
    def test_read_ruby_style_attribute_dict(self):
        # empty dict
        stream = Stream("{}><")
        assert dict(read_attribute_dict(stream, Compiler())) == {}
        assert stream.text[stream.ptr:] == '><'

        # string values
        assert dict(self._parse("{'class': 'test'} =Test")) == {
            'class': 'test'
        }
        assert dict(self._parse("{'class': 'test', 'id': 'something'}")) == {
            'class': 'test',
            'id': 'something'
        }

        # integer values
        assert dict(self._parse("{'data-number': 0}")) == {'data-number': '0'}
        assert dict(self._parse("{'data-number': 12345}")) == {
            'data-number': '12345'
        }

        # float values
        assert dict(self._parse("{'data-number': 123.456}")) == {
            'data-number': '123.456'
        }
        assert dict(self._parse("{'data-number': 0.001}")) == {
            'data-number': '0.001'
        }

        # None value
        assert dict(self._parse("{'controls': None}")) == {'controls': None}

        # boolean attributes
        assert dict(
            self._parse(
                "{disabled, class:'test', data-number : 123,\n foo:\"bar\"}")
        ) == {
            'disabled': True,
            'class': 'test',
            'data-number': '123',
            'foo': 'bar'
        }

        assert dict(
            self._parse(
                "{class:'test', data-number : 123,\n foo:\"bar\",  \t   disabled}"
            )) == {
                'disabled': True,
                'class': 'test',
                'data-number': '123',
                'foo': 'bar'
            }

        # attribute name has colon
        assert dict(self._parse("{'xml:lang': 'en'}")) == {'xml:lang': 'en'}

        # attribute value has colon or commas
        assert dict(self._parse("{'lang': 'en:g'}")) == {'lang': 'en:g'}
        assert dict(
            self._parse(
                '{name:"viewport", content:"width:device-width, initial-scale:1, minimum-scale:1, maximum-scale:1"}'
            )
        ) == {
            'name':
            'viewport',
            'content':
            'width:device-width, initial-scale:1, minimum-scale:1, maximum-scale:1'
        }

        # double quotes
        assert dict(self._parse('{"class": "test", "id": "something"}')) == {
            'class': 'test',
            'id': 'something'
        }

        # no quotes for key
        assert dict(self._parse("{class: 'test', id: 'something'}")) == {
            'class': 'test',
            'id': 'something'
        }

        # whitespace is ignored
        assert dict(
            self._parse(
                "{   class  \t :        'test',     data-number:    123  }")
        ) == {
            'class': 'test',
            'data-number': '123'
        }

        # trailing commas are fine
        assert dict(self._parse("{class: 'test', data-number: 123,}")) == {
            'class': 'test',
            'data-number': '123'
        }

        # attributes split onto multiple lines
        assert dict(
            self._parse("{class: 'test',\n     data-number: 123}")) == {
                'class': 'test',
                'data-number': '123'
            }

        # old style Ruby
        assert dict(self._parse("{:class => 'test', :data-number=>123}")) == {
            'class': 'test',
            'data-number': '123'
        }

        # list attribute values
        assert dict(
            self._parse(
                "{'class': [ 'a', 'b', 'c' ], data-list:[1, 2, 3]}")) == {
                    'class': ['a', 'b', 'c'],
                    'data-list': ['1', '2', '3']
                }

        # tuple attribute values
        assert dict(
            self._parse(
                "{:class=>( 'a', 'b', 'c' ), :data-list => (1, 2, 3)}")) == {
                    'class': ['a', 'b', 'c'],
                    'data-list': ['1', '2', '3']
                }

        # attribute order is maintained
        assert self._parse(
            "{'class': 'test', 'id': 'something', foo: 'bar'}") == OrderedDict(
                [('class', 'test'), ('id', 'something'), ('foo', 'bar')])

        # attribute values can be multi-line Haml
        assert dict(
            self._parse("""{
                'class':
                    - if forloop.first
                        link-first
\x20
                    - else
                        - if forloop.last
                            link-last
                'href':
                    - url 'some_view'
                }""")
        ) == {
            'class':
            '{% if forloop.first %} link-first {% else %} {% if forloop.last %} link-last {% endif %} {% endif %}',  # noqa
            'href': "{% url 'some_view' %}"
        }

        # non-ascii attribute values
        assert dict(self._parse("{class: 'test\u1234'}")) == {
            'class': 'test\u1234'
        }
Ejemplo n.º 2
0
    def test_read_ruby_style_attribute_dict(self):
        # empty dict
        stream = Stream("{}><")
        assert dict(read_attribute_dict(stream)) == {}
        assert stream.text[stream.ptr:] == '><'

        # string values
        assert dict(self._parse("{'class': 'test'} =Test")) == {'class': 'test'}
        assert dict(self._parse("{'class': 'test', 'id': 'something'}")) == {'class': 'test', 'id': 'something'}

        # integer values
        assert dict(self._parse("{'data-number': 0}")) == {'data-number': '0'}
        assert dict(self._parse("{'data-number': 12345}")) == {'data-number': '12345'}

        # float values
        assert dict(self._parse("{'data-number': 123.456}")) == {'data-number': '123.456'}
        assert dict(self._parse("{'data-number': 0.001}")) == {'data-number': '0.001'}

        # None value
        assert dict(self._parse("{'controls': None}")) == {'controls': None}

        # boolean attributes
        assert dict(self._parse(
            "{disabled, class:'test', data-number : 123,\n foo:\"bar\"}"
        )) == {'disabled': None, 'class': 'test', 'data-number': '123', 'foo': 'bar'}

        assert dict(self._parse(
            "{class:'test', data-number : 123,\n foo:\"bar\",  \t   disabled}"
        )) == {'disabled': None, 'class': 'test', 'data-number': '123', 'foo': 'bar'}

        # attribute name has colon
        assert dict(self._parse("{'xml:lang': 'en'}")) == {'xml:lang': 'en'}

        # attribute value has colon or commas
        assert dict(self._parse("{'lang': 'en:g'}")) == {'lang': 'en:g'}
        assert dict(self._parse(
            '{name:"viewport", content:"width:device-width, initial-scale:1, minimum-scale:1, maximum-scale:1"}'
        )) == {'name': 'viewport', 'content': 'width:device-width, initial-scale:1, minimum-scale:1, maximum-scale:1'}

        # double quotes
        assert dict(self._parse('{"class": "test", "id": "something"}')) == {'class': 'test', 'id': 'something'}

        # no quotes for key
        assert dict(self._parse("{class: 'test', id: 'something'}")) == {'class': 'test', 'id': 'something'}

        # whitespace is ignored
        assert dict(self._parse(
            "{   class  \t :        'test',     data-number:    123  }"
        )) == {'class': 'test', 'data-number': '123'}

        # trailing commas are fine
        assert dict(self._parse("{class: 'test', data-number: 123,}")) == {'class': 'test', 'data-number': '123'}

        # attributes split onto multiple lines
        assert dict(self._parse("{class: 'test',\n     data-number: 123}")) == {'class': 'test', 'data-number': '123'}

        # old style Ruby
        assert dict(self._parse("{:class => 'test', :data-number=>123}")) == {'class': 'test', 'data-number': '123'}

        # list attribute values
        assert dict(self._parse(
            "{'class': [ 'a', 'b', 'c' ], data-list:[1, 2, 3]}"
        )) == {'class': ['a', 'b', 'c'], 'data-list': ['1', '2', '3']}

        # tuple attribute values
        assert dict(self._parse(
            "{:class=>( 'a', 'b', 'c' ), :data-list => (1, 2, 3)}"
        )) == {'class': ('a', 'b', 'c'), 'data-list': ('1', '2', '3')}

        # attribute order is maintained
        assert self._parse(
            "{'class': 'test', 'id': 'something', foo: 'bar'}"
        ) == OrderedDict([('class', 'test'), ('id', 'something'), ('foo', 'bar')])

        # attribute values can be multi-line Haml
        assert dict(self._parse("""{
                'class':
                    - if forloop.first
                        link-first
\x20
                    - else
                        - if forloop.last
                            link-last
                'href':
                    - url 'some_view'
                }"""
        )) == {
            'class': '{% if forloop.first %} link-first {% else %} {% if forloop.last %} link-last {% endif %} {% endif %}',  # noqa
            'href': "{% url 'some_view' %}"
        }

        # non-ascii attribute values
        assert dict(self._parse("{class: 'test\u1234'}")) == {'class': 'test\u1234'}
Ejemplo n.º 3
0
 def _parse(text):
     return read_attribute_dict(Stream(text), Compiler())
Ejemplo n.º 4
0
 def _parse(text):
     return read_attribute_dict(Stream(text))