first commit
This commit is contained in:
commit
8c4dcae475
89 changed files with 24623 additions and 0 deletions
145
tests/mdparser_test.go
Normal file
145
tests/mdparser_test.go
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
// Emphasis follows CommonMark's flanking rules, so the loose asterisks and
|
||||
// underscores of OCR - footnote marks, flattened bullets, star pages,
|
||||
// form blanks - stay text, and real emphasis nests properly.
|
||||
func TestParseInlineEmphasis(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
// Real emphasis.
|
||||
"*it* and **bold**": "<em>it</em> and <strong>bold</strong>",
|
||||
"***bold italic***": "<em><strong>bold italic</strong></em>",
|
||||
"**2** | **61**": "<strong>2</strong> | <strong>61</strong>",
|
||||
"An *n*-clique": "An <em>n</em>-clique",
|
||||
"*a **b** c*": "<em>a <strong>b</strong> c</em>",
|
||||
"**bold *nested* x**": "<strong>bold <em>nested</em> x</strong>",
|
||||
"*`code`*": "<em><code>code</code></em>",
|
||||
"_em_ and __init__.py": "<em>em</em> and <strong>init</strong>.py",
|
||||
`"On Language,"* a *New York Times*`: ""On Language,"* a <em>New York Times</em>",
|
||||
// A closer takes the nearest opener, not the first one on the line.
|
||||
"Inc.*, 321 F.3d; see also *North v. Ashcroft*, 308": "Inc.*, 321 F.3d; see also <em>North v. Ashcroft</em>, 308",
|
||||
// Not emphasis.
|
||||
"PINWALE (Sweet* and Sour* partitions)": "PINWALE (Sweet* and Sour* partitions)",
|
||||
"NRO*, CIA, SCEs* and field sites": "NRO*, CIA, SCEs* and field sites",
|
||||
"works with: * NCSC * TAO": "works with: * NCSC * TAO",
|
||||
"*** Currently Inactive ***": "*** Currently Inactive ***",
|
||||
"a Driving Force ******": "a Driving Force ******",
|
||||
"2013 WL 5741573, at *3-*9": "2013 WL 5741573, at *3-*9",
|
||||
"2 * 3 * 4": "2 * 3 * 4",
|
||||
"snake_case_name": "snake_case_name",
|
||||
"D/DIR____,IG_____,OGC___": "D/DIR____,IG_____,OGC___",
|
||||
"SO ORDERED, this _____ day of July": "SO ORDERED, this _____ day of July",
|
||||
"datastore<br>_ target user<br>_ time": "datastore<br>_ target user<br>_ time",
|
||||
`\*not em\*`: "*not em*",
|
||||
} {
|
||||
if got := parseInline(in); got != want {
|
||||
t.Errorf("parseInline(%q)\n got %q\nwant %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A list never mixes bullets and numbers: a number kept in a <ul> would
|
||||
// lose itself to a bullet, a bullet kept in an <ol> would gain a number.
|
||||
func TestRenderListKindChange(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"1. Fact:\n* South Korea\n* Japan\n\n2. Existence": "<ol>\n<li>Fact:</li>\n</ol>\n" +
|
||||
"<ul>\n<li>South Korea</li>\n<li>Japan</li>\n</ul>\n" +
|
||||
"<ol>\n<li value=\"2\">Existence</li>\n</ol>\n",
|
||||
"* Insider claims\n1. ↑ one\n2. ↑ two": "<ul>\n<li>Insider claims</li>\n</ul>\n" +
|
||||
"<ol>\n<li>↑ one</li>\n<li>↑ two</li>\n</ol>\n",
|
||||
// Both nested lists stay inside the item they hang from.
|
||||
"- a\n - b\n 1. c": "<ul>\n<li>a\n<ul>\n<li>b</li>\n</ul>\n<ol>\n<li>c</li>\n</ol>\n</li>\n</ul>\n",
|
||||
// Unindented sub-bullets: "5." picks up where "4." left off.
|
||||
"4. Demonstrate:\n* one\n* two\n5. Thank": "<ol>\n<li value=\"4\">Demonstrate:</li>\n</ol>\n" +
|
||||
"<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n" +
|
||||
"<ol>\n<li value=\"5\">Thank</li>\n</ol>\n",
|
||||
} {
|
||||
if got := Render(in); got != want {
|
||||
t.Errorf("Render(%q)\n got %q\nwant %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A number only starts a list right under a line of text when it is a 1
|
||||
// with content (CommonMark), so OCR runover - a year, a section number, a
|
||||
// page cite wrapped onto the next line - stays part of the sentence.
|
||||
func TestRenderRunoverNumbers(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"as added by the FISA Amendments Act of\n2008.": "<p>as added by the FISA Amendments Act of<br>\n2008.</p>\n",
|
||||
"extended until August\n2013. He will be joined": "<p>extended until August<br>\n2013. He will be joined</p>\n",
|
||||
"* last visited NSA/CSS in May\n2012.": "<ul>\n<li>last visited NSA/CSS in May<br>\n2012.</li>\n</ul>\n",
|
||||
"See Alexander Decl. at 39-\n42. While terrorists": "<p>See Alexander Decl. at 39-<br>\n42. While terrorists</p>\n",
|
||||
"First Events\n+\nLast Events": "<p>First Events<br>\n+<br>\nLast Events</p>\n",
|
||||
// These still start a list.
|
||||
"Steps:\n1. one\n2. two": "<p>Steps:</p>\n<ol>\n<li>one</li>\n<li>two</li>\n</ol>\n",
|
||||
"Steps:\n- one": "<p>Steps:</p>\n<ul>\n<li>one</li>\n</ul>\n",
|
||||
"\n2008. after a blank": "<ol>\n<li value=\"2008\">after a blank</li>\n</ol>\n",
|
||||
"5. five\n6.\n7. seven": "<ol>\n<li value=\"5\">five</li>\n<li></li>\n<li>seven</li>\n</ol>\n",
|
||||
"1. one\n3. gap\n10. ten": "<ol>\n<li>one</li>\n<li value=\"3\">gap</li>\n<li value=\"10\">ten</li>\n</ol>\n",
|
||||
} {
|
||||
if got := Render(in); got != want {
|
||||
t.Errorf("Render(%q)\n got %q\nwant %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// "0." is a number like any other; it used to double as "not ordered" and
|
||||
// render as whatever the running count was.
|
||||
func TestRenderZeroNumber(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"0. zero\n1. one": "<ol>\n<li value=\"0\">zero</li>\n<li>one</li>\n</ol>\n",
|
||||
"20. a\n10. b\n0.": "<ol>\n<li value=\"20\">a</li>\n<li value=\"10\">b</li>\n<li value=\"0\"></li>\n</ol>\n",
|
||||
} {
|
||||
if got := Render(in); got != want {
|
||||
t.Errorf("Render(%q)\n got %q\nwant %q", in, got, want)
|
||||
}
|
||||
}
|
||||
// Ten digits is text, not a list item.
|
||||
if got := Render("1234567890. x"); got != "<p>1234567890. x</p>\n" {
|
||||
t.Errorf("Render ten-digit number = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Thematic breaks may space out their characters, as the court filings'
|
||||
// asterisms do; such a line is a rule, never a bullet.
|
||||
func TestRenderSpacedRule(t *testing.T) {
|
||||
for _, in := range []string{"* * *", "- - - - -", "_ _ _", " * *\t* ", "***", "---"} {
|
||||
if got := Render(in); got != "<hr>\n" {
|
||||
t.Errorf("Render(%q) = %q, want a rule", in, got)
|
||||
}
|
||||
}
|
||||
if got := Render("1. a\n* * *\n2. b"); got != "<ol>\n<li>a</li>\n</ol>\n<hr>\n<ol>\n<li value=\"2\">b</li>\n</ol>\n" {
|
||||
t.Errorf("a spaced rule inside a list = %q", got)
|
||||
}
|
||||
for _, in := range []string{"* * x", "--", "- -", "*-*"} {
|
||||
if got := Render(in); got == "<hr>\n" {
|
||||
t.Errorf("Render(%q) is a rule", in)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only the one outer pipe at each end of a row is dropped, so empty edge
|
||||
// cells keep their columns, and "\|" is a pipe inside a cell.
|
||||
func TestSplitCells(t *testing.T) {
|
||||
for in, want := range map[string][]string{
|
||||
"| a | b |": {"a", "b"},
|
||||
"||||| ECIs. |": {"", "", "", "", "ECIs."},
|
||||
"| B. (U) ASSOCIATIONS|||||": {"B. (U) ASSOCIATIONS", "", "", "", ""},
|
||||
"| | |": {"", ""},
|
||||
`| Service \| Rights | x |`: {"Service | Rights", "x"},
|
||||
`| a \|`: {"a |"},
|
||||
} {
|
||||
got := splitCells(in)
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("splitCells(%q) = %q, want %q", in, got, want)
|
||||
continue
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("splitCells(%q) = %q, want %q", in, got, want)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue