|
| 1 | +import pytest |
| 2 | + |
| 3 | +from quartodoc.pandoc.components import Attr |
| 4 | +from quartodoc.pandoc.blocks import ( |
| 5 | + Block, |
| 6 | + Blocks, |
| 7 | + BulletList, |
| 8 | + CodeBlock, |
| 9 | + DefinitionList, |
| 10 | + Div, |
| 11 | + Header, |
| 12 | + OrderedList, |
| 13 | + Para, |
| 14 | + Plain, |
| 15 | +) |
| 16 | +from quartodoc.pandoc.inlines import Span, Link |
| 17 | + |
| 18 | +# NOTE: |
| 19 | +# To make it easy to cross-check what the generated html code will be, |
| 20 | +# it should be possible to copy the markdown code on the right-hand side |
| 21 | +# of the assert statements and paste it at |
| 22 | +# https://pandoc.org/try/ |
| 23 | + |
| 24 | + |
| 25 | +def test_block(): |
| 26 | + b = Block() |
| 27 | + |
| 28 | + with pytest.raises(NotImplementedError): |
| 29 | + str(b) |
| 30 | + |
| 31 | + with pytest.raises(NotImplementedError): |
| 32 | + b.html |
| 33 | + |
| 34 | + |
| 35 | +def test_blocks(): |
| 36 | + b = Blocks([ |
| 37 | + Div("a", Attr("id-1", ["c1", "c2"])), |
| 38 | + CodeBlock("b = 2"), |
| 39 | + Div("c"), |
| 40 | + Blocks([ |
| 41 | + Span("d"), |
| 42 | + Div("e") |
| 43 | + ]) |
| 44 | + ]) |
| 45 | + assert str(b) == """ |
| 46 | +::: {#id-1 .c1 .c2} |
| 47 | +a |
| 48 | +::: |
| 49 | +
|
| 50 | +``` |
| 51 | +b = 2 |
| 52 | +``` |
| 53 | +
|
| 54 | +::: {} |
| 55 | +c |
| 56 | +::: |
| 57 | +
|
| 58 | +[d]{} |
| 59 | +
|
| 60 | +::: {} |
| 61 | +e |
| 62 | +::: |
| 63 | +""".strip() |
| 64 | + |
| 65 | + |
| 66 | +def test_bulletlist(): |
| 67 | + b = BulletList(["a", "b", "c"]) |
| 68 | + assert str(b) == """ |
| 69 | +* a |
| 70 | +* b |
| 71 | +* c |
| 72 | +""".strip() |
| 73 | + |
| 74 | + b = BulletList([Para("a"), Para("b"), Para("c")]) |
| 75 | + assert str(b) == """ |
| 76 | +* a |
| 77 | +
|
| 78 | +* b |
| 79 | +
|
| 80 | +* c |
| 81 | +""".strip() |
| 82 | + |
| 83 | + b = BulletList(["a", CodeBlock("b = 2"), "c", "d"]) |
| 84 | + assert str(b) == """ |
| 85 | +* a |
| 86 | +* |
| 87 | + ``` |
| 88 | + b = 2 |
| 89 | + ``` |
| 90 | +
|
| 91 | +* c |
| 92 | +* d |
| 93 | +""".strip() |
| 94 | + |
| 95 | + |
| 96 | + b = BulletList([Para("a"), CodeBlock("b = 2"), Para("c"), Para("d")]) |
| 97 | + assert str(b) == """ |
| 98 | +* a |
| 99 | +
|
| 100 | +* |
| 101 | + ``` |
| 102 | + b = 2 |
| 103 | + ``` |
| 104 | +
|
| 105 | +* c |
| 106 | +
|
| 107 | +* d |
| 108 | +""".strip() |
| 109 | + |
| 110 | + b = BulletList([BulletList(["a", "b", "c"]), BulletList(["d", "e", "f"])]) |
| 111 | + assert str(b) == """ |
| 112 | +* * a |
| 113 | + * b |
| 114 | + * c |
| 115 | +
|
| 116 | +* * d |
| 117 | + * e |
| 118 | + * f |
| 119 | +""".strip() |
| 120 | + |
| 121 | + |
| 122 | +def test_codeblock(): |
| 123 | + c = CodeBlock("a = 1") |
| 124 | + assert str(c) == """ |
| 125 | +``` |
| 126 | +a = 1 |
| 127 | +``` |
| 128 | +""".strip() |
| 129 | + |
| 130 | + c = CodeBlock("a = 1", Attr(classes=["py"])) |
| 131 | + assert str(c) == """ |
| 132 | +```py |
| 133 | +a = 1 |
| 134 | +``` |
| 135 | +""".strip() |
| 136 | + |
| 137 | + c = CodeBlock("a = 1", Attr(classes=["py", "c1"])) |
| 138 | + assert str(c) == """ |
| 139 | +``` {.py .c1} |
| 140 | +a = 1 |
| 141 | +``` |
| 142 | +""".strip() |
| 143 | + |
| 144 | + |
| 145 | +def test_definitionlist(): |
| 146 | + d = DefinitionList([ |
| 147 | + ("Term 1", "Definition 1"), |
| 148 | + ("Term 2", "Definition 2"), |
| 149 | + ]) |
| 150 | + assert str(d) == """ |
| 151 | +Term 1 |
| 152 | +
|
| 153 | +: Definition 1 |
| 154 | +
|
| 155 | +Term 2 |
| 156 | +
|
| 157 | +: Definition 2 |
| 158 | +""".strip() |
| 159 | + |
| 160 | + d = DefinitionList([ |
| 161 | + ("Term 1", ("1st Definition of Term 1", "2nd Definition of Term 1")), |
| 162 | + ("Term 2", ("1st Definition of Term 2", "2nd Definition of Term 2")), |
| 163 | + ]) |
| 164 | + assert str(d) == """ |
| 165 | +Term 1 |
| 166 | +
|
| 167 | +: 1st Definition of Term 1 |
| 168 | +
|
| 169 | +: 2nd Definition of Term 1 |
| 170 | +
|
| 171 | +Term 2 |
| 172 | +
|
| 173 | +: 1st Definition of Term 2 |
| 174 | +
|
| 175 | +: 2nd Definition of Term 2 |
| 176 | +""".strip() |
| 177 | + |
| 178 | + d = DefinitionList([ |
| 179 | + ([Span("Term 1 - Span 1"), Span("Term 1 - Span 2")], "Definition 1"), |
| 180 | + ([Span("Term 2 - Span 1"), Span("Term 2 - Span 2")], "Definition 2"), |
| 181 | + ]) |
| 182 | + assert str(d) == """ |
| 183 | +[Term 1 - Span 1]{} [Term 1 - Span 2]{} |
| 184 | +
|
| 185 | +: Definition 1 |
| 186 | +
|
| 187 | +[Term 2 - Span 1]{} [Term 2 - Span 2]{} |
| 188 | +
|
| 189 | +: Definition 2 |
| 190 | +""".strip() |
| 191 | + |
| 192 | + d = DefinitionList([ |
| 193 | + ("Term 1", Blocks([Div("Definition of 1"), CodeBlock("var_1 = 1")])), |
| 194 | + ("Term 2", Blocks([Div("Definition of 2"), CodeBlock("var_2 = 2")])), |
| 195 | + ]) |
| 196 | + assert str(d) == """ |
| 197 | +Term 1 |
| 198 | +
|
| 199 | +: ::: {} |
| 200 | + Definition of 1 |
| 201 | + ::: |
| 202 | +
|
| 203 | + ``` |
| 204 | + var_1 = 1 |
| 205 | + ``` |
| 206 | +
|
| 207 | +Term 2 |
| 208 | +
|
| 209 | +: ::: {} |
| 210 | + Definition of 2 |
| 211 | + ::: |
| 212 | +
|
| 213 | + ``` |
| 214 | + var_2 = 2 |
| 215 | + ``` |
| 216 | +""".strip() |
| 217 | + |
| 218 | + |
| 219 | +def test_div(): |
| 220 | + d = Div("a") |
| 221 | + assert str(d) == """ |
| 222 | +::: {} |
| 223 | +a |
| 224 | +::: |
| 225 | +""".strip() |
| 226 | + |
| 227 | + d = Div("a", Attr("div-id", classes=["c1", "c2"])) |
| 228 | + assert str(d) == """ |
| 229 | +::: {#div-id .c1 .c2} |
| 230 | +a |
| 231 | +::: |
| 232 | +""".strip() |
| 233 | + |
| 234 | + |
| 235 | +def test_header(): |
| 236 | + h = Header(1, "A") |
| 237 | + assert str(h) == "# A" |
| 238 | + |
| 239 | + h = Header(2, "A", Attr("header-id", classes=["c1", "c2"])) |
| 240 | + assert str(h) == "## A {#header-id .c1 .c2}" |
| 241 | + |
| 242 | +def test_orderedlist(): |
| 243 | + o = OrderedList(["a", "b", "c"]) |
| 244 | + assert str(o) == """ |
| 245 | +1. a |
| 246 | +2. b |
| 247 | +3. c |
| 248 | +""".strip() |
| 249 | + |
| 250 | + o = OrderedList([Para("a"), Para("b"), Para("c")]) |
| 251 | + assert str(o) == """ |
| 252 | +1. a |
| 253 | +
|
| 254 | +2. b |
| 255 | +
|
| 256 | +3. c |
| 257 | +""".strip() |
| 258 | + |
| 259 | + o = OrderedList(["a", CodeBlock("b = 2"), "c", "d"]) |
| 260 | + assert str(o) == """ |
| 261 | +1. a |
| 262 | +2. |
| 263 | + ``` |
| 264 | + b = 2 |
| 265 | + ``` |
| 266 | +
|
| 267 | +3. c |
| 268 | +4. d |
| 269 | +""".strip() |
| 270 | + |
| 271 | + |
| 272 | + o = OrderedList([Para("a"), CodeBlock("b = 2"), Para("c"), Para("d")]) |
| 273 | + assert str(o) == """ |
| 274 | +1. a |
| 275 | +
|
| 276 | +2. |
| 277 | + ``` |
| 278 | + b = 2 |
| 279 | + ``` |
| 280 | +
|
| 281 | +3. c |
| 282 | +
|
| 283 | +4. d |
| 284 | +""".strip() |
| 285 | + |
| 286 | + o = OrderedList([OrderedList(["a", "b", "c"]), OrderedList(["d", "e", "f"])]) |
| 287 | + assert str(o) == """ |
| 288 | +1. 1. a |
| 289 | + 2. b |
| 290 | + 3. c |
| 291 | +
|
| 292 | +2. 1. d |
| 293 | + 2. e |
| 294 | + 3. f |
| 295 | +""".strip() |
| 296 | + |
| 297 | + |
| 298 | +def test_para(): |
| 299 | + p = Para("A") |
| 300 | + assert str(p) == """ |
| 301 | +A |
| 302 | +""" |
| 303 | + |
| 304 | + |
| 305 | +def test_plain(): |
| 306 | + p = Plain("A") |
| 307 | + assert str(p) == "A" |
0 commit comments