~ubuntu-branches/debian/sid/python-doc8/sid

« back to all changes in this revision

Viewing changes to doc8/tests/test_checks.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2015-10-14 08:18:40 UTC
  • Revision ID: package-import@ubuntu.com-20151014081840-7hajogar155lmmeb
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
#    Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    a copy of the License at
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#    Unless required by applicable law or agreed to in writing, software
 
12
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
 
 
17
import tempfile
 
18
 
 
19
import testtools
 
20
 
 
21
from doc8 import checks
 
22
from doc8 import parser
 
23
 
 
24
 
 
25
class TestTrailingWhitespace(testtools.TestCase):
 
26
    def test_trailing(self):
 
27
        lines = ["a b  ", "ab"]
 
28
        check = checks.CheckTrailingWhitespace({})
 
29
        errors = []
 
30
        for line in lines:
 
31
            errors.extend(check.report_iter(line))
 
32
        self.assertEqual(1, len(errors))
 
33
        (code, msg) = errors[0]
 
34
        self.assertIn(code, check.REPORTS)
 
35
 
 
36
 
 
37
class TestTabIndentation(testtools.TestCase):
 
38
    def test_tabs(self):
 
39
        lines = ["    b", "\tabc", "efg", "\t\tc"]
 
40
        check = checks.CheckIndentationNoTab({})
 
41
        errors = []
 
42
        for line in lines:
 
43
            errors.extend(check.report_iter(line))
 
44
        self.assertEqual(2, len(errors))
 
45
        (code, msg) = errors[0]
 
46
        self.assertIn(code, check.REPORTS)
 
47
 
 
48
 
 
49
class TestCarriageReturn(testtools.TestCase):
 
50
    def test_cr(self):
 
51
        lines = ["\tabc", "efg", "\r\n"]
 
52
        check = checks.CheckCarriageReturn({})
 
53
        errors = []
 
54
        for line in lines:
 
55
            errors.extend(check.report_iter(line))
 
56
        self.assertEqual(1, len(errors))
 
57
        (code, msg) = errors[0]
 
58
        self.assertIn(code, check.REPORTS)
 
59
 
 
60
 
 
61
class TestLineLength(testtools.TestCase):
 
62
    def test_over_length(self):
 
63
        content = b"""
 
64
===
 
65
aaa
 
66
===
 
67
 
 
68
----
 
69
test
 
70
----
 
71
 
 
72
"""
 
73
        content += b"\n\n"
 
74
        content += (b"a" * 60) + b" " + (b"b" * 60)
 
75
        content += b"\n"
 
76
        conf = {
 
77
            'max_line_length': 79,
 
78
            'allow_long_titles': True,
 
79
        }
 
80
        for ext in ['.rst', '.txt']:
 
81
            with tempfile.NamedTemporaryFile(suffix=ext) as fh:
 
82
                fh.write(content)
 
83
                fh.flush()
 
84
 
 
85
                parsed_file = parser.ParsedFile(fh.name)
 
86
                check = checks.CheckMaxLineLength(conf)
 
87
                errors = list(check.report_iter(parsed_file))
 
88
                self.assertEqual(1, len(errors))
 
89
                (line, code, msg) = errors[0]
 
90
                self.assertIn(code, check.REPORTS)
 
91
 
 
92
    def test_correct_length(self):
 
93
        conf = {
 
94
            'max_line_length': 79,
 
95
            'allow_long_titles': True,
 
96
        }
 
97
        with tempfile.NamedTemporaryFile(suffix='.rst') as fh:
 
98
            fh.write(b'known exploit in the wild, for example'
 
99
                     b' \xe2\x80\x93 the time'
 
100
                     b' between advance notification')
 
101
            fh.flush()
 
102
 
 
103
            parsed_file = parser.ParsedFile(fh.name, encoding='utf-8')
 
104
            check = checks.CheckMaxLineLength(conf)
 
105
            errors = list(check.report_iter(parsed_file))
 
106
            self.assertEqual(0, len(errors))
 
107
 
 
108
    def test_unsplittable_length(self):
 
109
        content = b"""
 
110
===
 
111
aaa
 
112
===
 
113
 
 
114
----
 
115
test
 
116
----
 
117
 
 
118
"""
 
119
        content += b"\n\n"
 
120
        content += b"a" * 100
 
121
        content += b"\n"
 
122
        conf = {
 
123
            'max_line_length': 79,
 
124
            'allow_long_titles': True,
 
125
        }
 
126
        # This number is different since rst parsing is aware that titles
 
127
        # are allowed to be over-length, while txt parsing is not aware of
 
128
        # this fact (since it has no concept of title sections).
 
129
        extensions = [(0, '.rst'), (1, '.txt')]
 
130
        for expected_errors, ext in extensions:
 
131
            with tempfile.NamedTemporaryFile(suffix=ext) as fh:
 
132
                fh.write(content)
 
133
                fh.flush()
 
134
 
 
135
                parsed_file = parser.ParsedFile(fh.name)
 
136
                check = checks.CheckMaxLineLength(conf)
 
137
                errors = list(check.report_iter(parsed_file))
 
138
                self.assertEqual(expected_errors, len(errors))
 
139
 
 
140
 
 
141
class TestNewlineEndOfFile(testtools.TestCase):
 
142
    def test_newline(self):
 
143
        tests = [(1, b"testing"),
 
144
                 (1, b"testing\ntesting"),
 
145
                 (0, b"testing\n"),
 
146
                 (0, b"testing\ntesting\n")]
 
147
 
 
148
        for expected_errors, line in tests:
 
149
            with tempfile.NamedTemporaryFile() as fh:
 
150
                fh.write(line)
 
151
                fh.flush()
 
152
                parsed_file = parser.ParsedFile(fh.name)
 
153
                check = checks.CheckNewlineEndOfFile({})
 
154
                errors = list(check.report_iter(parsed_file))
 
155
                self.assertEqual(expected_errors, len(errors))