|
| 1 | +import pytest, copy |
| 2 | +from quartodoc.autosummary import Builder |
| 3 | + |
| 4 | +EXAMPLE_SECTIONS = [ |
| 5 | + {'title': 'Preperation Functions', |
| 6 | + 'desc': 'Functions that fetch objects.\nThey prepare a representation of the site.\n', |
| 7 | + 'contents': ['Auto', 'blueprint', 'collect', 'get_object', 'preview']}, |
| 8 | + {'title': 'Docstring Renderers', |
| 9 | + 'desc': 'Renderers convert parsed docstrings into a target format, like markdown.\n', |
| 10 | + 'contents': [{'name': 'MdRenderer', 'children': 'linked'}, |
| 11 | + {'name': 'MdRenderer.render', 'dynamic': True}, |
| 12 | + {'name': 'MdRenderer.render_annotation', 'dynamic': True}, |
| 13 | + {'name': 'MdRenderer.render_header', 'dynamic': True}] |
| 14 | + }, |
| 15 | + {'title': 'API Builders', |
| 16 | + 'desc': 'Builders build documentation. They tie all the pieces\nof quartodoc together.\n', |
| 17 | + 'contents': [{'kind': 'auto', 'name': 'Builder', 'members': []}, |
| 18 | + 'Builder.from_quarto_config', 'Builder.build', 'Builder.write_index'] |
| 19 | + }, |
| 20 | + ] |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def sections(): |
| 24 | + return copy.deepcopy(EXAMPLE_SECTIONS) |
| 25 | + |
| 26 | +def check_ValueError(sections): |
| 27 | + "Check that a ValueError is raised when creating a `Builder` instance. Return the error message as a string." |
| 28 | + with pytest.raises(ValueError) as e: |
| 29 | + Builder(sections=sections, package='quartodoc') |
| 30 | + return str(e.value) |
| 31 | + |
| 32 | +def test_valid_yaml(sections): |
| 33 | + "Test that valid YAML passes validation" |
| 34 | + Builder(sections=sections, package='quartodoc') |
| 35 | + |
| 36 | +def test_missing_title(sections): |
| 37 | + "Test that missing title raises an error" |
| 38 | + del sections[0]['title'] |
| 39 | + msg = check_ValueError(sections) |
| 40 | + assert '- Missing field `title` for element 0 in the list for `sections`' in msg |
| 41 | + |
| 42 | +def test_missing_desc(sections): |
| 43 | + "Test that a missing description raises an error" |
| 44 | + sections = copy.deepcopy(EXAMPLE_SECTIONS) |
| 45 | + del sections[2]['desc'] |
| 46 | + msg = check_ValueError(sections) |
| 47 | + assert '- Missing field `desc` for element 2 in the list for `sections`' in msg |
| 48 | + |
| 49 | +def test_missing_name_contents_1(sections): |
| 50 | + "Test that a missing name in contents raises an error" |
| 51 | + del sections[2]['contents'][0]['name'] |
| 52 | + msg = check_ValueError(sections) |
| 53 | + assert '- Missing field `name` for element 0 in the list for `contents` located in element 2 in the list for `sections`' in msg |
| 54 | + |
| 55 | +def test_missing_name_contents_2(sections): |
| 56 | + "Test that a missing name in contents raises an error in a different section." |
| 57 | + del sections[1]['contents'][0]['name'] |
| 58 | + msg = check_ValueError(sections) |
| 59 | + assert '- Missing field `name` for element 0 in the list for `contents` located in element 1 in the list for `sections`' in msg |
| 60 | + |
| 61 | +def test_misplaced_kindpage(sections): |
| 62 | + "Test that a misplaced kind: page raises an error" |
| 63 | + sections[0]['kind'] = 'page' |
| 64 | + msg = check_ValueError(sections) |
| 65 | + assert ' - Missing field `path` for element 0 in the list for `sections`, which you need when setting `kind: page`.' in msg |
0 commit comments