|
1 | 1 | # pylint: disable=C0103,C0114,C0115,C0116 |
2 | 2 | from unittest import TestCase |
3 | 3 |
|
4 | | -from hcl2.query.body import DocumentView, BodyView |
| 4 | +from hcl2.query.body import DocumentView, BodyView, _collect_leading_comments |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class TestDocumentView(TestCase): |
@@ -94,3 +94,82 @@ def test_attributes(self): |
94 | 94 | body = doc.body |
95 | 95 | attrs = body.attributes() |
96 | 96 | self.assertEqual(len(attrs), 2) |
| 97 | + |
| 98 | + |
| 99 | +class TestCollectLeadingComments(TestCase): |
| 100 | + """Tests for _collect_leading_comments helper.""" |
| 101 | + |
| 102 | + def _body(self, hcl: str): |
| 103 | + doc = DocumentView.parse(hcl) |
| 104 | + return doc.body.raw # BodyRule |
| 105 | + |
| 106 | + def test_comment_before_block(self): |
| 107 | + body = self._body('# about resource\nresource "a" "b" {}\n') |
| 108 | + # Find the BlockRule child |
| 109 | + from hcl2.rules.base import BlockRule |
| 110 | + |
| 111 | + for child in body.children: |
| 112 | + if isinstance(child, BlockRule): |
| 113 | + result = _collect_leading_comments(body, child.index) |
| 114 | + self.assertEqual(result, [{"value": "about resource"}]) |
| 115 | + return |
| 116 | + self.fail("No BlockRule found") |
| 117 | + |
| 118 | + def test_comment_before_attribute(self): |
| 119 | + body = self._body("# about x\nx = 1\n") |
| 120 | + from hcl2.rules.base import AttributeRule |
| 121 | + |
| 122 | + for child in body.children: |
| 123 | + if isinstance(child, AttributeRule): |
| 124 | + result = _collect_leading_comments(body, child.index) |
| 125 | + self.assertEqual(result, [{"value": "about x"}]) |
| 126 | + return |
| 127 | + self.fail("No AttributeRule found") |
| 128 | + |
| 129 | + def test_stops_at_previous_semantic_sibling(self): |
| 130 | + body = self._body("x = 1\n# about y\ny = 2\n") |
| 131 | + from hcl2.rules.base import AttributeRule |
| 132 | + |
| 133 | + attrs = [c for c in body.children if isinstance(c, AttributeRule)] |
| 134 | + # First attribute (x) — comment before it is empty (only bare newlines) |
| 135 | + result_x = _collect_leading_comments(body, attrs[0].index) |
| 136 | + self.assertEqual(result_x, []) |
| 137 | + # Second attribute (y) — has "about y" above it |
| 138 | + result_y = _collect_leading_comments(body, attrs[1].index) |
| 139 | + self.assertEqual(result_y, [{"value": "about y"}]) |
| 140 | + |
| 141 | + def test_bare_newlines_not_collected(self): |
| 142 | + body = self._body("\n\nx = 1\n") |
| 143 | + from hcl2.rules.base import AttributeRule |
| 144 | + |
| 145 | + for child in body.children: |
| 146 | + if isinstance(child, AttributeRule): |
| 147 | + result = _collect_leading_comments(body, child.index) |
| 148 | + self.assertEqual(result, []) |
| 149 | + return |
| 150 | + self.fail("No AttributeRule found") |
| 151 | + |
| 152 | + def test_multiple_comments_in_order(self): |
| 153 | + body = self._body("# first\n# second\nx = 1\n") |
| 154 | + from hcl2.rules.base import AttributeRule |
| 155 | + |
| 156 | + for child in body.children: |
| 157 | + if isinstance(child, AttributeRule): |
| 158 | + result = _collect_leading_comments(body, child.index) |
| 159 | + self.assertEqual(result, [{"value": "first"}, {"value": "second"}]) |
| 160 | + return |
| 161 | + self.fail("No AttributeRule found") |
| 162 | + |
| 163 | + def test_comment_between_two_blocks(self): |
| 164 | + body = self._body('resource "a" "b" {}\n# about variable\nvariable "c" {}\n') |
| 165 | + from hcl2.rules.base import BlockRule |
| 166 | + |
| 167 | + blocks = [c for c in body.children if isinstance(c, BlockRule)] |
| 168 | + self.assertEqual(len(blocks), 2) |
| 169 | + # First block: no leading comments |
| 170 | + self.assertEqual(_collect_leading_comments(body, blocks[0].index), []) |
| 171 | + # Second block: "about variable" |
| 172 | + self.assertEqual( |
| 173 | + _collect_leading_comments(body, blocks[1].index), |
| 174 | + [{"value": "about variable"}], |
| 175 | + ) |
0 commit comments