diff --git a/tests/rules/test_indentation.py b/tests/rules/test_indentation.py index a8f91fd..2beeb58 100644 --- a/tests/rules/test_indentation.py +++ b/tests/rules/test_indentation.py @@ -300,6 +300,87 @@ class IndentationStackTestCase(RuleTestCase): ' Scalar B_MAP:0\n' ' BEnd \n') + self.assertMultiLineEqual( + self.full_stack('sequence: &anchor\n' + '- entry\n' + '- &anchor\n' + ' - nested\n'), + 'BMapStart B_MAP:0\n' + ' Key B_MAP:0 KEY:0\n' + ' Scalar B_MAP:0 KEY:0\n' + ' Value B_MAP:0 KEY:0 VAL:2\n' + ' Anchor B_MAP:0 KEY:0 VAL:2\n' + # missing BSeqStart here + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + ' Scalar B_MAP:0 KEY:0 VAL:2 B_SEQ:0\n' + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + ' Anchor B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + 'BSeqStart B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2 B_ENT:4\n' + ' Scalar B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEnd B_MAP:0\n' + # missing BEnd here + ' BEnd \n') + + def test_tags(self): + self.assertMultiLineEqual( + self.full_stack('key: !!tag value\n'), + 'BMapStart B_MAP:0\n' + ' Key B_MAP:0 KEY:0\n' + ' Scalar B_MAP:0 KEY:0\n' + ' Value B_MAP:0 KEY:0 VAL:5\n' + ' Tag B_MAP:0 KEY:0 VAL:5\n' + ' Scalar B_MAP:0\n' + ' BEnd \n') + + self.assertMultiLineEqual( + self.full_stack('- !!map # Block collection\n' + ' foo : bar\n'), + 'BSeqStart B_SEQ:0\n' + ' BEntry B_SEQ:0 B_ENT:2\n' + ' Tag B_SEQ:0 B_ENT:2\n' + 'BMapStart B_SEQ:0 B_ENT:2 B_MAP:2\n' + ' Key B_SEQ:0 B_ENT:2 B_MAP:2 KEY:2\n' + ' Scalar B_SEQ:0 B_ENT:2 B_MAP:2 KEY:2\n' + ' Value B_SEQ:0 B_ENT:2 B_MAP:2 KEY:2 VAL:8\n' + ' Scalar B_SEQ:0 B_ENT:2 B_MAP:2\n' + ' BEnd B_SEQ:0\n' + ' BEnd \n') + + self.assertMultiLineEqual( + self.full_stack('- !!seq\n' + ' - nested item\n'), + 'BSeqStart B_SEQ:0\n' + ' BEntry B_SEQ:0 B_ENT:2\n' + ' Tag B_SEQ:0 B_ENT:2\n' + 'BSeqStart B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEntry B_SEQ:0 B_ENT:2 B_SEQ:2 B_ENT:4\n' + ' Scalar B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEnd B_SEQ:0\n' + ' BEnd \n') + + self.assertMultiLineEqual( + self.full_stack('sequence: !!seq\n' + '- entry\n' + '- !!seq\n' + ' - nested\n'), + 'BMapStart B_MAP:0\n' + ' Key B_MAP:0 KEY:0\n' + ' Scalar B_MAP:0 KEY:0\n' + ' Value B_MAP:0 KEY:0 VAL:2\n' + ' Tag B_MAP:0 KEY:0 VAL:2\n' + # missing BSeqStart here + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + ' Scalar B_MAP:0 KEY:0 VAL:2 B_SEQ:0\n' + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + ' Tag B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2\n' + 'BSeqStart B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEntry B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2 B_ENT:4\n' + ' Scalar B_MAP:0 KEY:0 VAL:2 B_SEQ:0 B_ENT:2 B_SEQ:2\n' + ' BEnd B_MAP:0\n' + # missing BEnd here + ' BEnd \n') + class IndentationTestCase(RuleTestCase): rule_id = 'indentation' @@ -1014,6 +1095,35 @@ class IndentationTestCase(RuleTestCase): ' truc\n' ' - k: *a\n', conf) + def test_tags(self): + conf = 'indentation: {spaces: 2}' + self.check('---\n' + '-\n' + ' "flow in block"\n' + '- >\n' + ' Block scalar\n' + '- !!map # Block collection\n' + ' foo: bar\n', conf) + + conf = 'indentation: {spaces: 2, indent-sequences: no}' + self.check('---\n' + 'sequence: !!seq\n' + '- entry\n' + '- !!seq\n' + ' - nested\n', conf) + self.check('---\n' + 'mapping: !!map\n' + ' foo: bar\n' + 'Block style: !!map\n' + ' Clark: Evans\n' + ' Ingy: döt Net\n' + ' Oren: Ben-Kiki\n', conf) + self.check('---\n' + 'Flow style: !!map {Clark: Evans, Ingy: döt Net}\n' + 'Block style: !!seq\n' + '- Clark Evans\n' + '- Ingy döt Net\n', conf) + class ScalarIndentationTestCase(RuleTestCase): rule_id = 'indentation' diff --git a/yamllint/rules/indentation.py b/yamllint/rules/indentation.py index 68e8157..8ff74fd 100644 --- a/yamllint/rules/indentation.py +++ b/yamllint/rules/indentation.py @@ -367,10 +367,13 @@ def check(conf, token, prev, next, nextnext, context): elif isinstance(token, yaml.ValueToken): assert context['stack'][-1].type == KEY - # Special case: + # Special cases: # key: &anchor # value - if isinstance(next, yaml.AnchorToken): + # and: + # key: !!tag + # value + if isinstance(next, (yaml.AnchorToken, yaml.TagToken)): if (next.start_mark.line == prev.start_mark.line and next.start_mark.line < nextnext.start_mark.line): next = nextnext @@ -440,8 +443,8 @@ def check(conf, token, prev, next, nextnext, context): elif (context['stack'][-1].type == B_ENT and not isinstance(token, yaml.BlockEntryToken) and context['stack'][-2].implicit_block_seq and - not isinstance(next, yaml.ScalarToken)): - # isinstance(next, yaml.KeyToken)): + not isinstance(token, (yaml.AnchorToken, yaml.TagToken)) and + not isinstance(next, yaml.BlockEntryToken)): context['stack'].pop() context['stack'].pop() @@ -451,10 +454,10 @@ def check(conf, token, prev, next, nextnext, context): elif (context['stack'][-1].type == VAL and not isinstance(token, yaml.ValueToken) and - not isinstance(token, yaml.AnchorToken)): - assert context['stack'][-2].type == KEY - context['stack'].pop() - context['stack'].pop() + not isinstance(token, (yaml.AnchorToken, yaml.TagToken))): + assert context['stack'][-2].type == KEY + context['stack'].pop() + context['stack'].pop() elif (context['stack'][-1].type == KEY and isinstance(next, (yaml.BlockEndToken,