Пример #1
0
    def test_call_base(self, mocker):
        mock_Section = mocker.patch.object(template, 'Section')
        values = {'sections': {}}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [
            perfile.Token(perfile.TOK_WORD, 'section'),
            perfile.Token(perfile.TOK_CHAR, '('),
            perfile.Token(perfile.TOK_WORD, 'var1'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var2'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var3'),
            perfile.Token(perfile.TOK_CHAR, ')'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]
        end_toks = []
        obj = template.SectionDirective(values, start_coord, start_toks)

        result = obj(end_coord, 'buf', end_toks)

        assert result is None
        assert values == {
            'sections': {
                'section': mock_Section.return_value
            },
        }
        mock_Section.assert_called_once_with(
            location.CoordinateRange('path', 23, 42),
            'section',
            {'var1', 'var2', 'var3'},
            'buf',
        )
Пример #2
0
    def test_output_location_switch_stream_name(self):
        obj = linelist.LineList(['l1', 'l2', 'l3'])
        obj.extend(['l4', 'l5', 'l6'], location.Coordinate('some.path', 10))
        obj.extend(['l7', 'l8', 'l9'])
        obj.extend(['l10', 'l11', 'l12'], location.Coordinate('my.path', 5))
        stream = six.StringIO()
        stream.name = 'base.path'

        obj.output(stream)

        assert stream.getvalue() == ('l1\n'
                                     'l2\n'
                                     'l3\n'
                                     '#line 10 "some.path"\n'
                                     'l4\n'
                                     'l5\n'
                                     'l6\n'
                                     '#line 9 "base.path"\n'
                                     'l7\n'
                                     'l8\n'
                                     'l9\n'
                                     '#line 5 "my.path"\n'
                                     'l10\n'
                                     'l11\n'
                                     'l12\n')
Пример #3
0
    def test_sub_coordinate_later(self, mocker):
        mock_CoordinateRange = mocker.patch.object(location, 'CoordinateRange')
        obj1 = location.Coordinate('file.name', 23)
        obj2 = location.Coordinate('file.name', 42)

        result = obj1.__sub__(obj2)

        assert result == mock_CoordinateRange.return_value
        mock_CoordinateRange.assert_called_once_with('file.name', 23, 42)
Пример #4
0
    def test_sub_coordinate_badpath(self, mocker):
        mock_CoordinateRange = mocker.patch.object(location, 'CoordinateRange')
        obj1 = location.Coordinate('file.name', 23)
        obj2 = location.Coordinate('other.name', 42)

        result = obj1.__sub__(obj2)

        assert result is NotImplemented
        assert not mock_CoordinateRange.called
Пример #5
0
    def test_call_bad_end(self, mocker):
        mock_Literal = mocker.patch.object(template, 'Literal')
        values = {'structure': []}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [perfile.Token(perfile.TOK_CHAR, '{')]
        end_toks = [perfile.Token(perfile.TOK_CHAR, '}')]
        obj = template.LiteralDirective(values, start_coord, start_toks)

        with pytest.raises(perfile.ParseException):
            obj(end_coord, 'buf', end_toks)

        assert values == {'structure': []}
        assert not mock_Literal.called
Пример #6
0
    def test_sub_other(self, mocker):
        mock_CoordinateRange = mocker.patch.object(location, 'CoordinateRange')
        obj = location.Coordinate('file.name', 23)

        result = obj.__sub__(other)

        assert result is NotImplemented
        assert not mock_CoordinateRange.called
Пример #7
0
    def test_call_base(self, mocker):
        mock_Literal = mocker.patch.object(template, 'Literal')
        values = {'structure': []}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [perfile.Token(perfile.TOK_CHAR, '{')]
        end_toks = []
        obj = template.LiteralDirective(values, start_coord, start_toks)

        result = obj(end_coord, 'buf', end_toks)

        assert result is None
        assert values == {
            'structure': [mock_Literal.return_value],
        }
        mock_Literal.assert_called_once_with(
            location.CoordinateRange('path', 23, 42), 'buf')
Пример #8
0
    def test_call_bad_end(self, mocker):
        mock_Define = mocker.patch.object(template, 'Define')
        values = {'defines': {}}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [
            perfile.Token(perfile.TOK_WORD, 'define'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]
        end_toks = [perfile.Token(perfile.TOK_CHAR, '}')]
        obj = template.DefineDirective(values, start_coord, start_toks)

        with pytest.raises(perfile.ParseException):
            obj(end_coord, 'buf', end_toks)

        assert values == {'defines': {}}
        assert not mock_Define.called
Пример #9
0
    def test_init_bad_tok(self):
        values = {'structure': []}
        coord = location.Coordinate('path', 23)
        toks = [perfile.Token(perfile.TOK_CHAR, '(')]

        with pytest.raises(perfile.ParseException):
            template.LiteralDirective(values, coord, toks)

        assert values == {'structure': []}
Пример #10
0
    def test_add_integer(self):
        obj = location.Coordinate('file.name', 23)

        result = obj.__add__(3)

        assert result is not obj
        assert isinstance(result, location.Coordinate)
        assert result.path == obj.path
        assert result.lno == obj.lno + 3
Пример #11
0
    def test_init_missing_toks(self):
        values = {'sections': {}}
        coord = location.Coordinate('path', 23)
        toks = [perfile.Token(perfile.TOK_CHAR, '{')]

        with pytest.raises(perfile.ParseException):
            template.SectionDirective(values, coord, toks)

        assert values == {'sections': {}}
Пример #12
0
    def test_init_base(self):
        values = {'structure': []}
        coord = location.Coordinate('path', 23)
        toks = [perfile.Token(perfile.TOK_CHAR, '{')]

        result = template.LiteralDirective(values, coord, toks)

        assert result.values is values
        assert result.start_coord == coord
        assert values == {'structure': []}
Пример #13
0
    def test_sub_integer(self, mocker):
        mock_CoordinateRange = mocker.patch.object(location, 'CoordinateRange')
        obj = location.Coordinate('file.name', 23)

        result = obj.__sub__(3)

        assert result is not obj
        assert isinstance(result, location.Coordinate)
        assert result.path == obj.path
        assert result.lno == obj.lno - 3
        assert not mock_CoordinateRange.called
Пример #14
0
    def test_bad_tok(self, mocker):
        mock_InsertSection = mocker.patch.object(template, 'InsertSection')
        values = {'structure': []}
        coord = location.Coordinate('path', 23)
        toks = [perfile.Token(perfile.TOK_CHAR, '{')]

        with pytest.raises(perfile.ParseException):
            template.insert(values, coord, toks)

        assert values == {'structure': []}
        assert not mock_InsertSection.called
Пример #15
0
    def test_init_bad_brace(self):
        values = {'sections': {}}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, 'section'),
            perfile.Token(perfile.TOK_CHAR, '('),
        ]

        with pytest.raises(perfile.ParseException):
            template.SectionDirective(values, coord, toks)

        assert values == {'sections': {}}
