Merge: Misc AST
[nit.git] / lib / markdown / test_markdown.nit
index 6dba442..e552883 100644 (file)
@@ -93,7 +93,10 @@ This is a H1
 This is a H2
 -------------
 """
-               var exp = "<h1>This is a H1</h1>\n<h2>This is a H2</h2>\n"
+               var exp = """
+<h1 id="This_is_a_H1">This is a H1</h1>
+<h2 id="This_is_a_H2">This is a H2</h2>
+"""
                var res = test.md_to_html.write_to_string
                assert res == exp
        end
@@ -105,7 +108,11 @@ This is a H2
 ## This is a H2
 ###### This is a H6
 """
-               var exp = "<h1>This is a H1</h1>\n<h2>This is a H2</h2>\n<h6>This is a H6</h6>\n"
+               var exp = """
+<h1 id="This_is_a_H1">This is a H1</h1>
+<h2 id="This_is_a_H2">This is a H2</h2>
+<h6 id="This_is_a_H6">This is a H6</h6>
+"""
                var res = test.md_to_html.write_to_string
                assert res == exp
        end
@@ -118,7 +125,11 @@ This is a H2
 
 ### This is a H3 ######
 """
-               var exp = "<h1>This is a H1</h1>\n<h2>This is a H2</h2>\n<h3>This is a H3</h3>\n"
+               var exp = """
+<h1 id="This_is_a_H1">This is a H1</h1>
+<h2 id="This_is_a_H2">This is a H2</h2>
+<h3 id="This_is_a_H3">This is a H3</h3>
+"""
                var res = test.md_to_html.write_to_string
                assert res == exp
        end
@@ -396,6 +407,36 @@ sit amet, consectetuer adipiscing elit.</p>
                assert res == exp
        end
 
+       fun test_process_list11 do
+               var test = """
+This is a paragraph
+* and this is not a list
+"""
+               var exp = """
+<p>This is a paragraph
+* and this is not a list</p>
+"""
+               var proc = new MarkdownProcessor
+               proc.ext_mode = false
+               var res = proc.process(test).write_to_string
+               assert res == exp
+       end
+
+       fun test_process_list_ext do
+               var test = """
+This is a paragraph
+* and this is not a list
+"""
+               var exp = """
+<p>This is a paragraph</p>
+<ul>
+<li>and this is not a list</li>
+</ul>
+"""
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
        fun test_process_code1 do
                var test = """
 This is a normal paragraph:
@@ -437,6 +478,106 @@ end tell
                assert res == exp
        end
 
+       fun test_process_code_ext1 do
+               var test = """
+Here is an example of AppleScript:
+~~~
+tell application "Foo"
+    beep
+end tell
+
+<div class="footer">
+    &copy; 2004 Foo Corporation
+</div>
+~~~
+"""
+               var exp = """
+<p>Here is an example of AppleScript:</p>
+<pre><code>tell application "Foo"
+    beep
+end tell
+
+&lt;div class="footer"&gt;
+    &amp;copy; 2004 Foo Corporation
+&lt;/div&gt;
+</code></pre>
+"""
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
+       fun test_process_code_ext2 do
+               var test = """
+Here is an example of AppleScript:
+```
+tell application "Foo"
+    beep
+end tell
+
+<div class="footer">
+    &copy; 2004 Foo Corporation
+</div>
+```
+"""
+               var exp = """
+<p>Here is an example of AppleScript:</p>
+<pre><code>tell application "Foo"
+    beep
+end tell
+
+&lt;div class="footer"&gt;
+    &amp;copy; 2004 Foo Corporation
+&lt;/div&gt;
+</code></pre>
+"""
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
+       fun test_process_code_ext3 do
+               var proc = new MarkdownProcessor
+               proc.ext_mode = false
+
+               var test = """
+Here is an example of AppleScript:
+    beep
+"""
+               var exp = """
+<p>Here is an example of AppleScript:
+beep</p>
+"""
+               var res = proc.process(test).write_to_string
+               assert res == exp
+       end
+
+       fun test_process_code_ext4 do
+               var test = """
+Here is an example of AppleScript:
+    beep
+"""
+               var exp = """
+<p>Here is an example of AppleScript:</p>
+<pre><code>beep
+</code></pre>
+"""
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
+       fun test_process_code_ext5 do
+               var test = """
+```nit
+print "Hello World!"
+```
+"""
+               var exp = """
+<pre class="nit"><code>print "Hello World!"
+</code></pre>
+"""
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
        fun test_process_nesting1 do
                var test = """
 > ## This is a header.
@@ -450,7 +591,7 @@ end tell
 """
                var exp = """
 <blockquote>
-<h2>This is a header.</h2>
+<h2 id="This_is_a_header.">This is a header.</h2>
 <ol>
 <li>This is the first list item.</li>
 <li>This is the second list item.</li>
@@ -573,6 +714,22 @@ __double underscores__
                assert res == exp
        end
 
+       fun test_process_emph3 do
+               var proc = new MarkdownProcessor
+               proc.ext_mode = false
+               var test = "Con_cat_this"
+               var exp = "<p>Con<em>cat</em>this</p>\n"
+               var res = proc.process(test).write_to_string
+               assert res == exp
+       end
+
+       fun test_process_emph_ext do
+               var test = "Con_cat_this"
+               var exp = "<p>Con_cat_this</p>\n"
+               var res = test.md_to_html.write_to_string
+               assert res == exp
+       end
+
        fun test_process_xml1 do
                var test = """
 This is a regular paragraph.
