Skip to content

Commit 8b11ab6

Browse files
committed
Add additional YAML specific TCs
Tests for: - Basic key-value pairs - Nested structures - List items - Document structures with --- - Flow mapping structures - complex structures and configs
1 parent a514f18 commit 8b11ab6

File tree

1 file changed

+342
-0
lines changed

1 file changed

+342
-0
lines changed

tests/test_analyse_utils.py

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,3 +900,345 @@ def test_extract_rst(text, rst_markers, rst_text, positions):
900900
assert extracted_rst["rst_text"] == rst_text
901901
assert extracted_rst["start_idx"] == positions["start_idx"]
902902
assert extracted_rst["end_idx"] == positions["end_idx"]
903+
904+
905+
# ========== YAML-specific tests ==========
906+
907+
908+
@pytest.mark.parametrize(
909+
("code", "expected_structure"),
910+
[
911+
# Basic key-value pair
912+
(
913+
b"""
914+
# Comment before key
915+
database:
916+
host: localhost
917+
""",
918+
"database:",
919+
),
920+
# Comment in nested structure
921+
(
922+
b"""
923+
services:
924+
web:
925+
# Comment before image
926+
image: nginx:latest
927+
""",
928+
"image: nginx:latest",
929+
),
930+
# Comment before list item
931+
(
932+
b"""
933+
items:
934+
# Comment before list item
935+
- name: item1
936+
value: test
937+
""",
938+
"- name: item1",
939+
),
940+
# Comment in document structure
941+
(
942+
b"""---
943+
# Comment in document
944+
version: "3.8"
945+
services:
946+
app:
947+
build: .
948+
""",
949+
"version:",
950+
),
951+
# Flow mapping structure
952+
(
953+
b"""
954+
# Comment before flow mapping
955+
config: {debug: true, port: 8080}
956+
""",
957+
"config:",
958+
),
959+
],
960+
)
961+
def test_find_yaml_next_structure(code, expected_structure, init_yaml_tree_sitter):
962+
"""Test the find_yaml_next_structure function."""
963+
parser, query = init_yaml_tree_sitter
964+
comments = utils.extract_comments(code, parser, query)
965+
assert comments, "No comments found in the code"
966+
967+
next_structure = utils.find_yaml_next_structure(comments[0])
968+
assert next_structure, "No next structure found"
969+
structure_text = next_structure.text.decode("utf-8")
970+
assert expected_structure in structure_text
971+
972+
973+
@pytest.mark.parametrize(
974+
("code", "expected_structure"),
975+
[
976+
# Comment associated with key-value pair
977+
(
978+
b"""
979+
# Database configuration
980+
database:
981+
host: localhost
982+
port: 5432
983+
""",
984+
"database:",
985+
),
986+
# Comment associated with nested structure
987+
(
988+
b"""
989+
services:
990+
web:
991+
# Web service image
992+
image: nginx:latest
993+
ports:
994+
- "80:80"
995+
""",
996+
"image: nginx:latest",
997+
),
998+
# Comment associated with list item
999+
(
1000+
b"""
1001+
dependencies:
1002+
# First dependency
1003+
- name: redis
1004+
version: "6.0"
1005+
- name: postgres
1006+
version: "13"
1007+
""",
1008+
"- name: redis",
1009+
),
1010+
# Comment inside parent structure
1011+
(
1012+
b"""
1013+
app:
1014+
# Internal comment
1015+
name: myapp
1016+
version: "1.0"
1017+
""",
1018+
"name: myapp",
1019+
),
1020+
],
1021+
)
1022+
def test_find_yaml_associated_structure(
1023+
code, expected_structure, init_yaml_tree_sitter
1024+
):
1025+
"""Test the find_yaml_associated_structure function."""
1026+
parser, query = init_yaml_tree_sitter
1027+
comments = utils.extract_comments(code, parser, query)
1028+
assert comments, "No comments found in the code"
1029+
1030+
associated_structure = utils.find_yaml_associated_structure(comments[0])
1031+
assert associated_structure, "No associated structure found"
1032+
structure_text = associated_structure.text.decode("utf-8")
1033+
assert expected_structure in structure_text
1034+
1035+
1036+
@pytest.mark.parametrize(
1037+
("code", "expected_results"),
1038+
[
1039+
# Multiple comments in sequence
1040+
(
1041+
b"""
1042+
# First comment
1043+
# Second comment
1044+
database:
1045+
host: localhost
1046+
""",
1047+
["database:", "database:"], # Both comments should associate with database
1048+
),
1049+
# Comments at different nesting levels
1050+
(
1051+
b"""
1052+
# Top level comment
1053+
services:
1054+
web:
1055+
# Nested comment
1056+
image: nginx:latest
1057+
""",
1058+
["services:", "image: nginx:latest"],
1059+
),
1060+
],
1061+
)
1062+
def test_multiple_yaml_comments(code, expected_results, init_yaml_tree_sitter):
1063+
"""Test handling of multiple YAML comments in the same file."""
1064+
parser, query = init_yaml_tree_sitter
1065+
comments = utils.extract_comments(code, parser, query)
1066+
comments.sort(key=lambda x: x.start_point.row)
1067+
1068+
assert len(comments) == len(expected_results), (
1069+
f"Expected {len(expected_results)} comments, found {len(comments)}"
1070+
)
1071+
1072+
for i, comment in enumerate(comments):
1073+
associated_structure = utils.find_yaml_associated_structure(comment)
1074+
assert associated_structure, f"No associated structure found for comment {i}"
1075+
structure_text = associated_structure.text.decode("utf-8")
1076+
assert expected_results[i] in structure_text
1077+
1078+
1079+
@pytest.mark.parametrize(
1080+
("code", "has_structure"),
1081+
[
1082+
# Comment at end of file with no following structure
1083+
(
1084+
b"""
1085+
database:
1086+
host: localhost
1087+
# End of file comment
1088+
""",
1089+
True, # This will actually find the parent database structure
1090+
),
1091+
# Comment with only whitespace after
1092+
(
1093+
b"""
1094+
# Lonely comment
1095+
1096+
1097+
""",
1098+
False,
1099+
),
1100+
# Comment before valid structure
1101+
(
1102+
b"""
1103+
# Valid comment
1104+
key: value
1105+
""",
1106+
True,
1107+
),
1108+
],
1109+
)
1110+
def test_yaml_edge_cases(code, has_structure, init_yaml_tree_sitter):
1111+
"""Test edge cases in YAML comment processing."""
1112+
parser, query = init_yaml_tree_sitter
1113+
comments = utils.extract_comments(code, parser, query)
1114+
1115+
if comments:
1116+
structure = utils.find_yaml_associated_structure(comments[0])
1117+
if has_structure:
1118+
assert structure, "Expected to find associated structure"
1119+
else:
1120+
assert structure is None, "Expected no associated structure"
1121+
else:
1122+
assert not has_structure, "No comments found but structure was expected"
1123+
1124+
1125+
@pytest.mark.parametrize(
1126+
("code", "expected_structures"),
1127+
[
1128+
# Simpler nested YAML structure
1129+
(
1130+
b"""# Global configuration
1131+
version: "3.8"
1132+
1133+
# Services section
1134+
services:
1135+
web:
1136+
image: nginx:latest
1137+
# Port configuration
1138+
ports:
1139+
- "80:80"
1140+
""",
1141+
[
1142+
"version:", # Global configuration
1143+
"services:", # Services section
1144+
'- "80:80"', # Port configuration
1145+
],
1146+
),
1147+
],
1148+
)
1149+
def test_complex_yaml_structure(code, expected_structures, init_yaml_tree_sitter):
1150+
"""Test complex nested YAML structures with multiple comments."""
1151+
parser, query = init_yaml_tree_sitter
1152+
comments = utils.extract_comments(code, parser, query)
1153+
comments.sort(key=lambda x: x.start_point.row)
1154+
1155+
assert len(comments) == len(expected_structures), (
1156+
f"Expected {len(expected_structures)} comments, found {len(comments)}"
1157+
)
1158+
1159+
for i, comment in enumerate(comments):
1160+
associated_structure = utils.find_yaml_associated_structure(comment)
1161+
assert associated_structure, f"No associated structure found for comment {i}"
1162+
structure_text = associated_structure.text.decode("utf-8")
1163+
assert expected_structures[i] in structure_text, (
1164+
f"Expected '{expected_structures[i]}' in structure text: '{structure_text}'"
1165+
)
1166+
1167+
1168+
@pytest.mark.parametrize(
1169+
("code", "expected_type"),
1170+
[
1171+
# Block mapping pair
1172+
(
1173+
b"""
1174+
# Comment
1175+
key: value
1176+
""",
1177+
"block_mapping_pair",
1178+
),
1179+
# Block sequence item
1180+
(
1181+
b"""
1182+
items:
1183+
# Comment
1184+
- item1
1185+
""",
1186+
"block_sequence_item",
1187+
),
1188+
# Nested block mapping
1189+
(
1190+
b"""
1191+
services:
1192+
# Comment
1193+
web:
1194+
image: nginx
1195+
""",
1196+
"block_mapping_pair",
1197+
),
1198+
],
1199+
)
1200+
def test_yaml_structure_types(code, expected_type, init_yaml_tree_sitter):
1201+
"""Test that YAML structures return the correct node types."""
1202+
parser, query = init_yaml_tree_sitter
1203+
comments = utils.extract_comments(code, parser, query)
1204+
assert comments, "No comments found"
1205+
1206+
structure = utils.find_yaml_associated_structure(comments[0])
1207+
assert structure, "No associated structure found"
1208+
assert structure.type == expected_type, (
1209+
f"Expected type {expected_type}, got {structure.type}"
1210+
)
1211+
1212+
1213+
def test_yaml_document_structure(init_yaml_tree_sitter):
1214+
"""Test YAML document structure handling."""
1215+
code = b"""---
1216+
# Document comment
1217+
apiVersion: v1
1218+
kind: ConfigMap
1219+
metadata:
1220+
name: my-config
1221+
data:
1222+
# Data comment
1223+
config.yml: |
1224+
setting: value
1225+
"""
1226+
1227+
parser, query = init_yaml_tree_sitter
1228+
comments = utils.extract_comments(code, parser, query)
1229+
comments.sort(key=lambda x: x.start_point.row)
1230+
1231+
# Should find both comments
1232+
assert len(comments) >= 2, f"Expected at least 2 comments, found {len(comments)}"
1233+
1234+
# First comment should associate with apiVersion
1235+
first_structure = utils.find_yaml_associated_structure(comments[0])
1236+
assert first_structure, "No structure found for first comment"
1237+
first_text = first_structure.text.decode("utf-8")
1238+
assert "apiVersion:" in first_text
1239+
1240+
# Second comment should associate with config.yml
1241+
second_structure = utils.find_yaml_associated_structure(comments[1])
1242+
assert second_structure, "No structure found for second comment"
1243+
second_text = second_structure.text.decode("utf-8")
1244+
assert "config.yml:" in second_text

0 commit comments

Comments
 (0)