|
| 1 | +"""Tests related to theme's patched admonitions.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from sphinx.testing.util import SphinxTestApp |
| 5 | +from sphinx.errors import ExtensionError |
| 6 | + |
| 7 | +conf = [ |
| 8 | + { |
| 9 | + "name": "legacy", |
| 10 | + "color": (236, 64, 11), |
| 11 | + "icon": "fontawesome/solid/recycle", |
| 12 | + "classes": ["custom-class"], |
| 13 | + }, |
| 14 | + { |
| 15 | + "name": "attention", |
| 16 | + "classes": ["important"], |
| 17 | + }, |
| 18 | + { |
| 19 | + "name": "versionremoved", |
| 20 | + "classes": ["warning"], |
| 21 | + "override": True, |
| 22 | + }, |
| 23 | + { |
| 24 | + "name": "deprecated", |
| 25 | + "color": (0, 0, 0), |
| 26 | + "override": True, |
| 27 | + }, |
| 28 | + { |
| 29 | + # This is impractical, but it covers a condition in production code |
| 30 | + "name": "versionchanged", |
| 31 | + "override": True, |
| 32 | + }, |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def test_admonitions(immaterial_make_app): |
| 37 | + app: SphinxTestApp = immaterial_make_app( |
| 38 | + extra_conf=f"sphinx_immaterial_custom_admonitions={repr(conf)}", |
| 39 | + files={ |
| 40 | + "index.rst": """ |
| 41 | +The Test |
| 42 | +======== |
| 43 | +
|
| 44 | +.. deprecated:: 0.0.1 scheduled for removal |
| 45 | +
|
| 46 | +.. versionremoved:: 0.1.0 Code was removed! |
| 47 | + :collapsible: open |
| 48 | + :class: custom-class |
| 49 | +
|
| 50 | + Some rationale. |
| 51 | +
|
| 52 | +.. legacy:: |
| 53 | + :collapsible: open |
| 54 | + :class: custom-class |
| 55 | +
|
| 56 | + Some content. |
| 57 | +
|
| 58 | +.. legacy:: A |
| 59 | + :title: Custom title |
| 60 | +
|
| 61 | + Some content. |
| 62 | +
|
| 63 | +""", |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + app.build() |
| 68 | + assert not app._warning.getvalue() # type: ignore[attr-defined] |
| 69 | + |
| 70 | + |
| 71 | +def test_admonition_name_error(immaterial_make_app): |
| 72 | + # both exceptions must be raised here, so signify this using nested with statements |
| 73 | + with pytest.raises(ExtensionError): |
| 74 | + with pytest.raises(ValueError): |
| 75 | + immaterial_make_app( |
| 76 | + extra_conf="sphinx_immaterial_custom_admonitions=[{" |
| 77 | + '"name":"legacy!","classes":["note"]}]', |
| 78 | + files={ |
| 79 | + "index.rst": "", |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +def test_admonitions_warnings(immaterial_make_app): |
| 85 | + app: SphinxTestApp = immaterial_make_app( |
| 86 | + files={ |
| 87 | + "index.rst": """ |
| 88 | +The Test |
| 89 | +======== |
| 90 | +
|
| 91 | +.. note:: |
| 92 | + :collapsible: |
| 93 | + :no-title: |
| 94 | +
|
| 95 | + Some content. |
| 96 | +
|
| 97 | +.. deprecated:: 0.1.0 |
| 98 | + :collapsible: |
| 99 | +
|
| 100 | +""", |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + app.build() |
| 105 | + warnings = app._warning.getvalue() # type: ignore[attr-defined] |
| 106 | + assert warnings |
| 107 | + assert "title is needed for collapsible admonitions" in warnings |
| 108 | + assert "Expected 2 arguments before content in deprecated directive" in warnings |
| 109 | + |
| 110 | + |
| 111 | +def test_todo_admonition(immaterial_make_app): |
| 112 | + app: SphinxTestApp = immaterial_make_app( |
| 113 | + extra_conf="extensions.append('sphinx.ext.todo')", |
| 114 | + files={ |
| 115 | + "index.rst": """ |
| 116 | +The Test |
| 117 | +======== |
| 118 | +
|
| 119 | +.. todo:: |
| 120 | + Some content. |
| 121 | +
|
| 122 | +""", |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + app.build() |
| 127 | + assert not app._warning.getvalue() # type: ignore[attr-defined] |
0 commit comments