[docs]  1# @Test suite for source analysis configuration validation, TEST_CONF_1, test, [IMPL_OLP_1]
  2import pytest
  3
  4from sphinx_codelinks.config import OneLineCommentStyle, SourceAnalyseConfig
  5
  6from .conftest import TEST_DIR
  7
  8
  9@pytest.mark.parametrize(
 10    ("analyse_config", "result"),
 11    [
 12        (
 13            SourceAnalyseConfig(
 14                src_files=[
 15                    TEST_DIR / "data" / "dcdc" / "charge" / "demo_1.cpp",
 16                ],
 17                src_dir=TEST_DIR / "data" / "dcdc",
 18                comment_type=123,
 19            ),
 20            [
 21                "Schema validation error in field 'comment_type': 123 is not of type 'string'",
 22            ],
 23        ),
 24        (
 25            SourceAnalyseConfig(
 26                src_files=None,
 27                src_dir=TEST_DIR / "data" / "dcdc",
 28                comment_type=123,
 29            ),
 30            [
 31                "Schema validation error in field 'comment_type': 123 is not of type 'string'",
 32                "Schema validation error in field 'src_files': None is not of type 'array'",
 33            ],
 34        ),
 35    ],
 36)
 37def test_config_schema_validator_negative(analyse_config, result):
 38    errors = analyse_config.check_schema()
 39    assert sorted(errors) == sorted(result)
 40
 41
 42@pytest.mark.parametrize(
 43    "oneline_config, result",
 44    [
 45        (
 46            OneLineCommentStyle(
 47                start_sequence="[[",
 48                end_sequence="]]",
 49                field_split_char=",",
 50                needs_fields=[
 51                    {"name": "title"},
 52                    {"name": "id"},
 53                    {"name": "type", "default": "impl"},
 54                    {"name": "links", "type": "list[]", "default": []},  # wrong type
 55                ],
 56            ),
 57            [
 58                "Schema validation error in need_fields 'links': 'list[]' is not one of ['str', 'list[str]']"
 59            ],
 60        ),
 61        (
 62            OneLineCommentStyle(
 63                start_sequence="[[",
 64                end_sequence="]]",
 65                field_split_char=",",
 66                needs_fields=[
 67                    {"name": "title"},
 68                    {"name": "id"},
 69                    {"name": "type", "default": 123},  # int is invalid
 70                    {"name": "links", "type": "list[str]", "default": []},
 71                ],
 72            ),
 73            [
 74                "Schema validation error in need_fields 'type': 123 is not of type 'string'"
 75            ],
 76        ),
 77        (
 78            OneLineCommentStyle(
 79                start_sequence="[[",
 80                end_sequence="]]",
 81                field_split_char=",",
 82                needs_fields=[
 83                    {"name": "title", "qwe": "qwe"},  # invalid qwe filed
 84                    {"name": "id"},
 85                    {"name": "type", "default": "impl"},
 86                    {"name": "links", "type": "list[str]", "default": []},
 87                ],
 88            ),
 89            [
 90                "Schema validation error in need_fields 'title': Additional properties are not allowed ('qwe' was unexpected)"
 91            ],
 92        ),
 93        (
 94            OneLineCommentStyle(
 95                start_sequence="[[",
 96                end_sequence="]]",
 97                field_split_char=",",
 98                needs_fields=[
 99                    {"name": "title"},
100                    {"name": "id"},
101                    {
102                        "name": "type",
103                        "type: ": "list[str]",
104                        "default": "impl",
105                    },  # wring combination of type and default
106                    {"name": "links", "type": "list[str]", "default": []},
107                ],
108            ),
109            [
110                "Schema validation error in need_fields 'type': Additional properties are not allowed ('type: ' was unexpected)"
111            ],
112        ),
113        (
114            OneLineCommentStyle(
115                start_sequence="[[",
116                end_sequence="]]",
117                field_split_char=",",
118                needs_fields=[
119                    {"name": "id"}  # "title" and "type" are not given
120                ],
121            ),
122            ["Missing required fields: ['title', 'type']"],
123        ),
124        (
125            OneLineCommentStyle(
126                start_sequence="[[",
127                end_sequence="]]",
128                field_split_char=",",
129                needs_fields=[
130                    {"name": "id"},
131                    {"name": "id"},  # duplicate
132                ],
133            ),
134            [
135                "Missing required fields: ['title', 'type']",
136                "Field 'id' is defined multiple times.",
137            ],
138        ),
139        (
140            OneLineCommentStyle(
141                start_sequence=1234,  # wrong type
142                end_sequence=5678,
143                field_split_char=2222,
144                needs_fields=[
145                    {"name": "id"},
146                ],
147            ),
148            [
149                "Schema validation error in field 'field_split_char': 2222 is not of type 'string'",
150                "Schema validation error in field 'end_sequence': 5678 is not of type 'string'",
151                "Schema validation error in field 'start_sequence': 1234 is not of type 'string'",
152                "Missing required fields: ['title', 'type']",
153            ],
154        ),
155    ],
156)
157def test_oneline_schema_validator_negative(oneline_config, result):
158    errors = oneline_config.check_fields_configuration()
159    assert sorted(errors) == sorted(result)
160
161
162@pytest.mark.parametrize(
163    "oneline_config",
164    [
165        OneLineCommentStyle(
166            start_sequence="[[",
167            end_sequence="]]",
168            field_split_char=",",
169            needs_fields=[
170                {"name": "title"},
171                {"name": "id"},
172                {"name": "type", "default": "impl"},
173                {"name": "links", "type": "list[str]", "default": []},
174            ],
175        ),
176        OneLineCommentStyle(
177            start_sequence="[[",
178            end_sequence="]]",
179            field_split_char=",",
180            needs_fields=[
181                {"name": "title"},  # minimum need_fields config
182                {"name": "type"},
183            ],
184        ),
185        OneLineCommentStyle(
186            needs_fields=[  # minimum config
187                {"name": "title"},
188                {"name": "type"},
189            ],
190        ),
191    ],
192)
193def test_oneline_schema_validator_positive(oneline_config):
194    assert len(oneline_config.check_fields_configuration()) == 0