~xigoi/xidoc

ea3eb3fd5b3e8c6d336d5877d4066d172e4c9cf0 — Adam Blažek 2 months ago cc1d22e
More tests
3 files changed, 75 insertions(+), 4 deletions(-)

M docs/commands.html
M docs/commands.xd
M tests/test_documents.nim
M docs/commands.html => docs/commands.html +2 -1
@@ 160,7 160,8 @@ font-family:var(--sans-serif);}code[class*=language-],pre[class*=language-]{font
<table><tr><th>Example</th><th>Output</th></tr>
<tr><td><pre class="language-xidoc"><code class="language-xidoc">TOP 5 LIST OF SMALLEST POSITIVE INTEGERS: <span class="token punctuation">[</span><span class="token tag">ordered-list</span> 1<span class="token punctuation">;</span> 2<span class="token punctuation">;</span> 3<span class="token punctuation">;</span> 4<span class="token punctuation">;</span> 5<span class="token punctuation">]</span></code></pre></td><td><div>TOP 5 LIST OF SMALLEST POSITIVE INTEGERS: <ol><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ol></div></td></tr></table></section>
<section id="description-list" class="command"><h5 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">description-list</span> *(key description)<span class="token punctuation">]</span></code></h5>Creates a description list associating <code>key</code>s with <code>description</code>s.
<dl><dt>Dog</dt><dd>Cute and loyal</dd><dt>Cat</dt><dd>Cute and entitled</dd></dl></section></section>
<table><tr><th>Example</th><th>Output</th></tr>
<tr><td><pre class="language-xidoc"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">description-list</span> Dog<span class="token punctuation">;</span> Cute and loyal<span class="token punctuation">;</span> Cat<span class="token punctuation">;</span> Cute and entitled<span class="token punctuation">]</span></code></pre></td><td><div><dl><dt>Dog</dt><dd>Cute and loyal</dd><dt>Cat</dt><dd>Cute and entitled</dd></dl></div></td></tr></table></section></section>
<section id="p" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">p</span> text<span class="token punctuation">]</span></code></h4>Creates a paragraph with the given <code>text</code>.</section>
<section id="section" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">section</span> ?title<span class="token punctuation">;</span> text<span class="token punctuation">]</span></code></h4>Creates a section with the given <code>title</code> and <code>text</code> (or without a title if not given). If it's inside another section, it will be a subsection. If it's inside a subsection, it will be a subsubsection. In HTML, this nesting can continue further.
<table><tr><th>Example</th><th>Output</th></tr>

M docs/commands.xd => docs/commands.xd +1 -1
@@ 230,7 230,7 @@ const factorial = (n) => {
      ]
      [cmd description-list; *(key description);
        Creates a description list associating [param key]s with [param description]s.
        [description-list Dog; Cute and loyal; Cat; Cute and entitled]
        [ex [description-list Dog; Cute and loyal; Cat; Cute and entitled]]
      ]
    ]
    [cmd p; text;

M tests/test_documents.nim => tests/test_documents.nim +72 -2
@@ 2,7 2,7 @@ import std/strutils
import std/unittest
import xidoc

proc shouldRenderAs(input: string, output: string) =
template shouldRenderAs(input: string, output: string) =
  check input.renderXidoc(snippet = true) == output

suite "plain text":


@@ 94,7 94,7 @@ suite "\"Inline formatting\" commands":
  test "[unit]":
    "The radius of the Earth is [unit 6378; km].".shouldRenderAs("The radius of the Earth is 6378 km.")

suite "\"Inline formatting\" commands":
suite "\"Block formatting\" commands":

  test "[block-quote]":
    "[p The first rule in the Zen of Nim is:] [block-quote Copying bad design is not good design.]"


@@ 139,6 139,10 @@ suite "\"Inline formatting\" commands":
    "TOP 5 LIST OF SMALLEST POSITIVE INTEGERS: [ordered-list 1; 2; 3; 4; 5]"
    .shouldRenderAs("TOP 5 LIST OF SMALLEST POSITIVE INTEGERS: <ol><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ol>")

  test "[description-list]":
    "[description-list Dog; Cute and loyal; Cat; Cute and entitled]"
    .shouldRenderAs("<dl><dt>Dog</dt><dd>Cute and loyal</dd><dt>Cat</dt><dd>Cute and entitled</dd></dl>")

  test "[p]":
    "[p PARAGRAPH]".shouldRenderAs("<p>PARAGRAPH</p>")



@@ 158,3 162,69 @@ suite "\"Inline formatting\" commands":
  test "[title]":
    "[title TITLE]".shouldRenderAs("<h1>TITLE</h1>")
    check renderXidoc("[title TITLE]").contains("<title>TITLE</title>")

suite "\"Unicode characters\" commands":

  test "[\"]":
    "[\" Hello!]".shouldRenderAs("“Hello!”")
    "[lang czech; [\" Ahoj!]]".shouldRenderAs("„Ahoj!“")

  test "[--]":
    "[--]".shouldRenderAs("–")

  test "[---]":
    "[---]".shouldRenderAs("—")

  test "[...]":
    "[...]".shouldRenderAs("…")

suite "\"Logos\" commands":

  test "[LaTeX]":
    "[LaTeX]".shouldRenderAs("<span class=\"xd-latex\">L<sup>a</sup>T<sub>e</sub>X</span>")

  test "[xidoc]":
    "[xidoc]".shouldRenderAs("<span class=\"xd-logo\">ξ</span>")

suite "Custom commands":

  test "no parameters":
    "[def foo; bar][foo]".shouldRenderAs("bar")

  test "parameters":
    "[def greet; name; Hello, [arg name]!][greet reader]".shouldRenderAs("Hello, reader!")

suite "\"Target detection\" commands":

  test "[if-html]":
    "[if-html You can see this only if you're in HTML]"
    .shouldRenderAs("You can see this only if you're in HTML")

  test "[if-latex]":
    "[if-latex You can see this only if you're in LaTeX]".shouldRenderAs("")

  test "[if-gemtext]":
    "[if-gemtext You can see this only if you're in Gemtext]".shouldRenderAs("")

suite "\"List manipulation\" commands":

  test "[for-each], [join]":
    "[join [space]; [for-each lang; HTML LaTeX Gemtext; xidoc compiles to [lang].]]"
    .shouldRenderAs("xidoc compiles to HTML. xidoc compiles to LaTeX. xidoc compiles to Gemtext.")

suite "\"List manipulation\" commands":

  test "[janet-call]":
    "[janet-call [raw (fn [radius] (describe (* 2 math/pi (scan-number radius))))]; 6]"
    .shouldRenderAs("37.6991")

  test "[janet-eval]":
    "[janet-eval [raw (do (defn gcd [a b] (if (= b 0) a (gcd b (% a b)))) (describe (gcd (scan-number a) (scan-number b)))) ]; a;128 ; b;168]"
    .shouldRenderAs("8")

  test "[js-call]":
    "[js-call [raw (radius) => Math.PI * radius * radius]; 6]".shouldRenderAs("113.09733552923255")

  test "[js-eval]":
    "[js-eval [raw { let discriminant = b*b - 4*a*c; let root1 = (-b + Math.sqrt(discriminant)) / (2 * a); let root2 = (-b - Math.sqrt(discriminant)) / (2 * a); root1 + \", \" + root2 }]; a;3 ; b;-18 ; c;24]"
    .shouldRenderAs("4, 2")