1
# -*- coding: utf-8 -*-
3
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
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
9
# http://www.apache.org/licenses/LICENSE-2.0
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
21
from doc8 import checks
22
from doc8 import parser
25
class TestTrailingWhitespace(testtools.TestCase):
26
def test_trailing(self):
27
lines = ["a b ", "ab"]
28
check = checks.CheckTrailingWhitespace({})
31
errors.extend(check.report_iter(line))
32
self.assertEqual(1, len(errors))
33
(code, msg) = errors[0]
34
self.assertIn(code, check.REPORTS)
37
class TestTabIndentation(testtools.TestCase):
39
lines = [" b", "\tabc", "efg", "\t\tc"]
40
check = checks.CheckIndentationNoTab({})
43
errors.extend(check.report_iter(line))
44
self.assertEqual(2, len(errors))
45
(code, msg) = errors[0]
46
self.assertIn(code, check.REPORTS)
49
class TestCarriageReturn(testtools.TestCase):
51
lines = ["\tabc", "efg", "\r\n"]
52
check = checks.CheckCarriageReturn({})
55
errors.extend(check.report_iter(line))
56
self.assertEqual(1, len(errors))
57
(code, msg) = errors[0]
58
self.assertIn(code, check.REPORTS)
61
class TestLineLength(testtools.TestCase):
62
def test_over_length(self):
74
content += (b"a" * 60) + b" " + (b"b" * 60)
77
'max_line_length': 79,
78
'allow_long_titles': True,
80
for ext in ['.rst', '.txt']:
81
with tempfile.NamedTemporaryFile(suffix=ext) as fh:
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)
92
def test_correct_length(self):
94
'max_line_length': 79,
95
'allow_long_titles': True,
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')
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))
108
def test_unsplittable_length(self):
120
content += b"a" * 100
123
'max_line_length': 79,
124
'allow_long_titles': True,
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:
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))
141
class TestNewlineEndOfFile(testtools.TestCase):
142
def test_newline(self):
143
tests = [(1, b"testing"),
144
(1, b"testing\ntesting"),
146
(0, b"testing\ntesting\n")]
148
for expected_errors, line in tests:
149
with tempfile.NamedTemporaryFile() as fh:
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))