@@ -840,6 +997,29 @@ break</a> with a line-ending space.</p>
                assert res == exp
        end
 
+       fun test_process_strike do
+               var proc = new MarkdownProcessor
+               proc.ext_mode = false
+               var test = "This is how you ~~strike text~~"
+               var exp = "<p>This is how you ~~strike text~~</p>\n"
+               var res = proc.process(test).write_to_string
+               assert exp == res
+       end
+
+       fun test_process_strike_ext do
+               var test = "This is how you ~~strike text~~"
+               var exp = "<p>This is how you <del>strike text</del></p>\n"
+               var res = test.md_to_html.write_to_string
+               assert exp == res
+       end
+
+       fun test_escape_bad_html do
+                       var test = "-1 if < , +1 if > and 0 otherwise"
+                       var exp = "<p>-1 if &lt; , +1 if > and 0 otherwise</p>\n"
+               var res = test.md_to_html.write_to_string
+               assert exp == res
+       end
+
        fun test_daring_encoding do
                var test = """
 AT&T has an ampersand in their name.
@@ -1207,6 +1387,9 @@ Here's how you put `` `backticks` `` in a code span.
        end
 
        fun test_daring_pars do
+               var proc = new MarkdownProcessor
+               proc.ext_mode = false
+
                var test = """
 In Markdown 1.0.0 and earlier. Version
 8. This line turns into a list item.