Пример #16
0
    def test_init_too_many_toks(self):
        values = {'structure': []}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, 'something'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]

        with pytest.raises(perfile.ParseException):
            template.LiteralDirective(values, coord, toks)

        assert values == {'structure': []}
Пример #17
0
    def test_init_no_name(self):
        values = {'defines': {}}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, ''),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]

        with pytest.raises(perfile.ParseException):
            template.DefineDirective(values, coord, toks)

        assert values == {'defines': {}}
Пример #18
0
    def test_call_base(self, mocker):
        mock_Define = mocker.patch.object(template, 'Define')
        values = {'defines': {}}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [
            perfile.Token(perfile.TOK_WORD, 'define'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]
        end_toks = []
        obj = template.DefineDirective(values, start_coord, start_toks)

        result = obj(end_coord, 'buf', end_toks)

        assert result is None
        assert values == {
            'defines': {
                'define': mock_Define.return_value
            },
        }
        mock_Define.assert_called_once_with(
            location.CoordinateRange('path', 23, 42), 'define', 'buf')
Пример #19
0
    def test_base(self, mocker):
        mock_InsertSection = mocker.patch.object(template, 'InsertSection')
        values = {'structure': []}
        coord = location.Coordinate('path', 23)
        toks = [perfile.Token(perfile.TOK_WORD, 'section')]

        result = template.insert(values, coord, toks)

        assert result is None
        assert values == {
            'structure': [mock_InsertSection.return_value],
        }
        mock_InsertSection.assert_called_once_with(
            location.CoordinateRange('path', 23, 23), 'section')
Пример #20
0
    def test_init_base(self):
        values = {'defines': {}}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, 'define'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]

        result = template.DefineDirective(values, coord, toks)

        assert result.name == 'define'
        assert result.values is values
        assert result.start_coord == coord
        assert values == {'defines': {}}
