|
| 1 | +import unittest |
| 2 | +from pyquickhelper.helpgen import rst2html |
| 3 | +from sphinx_runpython.ext_test_case import ExtTestCase, ignore_warnings |
| 4 | +from sphinx_runpython.collapse.sphinx_collapse_extension import ( |
| 5 | + CollapseDirective, |
| 6 | + collapse_node, |
| 7 | + visit_collapse_node_rst, |
| 8 | + depart_collapse_node_rst, |
| 9 | +) |
| 10 | + |
| 11 | +tives = [ |
| 12 | + ( |
| 13 | + "collapse", |
| 14 | + CollapseDirective, |
| 15 | + collapse_node, |
| 16 | + visit_collapse_node_rst, |
| 17 | + depart_collapse_node_rst, |
| 18 | + ) |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +class TestCollapseExtension(ExtTestCase): |
| 23 | + @ignore_warnings(PendingDeprecationWarning) |
| 24 | + def test_collapse(self): |
| 25 | + content = """ |
| 26 | + before |
| 27 | +
|
| 28 | + .. collapse:: |
| 29 | +
|
| 30 | + this code shoud appear___ |
| 31 | +
|
| 32 | + after |
| 33 | + """.replace( |
| 34 | + " ", "" |
| 35 | + ) |
| 36 | + content = content.replace('u"', '"') |
| 37 | + |
| 38 | + # RST |
| 39 | + html = rst2html(content, writer="rst", keep_warnings=True, directives=tives) |
| 40 | + |
| 41 | + t1 = " this code shoud appear___" |
| 42 | + if t1 not in html: |
| 43 | + raise AssertionError(html) |
| 44 | + |
| 45 | + t1 = ".. collapse::" |
| 46 | + if t1 not in html: |
| 47 | + raise AssertionError(html) |
| 48 | + |
| 49 | + t1 = " :legend:" |
| 50 | + if t1 not in html: |
| 51 | + raise AssertionError(html) |
| 52 | + |
| 53 | + t1 = "after" |
| 54 | + if t1 not in html: |
| 55 | + raise AssertionError(html) |
| 56 | + |
| 57 | + t1 = ".. collapse:: :legend:" |
| 58 | + if t1 in html: |
| 59 | + raise AssertionError(html) |
| 60 | + |
| 61 | + # HTML |
| 62 | + html = rst2html(content, writer="custom", keep_warnings=True, directives=tives) |
| 63 | + |
| 64 | + t1 = "this code shoud appear" |
| 65 | + if t1 not in html: |
| 66 | + raise AssertionError(html) |
| 67 | + |
| 68 | + t1 = "after" |
| 69 | + if t1 not in html: |
| 70 | + raise AssertionError(html) |
| 71 | + |
| 72 | + t1 = 'if (x.style.display === "none")' |
| 73 | + if t1 not in html: |
| 74 | + raise AssertionError(html) |
| 75 | + |
| 76 | + t1 = "b.innerText = 'unhide';" |
| 77 | + if t1 not in html: |
| 78 | + raise AssertionError(html) |
| 79 | + |
| 80 | + @ignore_warnings(PendingDeprecationWarning) |
| 81 | + def test_collapse_legend(self): |
| 82 | + content = """ |
| 83 | + before |
| 84 | +
|
| 85 | + .. collapse:: |
| 86 | + :legend: ABC/abcd |
| 87 | +
|
| 88 | + this code shoud appear___ |
| 89 | +
|
| 90 | + after |
| 91 | + """.replace( |
| 92 | + " ", "" |
| 93 | + ) |
| 94 | + content = content.replace('u"', '"') |
| 95 | + |
| 96 | + # RST |
| 97 | + html = rst2html(content, writer="rst", keep_warnings=True, directives=tives) |
| 98 | + |
| 99 | + t1 = ":legend: ABC/abcd" |
| 100 | + if t1 not in html: |
| 101 | + raise AssertionError(html) |
| 102 | + |
| 103 | + t1 = ":hide:" |
| 104 | + if t1 in html: |
| 105 | + raise AssertionError(html) |
| 106 | + |
| 107 | + # HTML |
| 108 | + html = rst2html(content, writer="custom", keep_warnings=True, directives=tives) |
| 109 | + |
| 110 | + t1 = "b.innerText = 'abcd';" |
| 111 | + if t1 not in html: |
| 112 | + raise AssertionError(html) |
| 113 | + |
| 114 | + @ignore_warnings(PendingDeprecationWarning) |
| 115 | + def test_collapse_show(self): |
| 116 | + content = """ |
| 117 | + before |
| 118 | +
|
| 119 | + .. collapse:: |
| 120 | + :legend: ABC/abcd |
| 121 | + :hide: |
| 122 | +
|
| 123 | + this code shoud appear___ |
| 124 | +
|
| 125 | + after |
| 126 | + """.replace( |
| 127 | + " ", "" |
| 128 | + ) |
| 129 | + content = content.replace('u"', '"') |
| 130 | + |
| 131 | + # RST |
| 132 | + html = rst2html(content, writer="rst", keep_warnings=True, directives=tives) |
| 133 | + |
| 134 | + t1 = ":hide:" |
| 135 | + if t1 not in html: |
| 136 | + raise AssertionError(html) |
| 137 | + |
| 138 | + # HTML |
| 139 | + html = rst2html(content, writer="custom", keep_warnings=True, directives=tives) |
| 140 | + |
| 141 | + t1 = ">abcd<" |
| 142 | + if t1 not in html: |
| 143 | + raise AssertionError(html) |
| 144 | + |
| 145 | + t1 = '"display:none;"' |
| 146 | + if t1 not in html: |
| 147 | + raise AssertionError(html) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + unittest.main() |
0 commit comments