Initial commit
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
0
tests/rules/__init__.py
Normal file
0
tests/rules/__init__.py
Normal file
45
tests/rules/common.py
Normal file
45
tests/rules/common.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import unittest
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.config import parse_config
|
||||
from yamllint.errors import LintProblem
|
||||
from yamllint import lint
|
||||
|
||||
|
||||
class RuleTestCase(unittest.TestCase):
|
||||
def build_fake_config(self, conf):
|
||||
if conf is None:
|
||||
conf = {}
|
||||
else:
|
||||
conf = yaml.safe_load(conf)
|
||||
conf = {'extends': 'default',
|
||||
'rules': conf}
|
||||
return parse_config(yaml.safe_dump(conf))
|
||||
|
||||
def check(self, source, conf, line=None, column=None, **kwargs):
|
||||
expected_problems = []
|
||||
for key in kwargs:
|
||||
assert key.startswith('problem')
|
||||
expected_problems.append(
|
||||
LintProblem(kwargs[key][0], kwargs[key][1], rule=self.rule_id))
|
||||
expected_problems.sort()
|
||||
|
||||
real_problems = list(lint(source, self.build_fake_config(conf)))
|
||||
self.assertEqual(real_problems, expected_problems)
|
||||
220
tests/rules/test_colons.py
Normal file
220
tests/rules/test_colons.py
Normal file
@@ -0,0 +1,220 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class ColonTestCase(RuleTestCase):
|
||||
rule_id = 'colons'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'colons: disable'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1 : v1\n'
|
||||
'obj2:\n'
|
||||
' k2 :\n'
|
||||
' - 8\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
' property : value\n'
|
||||
' prop2 : val2\n'
|
||||
' propriété : [ valeur ]\n'
|
||||
' o:\n'
|
||||
' k1: [v1, v2]\n'
|
||||
' p:\n'
|
||||
' - k3: >\n'
|
||||
' val\n'
|
||||
' - o: { k1: v1 }\n'
|
||||
' - p: kdjf\n'
|
||||
' - q: val0\n'
|
||||
' q2:\n'
|
||||
' - val1\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: v1\n'
|
||||
'obj2:\n'
|
||||
' k2:\n'
|
||||
' - 8\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
' property: value\n'
|
||||
' prop2: val2\n'
|
||||
' propriété: [ valeur ]\n'
|
||||
' o:\n'
|
||||
' k1: [v1, v2]\n', conf)
|
||||
self.check('---\n'
|
||||
'obj:\n'
|
||||
' p:\n'
|
||||
' - k1: >\n'
|
||||
' val\n'
|
||||
' - k3: >\n'
|
||||
' val\n'
|
||||
' - o: { k1: v1 }\n'
|
||||
' - o: { k1: v1 }\n'
|
||||
' q2:\n'
|
||||
' - val1\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'a: {b: {c: d, e : f}}\n', conf)
|
||||
|
||||
def test_before_enabled(self):
|
||||
conf = 'colons: {max-spaces-before: 0, max-spaces-after: -1}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2: v2\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1 :\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2: v2\n'
|
||||
'...\n', conf, problem=(3, 5))
|
||||
self.check('---\n'
|
||||
'lib :\n'
|
||||
' - var\n'
|
||||
'...\n', conf, problem=(2, 4))
|
||||
self.check('---\n'
|
||||
'- lib :\n'
|
||||
' - var\n'
|
||||
'...\n', conf, problem=(2, 6))
|
||||
self.check('---\n'
|
||||
'a: {b: {c : d, e : f}}\n', conf,
|
||||
problem1=(2, 10), problem2=(2, 17))
|
||||
|
||||
def test_before_max(self):
|
||||
conf = 'colons: {max-spaces-before: 3, max-spaces-after: -1}'
|
||||
self.check('---\n'
|
||||
'object :\n'
|
||||
' k1 :\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2 : v2\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object :\n'
|
||||
' k1 :\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2 : v2\n'
|
||||
'...\n', conf, problem=(3, 8))
|
||||
|
||||
def test_before_with_explicit_block_mappings(self):
|
||||
conf = 'colons: {max-spaces-before: 0, max-spaces-after: -1}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' ? key\n'
|
||||
' : value\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object :\n'
|
||||
' ? key\n'
|
||||
' : value\n'
|
||||
'...\n', conf, problem=(2, 7))
|
||||
|
||||
def test_after_enabled(self):
|
||||
conf = 'colons: {max-spaces-before: -1, max-spaces-after: 1}'
|
||||
self.check('---\n'
|
||||
'key: value\n', conf)
|
||||
self.check('---\n'
|
||||
'key: value\n', conf, problem=(2, 6))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: [a, b]\n'
|
||||
' k2: string\n', conf, problem=(3, 7))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: [a, b]\n'
|
||||
' k2: string\n', conf, problem=(4, 7))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' other: {key: value}\n'
|
||||
'...\n', conf, problem=(3, 16))
|
||||
self.check('---\n'
|
||||
'a: {b: {c: d, e : f}}\n', conf,
|
||||
problem1=(2, 12), problem2=(2, 20))
|
||||
|
||||
def test_after_max(self):
|
||||
conf = 'colons: {max-spaces-before: -1, max-spaces-after: 3}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: [a, b]\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: [a, b]\n', conf, problem=(3, 9))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k2: string\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k2: string\n', conf, problem=(3, 9))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' other: {key: value}\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' other: {key: value}\n'
|
||||
'...\n', conf, problem=(3, 18))
|
||||
|
||||
def test_after_with_explicit_block_mappings(self):
|
||||
conf = 'colons: {max-spaces-before: -1, max-spaces-after: 1}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' ? key\n'
|
||||
' : value\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' ? key\n'
|
||||
' : value\n'
|
||||
'...\n', conf, problem=(4, 5))
|
||||
|
||||
def test_after_do_not_confound_with_trailing_space(self):
|
||||
conf = ('colons: {max-spaces-before: 1, max-spaces-after: 1}\n'
|
||||
'trailing-spaces: disable\n')
|
||||
self.check('---\n'
|
||||
'trailing: \n'
|
||||
' - spaces\n', conf)
|
||||
|
||||
def test_both_before_and_after(self):
|
||||
conf = 'colons: {max-spaces-before: 0, max-spaces-after: 1}'
|
||||
self.check('---\n'
|
||||
'obj:\n'
|
||||
' string: text\n'
|
||||
' k:\n'
|
||||
' - 8\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
' property: [value]\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1 : v1\n', conf, problem1=(3, 5), problem2=(3, 8))
|
||||
self.check('---\n'
|
||||
'obj:\n'
|
||||
' string: text\n'
|
||||
' k :\n'
|
||||
' - 8\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
' property: {a: 1, b: 2, c : 3}\n', conf,
|
||||
problem1=(3, 11), problem2=(4, 4),
|
||||
problem3=(8, 23), problem4=(8, 28))
|
||||
80
tests/rules/test_document_end.py
Normal file
80
tests/rules/test_document_end.py
Normal file
@@ -0,0 +1,80 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class DocumentEndTestCase(RuleTestCase):
|
||||
rule_id = 'document-end'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'document-end: disable'
|
||||
self.check('---\n'
|
||||
'with:\n'
|
||||
' document: end\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'without:\n'
|
||||
' document: end\n', conf)
|
||||
|
||||
def test_required(self):
|
||||
conf = 'document-end: {present: yes}'
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('---\n'
|
||||
'with:\n'
|
||||
' document: end\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'without:\n'
|
||||
' document: end\n', conf, problem=(3, 1))
|
||||
|
||||
def test_forbidden(self):
|
||||
conf = 'document-end: {present: no}'
|
||||
self.check('---\n'
|
||||
'with:\n'
|
||||
' document: end\n'
|
||||
'...\n', conf, problem=(4, 1))
|
||||
self.check('---\n'
|
||||
'without:\n'
|
||||
' document: end\n', conf)
|
||||
|
||||
def test_multiple_documents(self):
|
||||
conf = ('document-end: {present: yes}\n'
|
||||
'document-start: disable\n')
|
||||
self.check('---\n'
|
||||
'first: document\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'second: document\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'third: document\n'
|
||||
'...\n', conf)
|
||||
self.check('first: document\n'
|
||||
'...\n'
|
||||
'second: document\n'
|
||||
'...\n'
|
||||
'third: document\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'first: document\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'second: document\n'
|
||||
'---\n'
|
||||
'third: document\n'
|
||||
'...\n', conf, problem=(6, 1))
|
||||
90
tests/rules/test_document_start.py
Normal file
90
tests/rules/test_document_start.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class DocumentStartTestCase(RuleTestCase):
|
||||
rule_id = 'document-start'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'document-start: disable'
|
||||
self.check('', conf)
|
||||
self.check('key: val\n', conf)
|
||||
self.check('---\n'
|
||||
'key: val\n', conf)
|
||||
|
||||
def test_required(self):
|
||||
conf = ('document-start: {present: yes}\n'
|
||||
'empty-lines: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('key: val\n', conf, problem=(1, 1))
|
||||
self.check('\n'
|
||||
'\n'
|
||||
'key: val\n', conf, problem=(3, 1))
|
||||
self.check('---\n'
|
||||
'key: val\n', conf)
|
||||
self.check('\n'
|
||||
'\n'
|
||||
'---\n'
|
||||
'key: val\n', conf)
|
||||
|
||||
def test_forbidden(self):
|
||||
conf = ('document-start: {present: no}\n'
|
||||
'empty-lines: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('key: val\n', conf)
|
||||
self.check('\n'
|
||||
'\n'
|
||||
'key: val\n', conf)
|
||||
self.check('---\n'
|
||||
'key: val\n', conf, problem=(1, 1))
|
||||
self.check('\n'
|
||||
'\n'
|
||||
'---\n'
|
||||
'key: val\n', conf, problem=(3, 1))
|
||||
self.check('first: document\n'
|
||||
'---\n'
|
||||
'key: val\n', conf, problem=(2, 1))
|
||||
|
||||
def test_multiple_documents(self):
|
||||
conf = 'document-start: {present: yes}'
|
||||
self.check('---\n'
|
||||
'first: document\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'second: document\n'
|
||||
'...\n'
|
||||
'---\n'
|
||||
'third: document\n', conf)
|
||||
self.check('---\n'
|
||||
'first: document\n'
|
||||
'---\n'
|
||||
'second: document\n'
|
||||
'---\n'
|
||||
'third: document\n', conf)
|
||||
self.check('---\n'
|
||||
'first: document\n'
|
||||
'...\n'
|
||||
'second: document\n'
|
||||
'---\n'
|
||||
'third: document\n', conf, problem=(4, 1))
|
||||
|
||||
def test_directives(self):
|
||||
# TODO
|
||||
# %YAML 1.2
|
||||
pass
|
||||
80
tests/rules/test_empty_lines.py
Normal file
80
tests/rules/test_empty_lines.py
Normal file
@@ -0,0 +1,80 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class EmptyLinesTestCase(RuleTestCase):
|
||||
rule_id = 'empty-lines'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = ('empty-lines: disable\n'
|
||||
'new-line-at-end-of-file: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('\n\n', conf)
|
||||
self.check('\n\n\n\n\n\n\n\n\n', conf)
|
||||
self.check('some text\n\n\n\n\n\n\n\n\n', conf)
|
||||
self.check('\n\n\n\n\n\n\n\n\nsome text', conf)
|
||||
self.check('\n\n\nsome text\n\n\n', conf)
|
||||
|
||||
def test_empty_document(self):
|
||||
conf = ('empty-lines: {max: 0, max-start: 0, max-end: 0}\n'
|
||||
'new-line-at-end-of-file: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
|
||||
def test_0_empty_lines(self):
|
||||
conf = ('empty-lines: {max: 0, max-start: 0, max-end: 0}\n'
|
||||
'new-line-at-end-of-file: disable\n')
|
||||
self.check('---\n', conf)
|
||||
self.check('---\ntext\n\ntext', conf, problem=(3, 1))
|
||||
self.check('---\ntext\n\ntext\n', conf, problem=(3, 1))
|
||||
|
||||
def test_10_empty_lines(self):
|
||||
conf = 'empty-lines: {max: 10, max-start: 0, max-end: 0}'
|
||||
self.check('---\nintro\n\n\n\n\n\n\n\n\n\n\nconclusion\n', conf)
|
||||
self.check('---\nintro\n\n\n\n\n\n\n\n\n\n\n\nconclusion\n', conf,
|
||||
problem=(13, 1))
|
||||
|
||||
def test_spaces(self):
|
||||
conf = ('empty-lines: {max: 1, max-start: 0, max-end: 0}\n'
|
||||
'trailing-spaces: disable\n')
|
||||
self.check('---\nintro\n\n \n\nconclusion\n', conf)
|
||||
self.check('---\nintro\n\n \n\n\nconclusion\n', conf, problem=(6, 1))
|
||||
|
||||
def test_empty_lines_at_start(self):
|
||||
conf = ('empty-lines: {max: 2, max-start: 4, max-end: 0}\n'
|
||||
'document-start: disable\n')
|
||||
self.check('\n\n\n\nnon empty\n', conf)
|
||||
self.check('\n\n\n\n\nnon empty\n', conf, problem=(5, 1))
|
||||
|
||||
conf = ('empty-lines: {max: 2, max-start: 0, max-end: 0}\n'
|
||||
'document-start: disable\n')
|
||||
self.check('non empty\n', conf)
|
||||
self.check('\nnon empty\n', conf, problem=(1, 1))
|
||||
|
||||
def test_empty_lines_at_end(self):
|
||||
conf = ('empty-lines: {max: 2, max-start: 0, max-end: 4}\n'
|
||||
'document-start: disable\n')
|
||||
self.check('non empty\n\n\n\n\n', conf)
|
||||
self.check('non empty\n\n\n\n\n\n', conf, problem=(6, 1))
|
||||
conf = ('empty-lines: {max: 2, max-start: 0, max-end: 0}\n'
|
||||
'document-start: disable\n')
|
||||
self.check('non empty\n', conf)
|
||||
self.check('non empty\n\n', conf, problem=(2, 1))
|
||||
106
tests/rules/test_hyphens.py
Normal file
106
tests/rules/test_hyphens.py
Normal file
@@ -0,0 +1,106 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class HyphenTestCase(RuleTestCase):
|
||||
rule_id = 'hyphens'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'hyphens: disable'
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' subobject:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' subobject:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf)
|
||||
|
||||
def test_enabled(self):
|
||||
conf = 'hyphens: {max-spaces-after: 1}'
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem=(3, 3))
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem1=(2, 3), problem2=(3, 3))
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem=(2, 3))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem=(4, 3))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem1=(3, 3), problem2=(4, 3))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' subobject:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf, problem=(5, 7))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' subobject:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf, problem1=(4, 7), problem2=(5, 7))
|
||||
|
||||
def test_max_3(self):
|
||||
conf = 'hyphens: {max-spaces-after: 3}'
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'- elem1\n'
|
||||
'- elem2\n', conf, problem=(2, 5))
|
||||
self.check('---\n'
|
||||
'a:\n'
|
||||
' b:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf)
|
||||
self.check('---\n'
|
||||
'a:\n'
|
||||
' b:\n'
|
||||
' - elem1\n'
|
||||
' - elem2\n', conf, problem1=(4, 9), problem2=(5, 9))
|
||||
191
tests/rules/test_indentation.py
Normal file
191
tests/rules/test_indentation.py
Normal file
@@ -0,0 +1,191 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class IndentationTestCase(RuleTestCase):
|
||||
rule_id = 'indentation'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'indentation: disable'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1: v1\n'
|
||||
'obj2:\n'
|
||||
' k2:\n'
|
||||
' - 8\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
' o:\n'
|
||||
' k1: v1\n'
|
||||
' p:\n'
|
||||
' k3:\n'
|
||||
' val\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
' - o:\n'
|
||||
' k1: v1\n'
|
||||
' - p: kdjf\n'
|
||||
' - q:\n'
|
||||
' k3:\n'
|
||||
' - val\n'
|
||||
'...\n', conf)
|
||||
|
||||
def test_one_space(self):
|
||||
conf = 'indentation: {spaces: 1}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2: v2\n'
|
||||
'...\n', conf)
|
||||
|
||||
def test_two_spaces(self):
|
||||
conf = 'indentation: {spaces: 2}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2: v2\n'
|
||||
'...\n', conf)
|
||||
|
||||
def test_three_spaces(self):
|
||||
conf = 'indentation: {spaces: 3}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
' - b\n'
|
||||
' k2: v2\n'
|
||||
'...\n', conf)
|
||||
|
||||
def test_under_indented(self):
|
||||
conf = 'indentation: {spaces: 2}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' val: 1\n'
|
||||
'...\n', conf, problem=(3, 2))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
'...\n', conf, problem=(4, 4))
|
||||
conf = 'indentation: {spaces: 4}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' val: 1\n'
|
||||
'...\n', conf, problem=(3, 4))
|
||||
self.check('---\n'
|
||||
'- el1\n'
|
||||
'- el2:\n'
|
||||
' - subel\n'
|
||||
'...\n', conf, problem=(4, 4))
|
||||
|
||||
def test_over_indented(self):
|
||||
conf = 'indentation: {spaces: 2}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' val: 1\n'
|
||||
'...\n', conf, problem=(3, 4))
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' k1:\n'
|
||||
' - a\n'
|
||||
'...\n', conf, problem=(4, 6))
|
||||
conf = 'indentation: {spaces: 4}'
|
||||
self.check('---\n'
|
||||
'object:\n'
|
||||
' val: 1\n'
|
||||
'...\n', conf, problem=(3, 6))
|
||||
self.check('---\n'
|
||||
' object:\n'
|
||||
' val: 1\n'
|
||||
'...\n', conf, problem=(2, 2))
|
||||
self.check('---\n'
|
||||
'- el1\n'
|
||||
'- el2:\n'
|
||||
' - subel\n'
|
||||
'...\n', conf, problem=(4, 6))
|
||||
self.check('---\n'
|
||||
'- el1\n'
|
||||
'- el2:\n'
|
||||
' - subel\n'
|
||||
'...\n', conf, problem=(4, 15))
|
||||
self.check('---\n'
|
||||
' - el1\n'
|
||||
' - el2:\n'
|
||||
' - subel\n'
|
||||
'...\n', conf, problem1=(2, 3), problem2=(4, 7))
|
||||
|
||||
def test_multi_lines(self):
|
||||
self.check('---\n'
|
||||
'long_string: >\n'
|
||||
' bla bla blah\n'
|
||||
' blah bla bla\n'
|
||||
'...\n', None)
|
||||
self.check('---\n'
|
||||
'- long_string: >\n'
|
||||
' bla bla blah\n'
|
||||
' blah bla bla\n'
|
||||
'...\n', None)
|
||||
self.check('---\n'
|
||||
'obj:\n'
|
||||
' - long_string: >\n'
|
||||
' bla bla blah\n'
|
||||
' blah bla bla\n'
|
||||
'...\n', None)
|
||||
|
||||
def test_nested_collections(self):
|
||||
self.check('---\n'
|
||||
'- o:\n'
|
||||
' k1: v1\n'
|
||||
'...\n', None)
|
||||
self.check('---\n'
|
||||
'- o:\n'
|
||||
' k1: v1\n'
|
||||
'...\n', None, problem=(3, 4))
|
||||
self.check('---\n'
|
||||
'- o:\n'
|
||||
' k1: v1\n'
|
||||
'...\n', None, problem=(3, 6))
|
||||
|
||||
def test_return(self):
|
||||
self.check('---\n'
|
||||
'a:\n'
|
||||
' b:\n'
|
||||
' c:\n'
|
||||
' d:\n'
|
||||
' e:\n'
|
||||
' f:\n'
|
||||
'g:\n'
|
||||
'...\n', None)
|
||||
# self.check('---\n'
|
||||
# 'a:\n'
|
||||
# ' b:\n'
|
||||
# ' c:\n'
|
||||
# ' d:\n'
|
||||
# '...\n', None, problem=(5, 5))
|
||||
# self.check('---\n'
|
||||
# 'a:\n'
|
||||
# ' b:\n'
|
||||
# ' c:\n'
|
||||
# ' d:\n'
|
||||
# '...\n', None, problem=(5, 2))
|
||||
63
tests/rules/test_line_length.py
Normal file
63
tests/rules/test_line_length.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class LineLengthTestCase(RuleTestCase):
|
||||
rule_id = 'line-length'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = ('line-length: disable\n'
|
||||
'empty-lines: disable\n'
|
||||
'new-line-at-end-of-file: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('---\n', conf)
|
||||
self.check(81 * 'a', conf)
|
||||
self.check('---\n' + 81 * 'a' + '\n', conf)
|
||||
self.check(1000 * 'b', conf)
|
||||
self.check('---\n' + 1000 * 'b' + '\n', conf)
|
||||
|
||||
def test_default(self):
|
||||
conf = ('line-length: {max: 80}\n'
|
||||
'empty-lines: disable\n'
|
||||
'new-line-at-end-of-file: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('---\n', conf)
|
||||
self.check(80 * 'a', conf)
|
||||
self.check('---\n' + 80 * 'a' + '\n', conf)
|
||||
self.check(81 * 'a', conf, problem=(1, 81))
|
||||
self.check('---\n' + 81 * 'a' + '\n', conf, problem=(2, 81))
|
||||
self.check(1000 * 'b', conf, problem=(1, 81))
|
||||
self.check('---\n' + 1000 * 'b' + '\n', conf, problem=(2, 81))
|
||||
|
||||
def test_max_length_10(self):
|
||||
conf = ('line-length: {max: 10}\n'
|
||||
'new-line-at-end-of-file: disable\n')
|
||||
self.check('---\nABCDEFGHIJ', conf)
|
||||
self.check('---\nABCDEFGHIJK', conf, problem=(2, 11))
|
||||
self.check('---\nABCDEFGHIJK\n', conf, problem=(2, 11))
|
||||
|
||||
def test_spaces(self):
|
||||
conf = ('line-length: {max: 80}\n'
|
||||
'new-line-at-end-of-file: disable\n'
|
||||
'trailing-spaces: disable\n')
|
||||
self.check('---\n' + 81 * ' ', conf, problem=(2, 81))
|
||||
self.check('---\n' + 81 * ' ' + '\n', conf, problem=(2, 81))
|
||||
42
tests/rules/test_new_line_at_end_of_file.py
Normal file
42
tests/rules/test_new_line_at_end_of_file.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class NewLineAtEndOfFileTestCase(RuleTestCase):
|
||||
rule_id = 'new-line-at-end-of-file'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = ('new-line-at-end-of-file: disable\n'
|
||||
'empty-lines: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('word', conf)
|
||||
self.check('Sentence.\n', conf)
|
||||
|
||||
def test_enabled(self):
|
||||
conf = ('new-line-at-end-of-file: {}\n'
|
||||
'empty-lines: disable\n'
|
||||
'document-start: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('word', conf, problem=(1, 5))
|
||||
self.check('Sentence.\n', conf)
|
||||
self.check('---\n'
|
||||
'yaml: document\n'
|
||||
'...', conf, problem=(3, 4))
|
||||
47
tests/rules/test_new_lines.py
Normal file
47
tests/rules/test_new_lines.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class NewLinesTestCase(RuleTestCase):
|
||||
rule_id = 'new-lines'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = ('new-line-at-end-of-file: disable\n'
|
||||
'new-lines: disable\n')
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('\r', conf)
|
||||
self.check('\r\n', conf)
|
||||
self.check('---\ntext\n', conf)
|
||||
self.check('---\r\ntext\r\n', conf)
|
||||
|
||||
def test_unix_type(self):
|
||||
conf = 'new-lines: {type: unix}'
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check('\r\n', conf, problem=(1, 1))
|
||||
self.check('---\ntext\n', conf)
|
||||
self.check('---\r\ntext\r\n', conf, problem=(1, 4))
|
||||
|
||||
def test_dos_type(self):
|
||||
conf = 'new-lines: {type: dos}\n'
|
||||
self.check('', conf)
|
||||
self.check('\n', conf, problem=(1, 1))
|
||||
self.check('\r\n', conf)
|
||||
self.check('---\ntext\n', conf, problem=(1, 4))
|
||||
self.check('---\r\ntext\r\n', conf)
|
||||
48
tests/rules/test_trailing_spaces.py
Normal file
48
tests/rules/test_trailing_spaces.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.rules.common import RuleTestCase
|
||||
|
||||
|
||||
class TrailingSpacesTestCase(RuleTestCase):
|
||||
rule_id = 'trailing-spaces'
|
||||
|
||||
def test_disabled(self):
|
||||
conf = 'trailing-spaces: disable'
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check(' \n', conf)
|
||||
self.check('---\n'
|
||||
'some: text \n', conf)
|
||||
|
||||
def test_enabled(self):
|
||||
conf = 'trailing-spaces: {}'
|
||||
self.check('', conf)
|
||||
self.check('\n', conf)
|
||||
self.check(' \n', conf, problem=(1, 1))
|
||||
self.check('\t\t\t\n', conf, problem=(1, 1))
|
||||
self.check('---\n'
|
||||
'some: text \n', conf, problem=(2, 11))
|
||||
self.check('---\n'
|
||||
'some: text\t\n', conf, problem=(2, 11))
|
||||
|
||||
def test_with_dos_new_lines(self):
|
||||
conf = ('trailing-spaces: {}\n'
|
||||
'new-lines: {type: dos}\n')
|
||||
self.check('---\r\n'
|
||||
'some: text\r\n', conf)
|
||||
self.check('---\r\n'
|
||||
'some: text \r\n', conf, problem=(2, 11))
|
||||
93
tests/test_parser.py
Normal file
93
tests/test_parser.py
Normal file
@@ -0,0 +1,93 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2016 Adrien Vergé
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import unittest
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.parser import (line_generator, token_generator,
|
||||
token_or_line_generator, Line, Token)
|
||||
|
||||
|
||||
class ParserTestCase(unittest.TestCase):
|
||||
def test_line_generator(self):
|
||||
e = list(line_generator(''))
|
||||
self.assertEqual(len(e), 1)
|
||||
self.assertEqual(e[0].line_no, 1)
|
||||
self.assertEqual(e[0].start, 0)
|
||||
self.assertEqual(e[0].end, 0)
|
||||
|
||||
e = list(line_generator('\n'))
|
||||
self.assertEqual(len(e), 2)
|
||||
|
||||
e = list(line_generator(' \n'))
|
||||
self.assertEqual(len(e), 2)
|
||||
self.assertEqual(e[0].line_no, 1)
|
||||
self.assertEqual(e[0].start, 0)
|
||||
self.assertEqual(e[0].end, 1)
|
||||
|
||||
e = list(line_generator('\n\n'))
|
||||
self.assertEqual(len(e), 3)
|
||||
|
||||
e = list(line_generator('---\n'
|
||||
'this is line 1\n'
|
||||
'line 2\n'
|
||||
'\n'
|
||||
'3\n'))
|
||||
self.assertEqual(len(e), 6)
|
||||
self.assertEqual(e[0].line_no, 1)
|
||||
self.assertEqual(e[0].content, '---')
|
||||
self.assertEqual(e[2].content, 'line 2')
|
||||
self.assertEqual(e[3].content, '')
|
||||
self.assertEqual(e[5].line_no, 6)
|
||||
|
||||
e = list(line_generator('test with\n'
|
||||
'no newline\n'
|
||||
'at the end'))
|
||||
self.assertEqual(len(e), 3)
|
||||
self.assertEqual(e[2].line_no, 3)
|
||||
self.assertEqual(e[2].content, 'at the end')
|
||||
|
||||
def test_token_generator(self):
|
||||
e = list(token_generator(''))
|
||||
self.assertEqual(len(e), 2)
|
||||
self.assertEqual(e[0].prev, None)
|
||||
self.assertIsInstance(e[0].curr, yaml.Token)
|
||||
self.assertIsInstance(e[0].next, yaml.Token)
|
||||
self.assertEqual(e[1].prev, e[0].curr)
|
||||
self.assertEqual(e[1].curr, e[0].next)
|
||||
self.assertEqual(e[1].next, None)
|
||||
|
||||
e = list(token_generator('---\n'
|
||||
'k: v\n'))
|
||||
self.assertEqual(len(e), 9)
|
||||
self.assertIsInstance(e[3].curr, yaml.KeyToken)
|
||||
self.assertIsInstance(e[5].curr, yaml.ValueToken)
|
||||
|
||||
def test_token_or_line_generator(self):
|
||||
e = list(token_or_line_generator('---\n'
|
||||
'k: v\n'))
|
||||
self.assertEqual(len(e), 12)
|
||||
self.assertIsInstance(e[0], Token)
|
||||
self.assertIsInstance(e[0].curr, yaml.StreamStartToken)
|
||||
self.assertIsInstance(e[1], Token)
|
||||
self.assertIsInstance(e[1].curr, yaml.DocumentStartToken)
|
||||
self.assertIsInstance(e[2], Line)
|
||||
self.assertIsInstance(e[3].curr, yaml.BlockMappingStartToken)
|
||||
self.assertIsInstance(e[4].curr, yaml.KeyToken)
|
||||
self.assertIsInstance(e[6].curr, yaml.ValueToken)
|
||||
self.assertIsInstance(e[8], Line)
|
||||
self.assertIsInstance(e[11], Line)
|
||||
Reference in New Issue
Block a user