Rules: Add the 'braces' rule
This commit is contained in:
108
tests/rules/test_braces.py
Normal file
108
tests/rules/test_braces.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
# -*- 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 = 'braces'
|
||||||
|
|
||||||
|
def test_disabled(self):
|
||||||
|
conf = 'braces: disable'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict1: {}\n'
|
||||||
|
'dict2: { }\n'
|
||||||
|
'dict3: { a: 1, b}\n'
|
||||||
|
'dict4: {a: 1, b, c: 3 }\n'
|
||||||
|
'dict5: {a: 1, b, c: 3 }\n'
|
||||||
|
'dict6: { a: 1, b, c: 3 }\n'
|
||||||
|
'dict7: { a: 1, b, c: 3 }\n', conf)
|
||||||
|
|
||||||
|
def test_min_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b}\n', conf,
|
||||||
|
problem1=(2, 8), problem2=(2, 15))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
' a: 1,\n'
|
||||||
|
' b\n'
|
||||||
|
'}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: -1, min-spaces-inside: 3}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 9), problem2=(2, 17))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
|
||||||
|
def test_max_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: 0, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 8), problem2=(2, 16))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 10), problem2=(2, 20))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {\n'
|
||||||
|
' a: 1,\n'
|
||||||
|
' b\n'
|
||||||
|
'}\n', conf)
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 3, min-spaces-inside: -1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b }\n', conf,
|
||||||
|
problem1=(2, 11), problem2=(2, 23))
|
||||||
|
|
||||||
|
def test_min_and_max_spaces(self):
|
||||||
|
conf = 'braces: {max-spaces-inside: 0, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {}\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { }\n', conf, problem=(2, 8))
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b}\n', conf, problem=(2, 10))
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 1, min-spaces-inside: 1}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b, c: 3 }\n', conf, problem=(2, 8))
|
||||||
|
|
||||||
|
conf = 'braces: {max-spaces-inside: 2, min-spaces-inside: 0}'
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: {a: 1, b, c: 3 }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b, c: 3 }\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'dict: { a: 1, b, c: 3 }\n', conf, problem=(2, 10))
|
||||||
@@ -38,7 +38,7 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' p:\n'
|
' p:\n'
|
||||||
' - k3: >\n'
|
' - k3: >\n'
|
||||||
' val\n'
|
' val\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' - p: kdjf\n'
|
' - p: kdjf\n'
|
||||||
' - q: val0\n'
|
' - q: val0\n'
|
||||||
' q2:\n'
|
' q2:\n'
|
||||||
@@ -64,8 +64,8 @@ class ColonTestCase(RuleTestCase):
|
|||||||
' val\n'
|
' val\n'
|
||||||
' - k3: >\n'
|
' - k3: >\n'
|
||||||
' val\n'
|
' val\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' - o: { k1: v1 }\n'
|
' - o: {k1: v1}\n'
|
||||||
' q2:\n'
|
' q2:\n'
|
||||||
' - val1\n'
|
' - val1\n'
|
||||||
'...\n', conf)
|
'...\n', conf)
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
rules:
|
rules:
|
||||||
#block-sequence-indentation:
|
#block-sequence-indentation:
|
||||||
# present: yes
|
# present: yes
|
||||||
|
braces:
|
||||||
|
min-spaces-inside: 0
|
||||||
|
max-spaces-inside: 0
|
||||||
brackets:
|
brackets:
|
||||||
min-spaces-inside: 0
|
min-spaces-inside: 0
|
||||||
max-spaces-inside: 0
|
max-spaces-inside: 0
|
||||||
#braces:
|
|
||||||
# min-spaces-inside: 0
|
|
||||||
# max-spaces-inside: 0
|
|
||||||
colons:
|
colons:
|
||||||
max-spaces-before: 0
|
max-spaces-before: 0
|
||||||
max-spaces-after: 1
|
max-spaces-after: 1
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from yamllint.rules import (
|
from yamllint.rules import (
|
||||||
|
braces,
|
||||||
brackets,
|
brackets,
|
||||||
colons,
|
colons,
|
||||||
commas,
|
commas,
|
||||||
@@ -30,6 +31,7 @@ from yamllint.rules import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
_RULES = {
|
_RULES = {
|
||||||
|
braces.ID: braces,
|
||||||
brackets.ID: brackets,
|
brackets.ID: brackets,
|
||||||
colons.ID: colons,
|
colons.ID: colons,
|
||||||
commas.ID: commas,
|
commas.ID: commas,
|
||||||
|
|||||||
47
yamllint/rules/braces.py
Normal file
47
yamllint/rules/braces.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/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from yamllint.rules.common import spaces_after, spaces_before
|
||||||
|
|
||||||
|
|
||||||
|
ID = 'braces'
|
||||||
|
TYPE = 'token'
|
||||||
|
CONF = {'min-spaces-inside': int,
|
||||||
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
|
def check(conf, token, prev, next):
|
||||||
|
if isinstance(token, yaml.FlowMappingStartToken):
|
||||||
|
problem = spaces_after(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside braces',
|
||||||
|
max_desc='too many spaces inside braces')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
|
|
||||||
|
elif (isinstance(token, yaml.FlowMappingEndToken) and
|
||||||
|
(prev is None or
|
||||||
|
not isinstance(prev, yaml.FlowMappingStartToken))):
|
||||||
|
problem = spaces_before(token, prev, next,
|
||||||
|
min=conf['min-spaces-inside'],
|
||||||
|
max=conf['max-spaces-inside'],
|
||||||
|
min_desc='too few spaces inside braces',
|
||||||
|
max_desc='too many spaces inside braces')
|
||||||
|
if problem is not None:
|
||||||
|
yield problem
|
||||||
Reference in New Issue
Block a user