@@ -1227,7 +1410,7 @@ list item.</p>
 <p>Here's one with a bullet.
 * criminey.</p>
 """
-               var res = test.md_to_html.write_to_string
+               var res = proc.process(test).write_to_string
                assert res == exp
        end
 
@@ -1951,7 +2134,7 @@ Same thing but with paragraphs:
 """
 
                var exp = """
-<h2>Unordered</h2>
+<h2 id="Unordered">Unordered</h2>
 <p>Asterisks tight:</p>
 <ul>
 <li>asterisk 1</li>
@@ -1999,7 +2182,7 @@ Same thing but with paragraphs:
 <li><p>Minus 3</p>
 </li>
 </ul>
-<h2>Ordered</h2>
+<h2 id="Ordered">Ordered</h2>
 <p>Tight:</p>
 <ol>
 <li>First</li>
@@ -2041,7 +2224,7 @@ back.</p>
 <li><p>Item 3.</p>
 </li>
 </ol>
-<h2>Nested</h2>
+<h2 id="Nested">Nested</h2>
 <ul>
 <li>Tab<ul>
 <li>Tab<ul>
@@ -2345,8 +2528,6 @@ class TestLine
 
        var subject: MDLine
 
-       init do end
-
        fun test_is_empty do
                subject = new MDLine("")
                assert subject.is_empty
@@ -2383,51 +2564,55 @@ class TestLine
        fun test_line_type do
                var v = new MarkdownProcessor
                subject = new MDLine("")
-               assert subject.kind(v) isa LineEmpty
+               assert v.line_kind(subject) isa LineEmpty
                subject = new MDLine("    ")
-               assert subject.kind(v) isa LineEmpty
+               assert v.line_kind(subject) isa LineEmpty
                subject = new MDLine("text   ")
-               assert subject.kind(v) isa LineOther
+               assert v.line_kind(subject) isa LineOther
                subject = new MDLine("  # Title")
-               assert subject.kind(v) isa LineHeadline
+               assert v.line_kind(subject) isa LineHeadline
                subject = new MDLine("  ### Title")
-               assert subject.kind(v) isa LineHeadline
+               assert v.line_kind(subject) isa LineHeadline
                subject = new MDLine("    code")
-               assert subject.kind(v) isa LineCode
-               subject = new MDLine("  ~~~")
-               assert subject.kind(v) isa LineFence
-               subject = new MDLine("  ```")
-               assert subject.kind(v) isa LineFence
+               assert v.line_kind(subject) isa LineCode
                subject = new MDLine("   Title  ")
                subject.next = new MDLine("== ")
-               assert subject.kind(v) isa LineHeadline1
+               assert v.line_kind(subject) isa LineHeadline1
                subject = new MDLine("   Title  ")
                subject.next = new MDLine("-- ")
-               assert subject.kind(v) isa LineHeadline2
+               assert v.line_kind(subject) isa LineHeadline2
                subject = new MDLine("  *    *   * ")
-               assert subject.kind(v) isa LineHR
+               assert v.line_kind(subject) isa LineHR
                subject = new MDLine("  *** ")
-               assert subject.kind(v) isa LineHR
+               assert v.line_kind(subject) isa LineHR
                subject = new MDLine("- -- ")
-               assert subject.kind(v) isa LineHR
+               assert v.line_kind(subject) isa LineHR
                subject = new MDLine("--------- ")
-               assert subject.kind(v) isa LineHR
+               assert v.line_kind(subject) isa LineHR
                subject = new MDLine(" >")
-               assert subject.kind(v) isa LineBlockquote
+               assert v.line_kind(subject) isa LineBlockquote
                subject = new MDLine("<p></p>")
-               assert subject.kind(v) isa LineXML
+               assert v.line_kind(subject) isa LineXML
                subject = new MDLine("<p>")
-               assert subject.kind(v) isa LineOther
+               assert v.line_kind(subject) isa LineOther
                subject = new MDLine("  * foo")
-               assert subject.kind(v) isa LineUList
+               assert v.line_kind(subject) isa LineUList
                subject = new MDLine("- foo")
-               assert subject.kind(v) isa LineUList
+               assert v.line_kind(subject) isa LineUList
                subject = new MDLine("+ foo")
-               assert subject.kind(v) isa LineUList
+               assert v.line_kind(subject) isa LineUList
                subject = new MDLine("1. foo")
-               assert subject.kind(v) isa LineOList
+               assert v.line_kind(subject) isa LineOList
                subject = new MDLine("   11111. foo")
-               assert subject.kind(v) isa LineOList
+               assert v.line_kind(subject) isa LineOList
+       end
+
+       fun test_line_type_ext do
+               var v = new MarkdownProcessor
+               subject = new MDLine("  ~~~")
+               assert v.line_kind(subject) isa LineFence
+               subject = new MDLine("  ```")
+               assert v.line_kind(subject) isa LineFence
        end
 
        fun test_count_chars do
@@ -2456,3 +2641,44 @@ class TestLine
                assert subject.count_chars_start('*') == 0
        end
 end
+
+class TestHTMLDecorator
+       super TestSuite
+
+       fun test_headlines do
+               var test = """
+# **a**
+## a.a
+### a.a.b
+### a.a.b
+## a.b
+# [b](test)
+## b.a
+### b.a.c
+## b.b
+## b.c
+# c
+"""
+               var proc = new MarkdownProcessor
+               var decorator = proc.emitter.decorator.as(HTMLDecorator)
+               proc.process(test)
+               var res = ""
+               for id, headline in decorator.headlines do
+                       res += "{headline.title}:{id}\n"
+               end
+               var exp = """
+**a**:a
+a.a:a.a
+a.a.b:a.a.b
+a.a.b:a.a.b_1
+a.b:a.b
+[b](test):btest
+b.a:b.a
+b.a.c:b.a.c
+b.b:b.b
+b.c:b.c
+c:c
+"""
+               assert res == exp
+       end
+end