diff --git a/tests/rules/test_indentation.py b/tests/rules/test_indentation.py index ca86665..40b38aa 100644 --- a/tests/rules/test_indentation.py +++ b/tests/rules/test_indentation.py @@ -169,7 +169,9 @@ class IndentationTestCase(RuleTestCase): '- b\n' '- c\n', conf, problem=(6, 1, 'syntax')) - def test_flow_mappings(self): + def test_direct_flows(self): + # flow: [ ... + # ] conf = 'indentation: {spaces: 2}' self.check('---\n' 'a: {x: 1,\n' @@ -183,10 +185,46 @@ class IndentationTestCase(RuleTestCase): 'a: {x: 1,\n' ' y,\n' ' z: 1}\n', conf, problem=(3, 6)) + self.check('---\n' + 'a: {x: 1,\n' + ' y, z: 1}\n', conf, problem=(3, 3)) + self.check('---\n' + 'a: {x: 1,\n' + ' y, z: 1\n' + '}\n', conf) self.check('---\n' 'a: {x: 1,\n' ' y, z: 1\n' '}\n', conf, problem=(3, 3)) + self.check('---\n' + 'a: [x,\n' + ' y,\n' + ' z]\n', conf) + self.check('---\n' + 'a: [x,\n' + ' y,\n' + ' z]\n', conf, problem=(3, 4)) + self.check('---\n' + 'a: [x,\n' + ' y,\n' + ' z]\n', conf, problem=(3, 6)) + self.check('---\n' + 'a: [x,\n' + ' y, z]\n', conf, problem=(3, 3)) + self.check('---\n' + 'a: [x,\n' + ' y, z\n' + ']\n', conf) + self.check('---\n' + 'a: [x,\n' + ' y, z\n' + ']\n', conf, problem=(3, 3)) + + def test_broken_flows(self): + # flow: [ + # ... + # ] + conf = 'indentation: {spaces: 2}' self.check('---\n' 'a: {\n' ' x: 1,\n' @@ -206,25 +244,6 @@ class IndentationTestCase(RuleTestCase): ' x: 1,\n' ' y, z: 1\n' ' }\n', conf, problem=(5, 3)) - - def test_flow_sequences(self): - conf = 'indentation: {spaces: 2}' - self.check('---\n' - 'a: [x,\n' - ' y,\n' - ' z]\n', conf) - self.check('---\n' - 'a: [x,\n' - ' y,\n' - ' z]\n', conf, problem=(3, 4)) - self.check('---\n' - 'a: [x,\n' - ' y,\n' - ' z]\n', conf, problem=(3, 6)) - self.check('---\n' - 'a: [x,\n' - ' y, z\n' - ']\n', conf, problem=(3, 3)) self.check('---\n' 'a: [\n' ' x,\n' @@ -244,6 +263,138 @@ class IndentationTestCase(RuleTestCase): ' x,\n' ' y, z\n' ' ]\n', conf, problem=(5, 3)) + self.check('---\n' + 'obj: {\n' + ' a: 1,\n' + ' b: 2,\n' + ' c: 3\n' + '}\n', conf, problem1=(4, 4), problem2=(5, 2)) + self.check('---\n' + 'list: [\n' + ' 1,\n' + ' 2,\n' + ' 3\n' + ']\n', conf, problem1=(4, 4), problem2=(5, 2)) + self.check('---\n' + 'top:\n' + ' rules: [\n' + ' 1, 2,\n' + ' ]\n', conf) + self.check('---\n' + 'top:\n' + ' rules: [\n' + ' 1, 2,\n' + ']\n' + ' rulez: [\n' + ' 1, 2,\n' + ' ]\n', conf, problem1=(5, 1), problem2=(8, 5)) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' here: {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' here: {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n' + ' there: {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf, problem1=(7, 7), problem2=(11, 3)) + + def test_cleared_flows(self): + # flow: + # [ + # ... + # ] + conf = 'indentation: {spaces: 2}' + self.check('---\n' + 'top:\n' + ' rules:\n' + ' {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf, problem=(5, 8)) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf, problem=(4, 4)) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf, problem=(7, 4)) + self.check('---\n' + 'top:\n' + ' rules:\n' + ' {\n' + ' foo: 1,\n' + ' bar: 2\n' + ' }\n', conf, problem=(7, 6)) + self.check('---\n' + 'top:\n' + ' [\n' + ' a, b, c\n' + ' ]\n', conf) + self.check('---\n' + 'top:\n' + ' [\n' + ' a, b, c\n' + ' ]\n', conf, problem=(4, 6)) + self.check('---\n' + 'top:\n' + ' [\n' + ' a, b, c\n' + ' ]\n', conf, problem=(3, 4)) + self.check('---\n' + 'top:\n' + ' [\n' + ' a, b, c\n' + ' ]\n', conf, problem=(5, 4)) + self.check('---\n' + 'top:\n' + ' rules: [\n' + ' {\n' + ' foo: 1\n' + ' },\n' + ' {\n' + ' foo: 2,\n' + ' bar: [\n' + ' a, b, c\n' + ' ],\n' + ' },\n' + ' ]\n', conf) + self.check('---\n' + 'top:\n' + ' rules: [\n' + ' {\n' + ' foo: 1\n' + ' },\n' + ' {\n' + ' foo: 2,\n' + ' bar: [\n' + ' a, b, c\n' + ' ],\n' + ' },\n' + ']\n', conf, problem1=(5, 6), problem2=(6, 6), + problem3=(9, 9), problem4=(11, 7), problem5=(13, 1)) def test_under_indented(self): conf = 'indentation: {spaces: 2, indent-sequences: yes}' @@ -438,21 +589,6 @@ class IndentationTestCase(RuleTestCase): 'document-start: disable\n') self.check(' a: 1\n', conf, problem=(1, 3)) - def test_broken_inline_flows(self): - conf = 'indentation: {spaces: 2}' - self.check('---\n' - 'obj: {\n' - ' a: 1,\n' - ' b: 2,\n' - ' c: 3\n' - '}\n', conf, problem1=(4, 4), problem2=(5, 2)) - self.check('---\n' - 'list: [\n' - ' 1,\n' - ' 2,\n' - ' 3\n' - ']\n', conf, problem1=(4, 4), problem2=(5, 2)) - def test_explicit_block_mappings(self): conf = 'indentation: {spaces: 4}' self.check('---\n' diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index 60b880c..d195e25 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -158,9 +158,10 @@ ROOT, MAP, B_SEQ, F_SEQ, B_ENT, KEY, VAL = range(7) class Parent(object): - def __init__(self, type, indent): + def __init__(self, type, indent, line_indent=None): self.type = type self.indent = indent + self.line_indent = line_indent self.explicit_key = False @@ -250,7 +251,7 @@ def check(conf, token, prev, next, context): if isinstance(token, (yaml.FlowMappingEndToken, yaml.FlowSequenceEndToken)): - expected = 0 + expected = context['stack'][-1].line_indent elif (context['stack'][-1].type == KEY and context['stack'][-1].explicit_key and not isinstance(token, yaml.ValueToken)): @@ -301,11 +302,12 @@ def check(conf, token, prev, next, context): indent = next.start_mark.column else: # - { - # a: 1, b: 2 - # } + # a: 1, b: 2 + # } indent = context['cur_line_indent'] + conf['spaces'] - context['stack'].append(Parent(MAP, indent)) + context['stack'].append(Parent(MAP, indent, + line_indent=context['cur_line_indent'])) elif isinstance(token, yaml.BlockSequenceStartToken): # - - a @@ -344,7 +346,8 @@ def check(conf, token, prev, next, context): # ] indent = context['cur_line_indent'] + conf['spaces'] - context['stack'].append(Parent(F_SEQ, indent)) + context['stack'].append(Parent(F_SEQ, indent, + line_indent=context['cur_line_indent'])) elif isinstance(token, (yaml.BlockEndToken, yaml.FlowMappingEndToken,