Пример #21
0
    def test_call_unclosed(self, mocker):
        mock_Section = mocker.patch.object(template, 'Section')
        values = {'sections': {}}
        start_coord = location.Coordinate('path', 23)
        end_coord = location.Coordinate('path', 42)
        start_toks = [
            perfile.Token(perfile.TOK_WORD, 'section'),
            perfile.Token(perfile.TOK_CHAR, '('),
            perfile.Token(perfile.TOK_WORD, 'var1'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var2'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var3'),
            perfile.Token(perfile.TOK_CHAR, ')'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]
        end_toks = None
        obj = template.SectionDirective(values, start_coord, start_toks)

        with pytest.raises(perfile.ParseException):
            obj(end_coord, 'buf', end_toks)

        assert values == {'sections': {}}
        assert not mock_Section.called
Пример #22
0
    def test_init_missing_close_paren(self):
        values = {'sections': {}}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, 'section'),
            perfile.Token(perfile.TOK_CHAR, '('),
            perfile.Token(perfile.TOK_WORD, 'var1'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var2'),
            perfile.Token(perfile.TOK_CHAR, ','),
            perfile.Token(perfile.TOK_WORD, 'var3'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]

        with pytest.raises(perfile.ParseException):
            template.SectionDirective(values, coord, toks)

        assert values == {'sections': {}}
Пример #23
0
    def test_init_one_require(self):
        values = {'sections': {}}
        coord = location.Coordinate('path', 23)
        toks = [
            perfile.Token(perfile.TOK_WORD, 'section'),
            perfile.Token(perfile.TOK_CHAR, '('),
            perfile.Token(perfile.TOK_WORD, 'var1'),
            perfile.Token(perfile.TOK_CHAR, ')'),
            perfile.Token(perfile.TOK_CHAR, '{'),
        ]

        result = template.SectionDirective(values, coord, toks)

        assert result.name == 'section'
        assert result.requires == {'var1'}
        assert result.values is values
        assert result.start_coord == coord
        assert values == {'sections': {}}
Пример #24
0
    def parse(self, stream, path=None):
        """
        Parse a percent-directive file.

        :param stream: A stream, as opened with ``open()``.  The
                       stream should be opened in text mode with
                       universal newlines.
        :param str path: The path of the file being parsed.  If not
                         provided, ``stream.name`` will be used.

        :returns: A dictionary of values developed from parsing the
                  directives contained within the input file.

        :raises AttributeError:
            The provided ``stream`` has no ``name`` attribute and
            ``path`` was not provided.

        :raises ParseException:
            An error occurred while parsing the input file.
        """

        # Figure out the path
        if path is None:
            path = stream.name

        # Start with the initialized values
        values = self._values()

        # Parse the stream
        for lno, line in enumerate(stream):
            try:
                self._parse_line(
                    location.Coordinate(path, lno + 1), line, values
                )
            except Continue:
                # Special exception indicating an extended line
                continue

        # Was there a line continuation?
        if self._continued is not None:
            raise ParseException(
                'Trailing directive continuation at end of file; '
                'starts at %s' % self._start_coord
            )

        # Were we in the middle of a comment?
        if self._comment_pfx is not None:
            raise ParseException(
                'Unclosed comment at end of file; starts at %s' %
                self._start_coord
            )

        # How about the middle of a directive?
        if self._deferred:
            # Give the deferred routine a chance to complain
            self._deferred(
                location.Coordinate(path, self._lines), self._buf, None
            )

            # But complain anyway if it doesn't
            raise ParseException('Unclosed directive at end of file')

        return values
Пример #25
0
    def test_eq_unequal_line(self):
        obj1 = location.Coordinate('file.name', 23)
        obj2 = location.Coordinate('file.name', 42)

        assert not obj1.__eq__(obj2)
Пример #26
0
    def test_add_other(self):
        obj = location.Coordinate('file.name', 23)

        result = obj.__add__(other)

        assert result is NotImplemented
Пример #27
0
    def test_ne_equal(self):
        obj1 = location.Coordinate('file.name', 23)
        obj2 = location.Coordinate('file.name', 23)

        assert not obj1.__ne__(obj2)
Пример #28
0
    def test_ne_unequal_file(self):
        obj1 = location.Coordinate('file.name', 23)
        obj2 = location.Coordinate('other.name', 23)

        assert obj1.__ne__(obj2)
Пример #29
0
    def test_ne_other(self):
        obj = location.Coordinate('file.name', 23)

        assert obj.__ne__(other)
Пример #30
0
    def test_init(self):
        result = location.Coordinate('file.name', 23)

        assert result.path == 'file.name'
        assert result.lno == 23