[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            OneLineCommentStyle(
157                start_sequence="@need",
158                end_sequence="\n",
159                field_split_char=",",
160                needs_fields=[
161                    {"name": "id"},
162                    {"name": "implements", "type": "list[str]", "default": []},
163                    {"name": "type", "default": "impl"},
164                    {"name": "title"},  # required after optional
165                ],
166            ),
167            [
168                "Field 'title' without a default follows field 'implements' which has a default. "
169                "Fields without defaults must be defined before fields with defaults.",
170            ],
171        ),
172    ],
173)
174def test_oneline_schema_validator_negative(oneline_config, result):
175    errors = oneline_config.check_fields_configuration()
176    assert sorted(errors) == sorted(result)
177
178
179@pytest.mark.parametrize(
180    "oneline_config",
181    [
182        OneLineCommentStyle(
183            start_sequence="[[",
184            end_sequence="]]",
185            field_split_char=",",
186            needs_fields=[
187                {"name": "title"},
188                {"name": "id"},
189                {"name": "type", "default": "impl"},
190                {"name": "links", "type": "list[str]", "default": []},
191            ],
192        ),
193        OneLineCommentStyle(
194            start_sequence="[[",
195            end_sequence="]]",
196            field_split_char=",",
197            needs_fields=[
198                {"name": "title"},  # minimum need_fields config
199                {"name": "type"},
200            ],
201        ),
202        OneLineCommentStyle(
203            needs_fields=[  # minimum config
204                {"name": "title"},
205                {"name": "type"},
206            ],
207        ),
208    ],
209)
210def test_oneline_schema_validator_positive(oneline_config):
211    assert len(oneline_config.check_fields_configuration()) == 0