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**": "it and bold",
"***bold italic***": "bold italic",
"**2** | **61**": "2 | 61",
"An *n*-clique": "An n-clique",
"*a **b** c*": "a b c",
"**bold *nested* x**": "bold nested x",
"*`code`*": "code",
"_em_ and __init__.py": "em and init.py",
`"On Language,"* a *New York Times*`: ""On Language,"* a New York Times",
// 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 North v. Ashcroft, 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
_ target user
_ time": "datastore
_ target user
_ 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
would
// lose itself to a bullet, a bullet kept in an 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": "\n- Fact:
\n
\n" +
"\n" +
"\n- Existence
\n
\n",
"* Insider claims\n1. ↑ one\n2. ↑ two": "\n" +
"\n- ↑ one
\n- ↑ two
\n
\n",
// Both nested lists stay inside the item they hang from.
"- a\n - b\n 1. c": "\n",
// Unindented sub-bullets: "5." picks up where "4." left off.
"4. Demonstrate:\n* one\n* two\n5. Thank": "\n- Demonstrate:
\n
\n" +
"\n" +
"\n- Thank
\n
\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.": "as added by the FISA Amendments Act of
\n2008.
\n",
"extended until August\n2013. He will be joined": "extended until August
\n2013. He will be joined
\n",
"* last visited NSA/CSS in May\n2012.": "\n- last visited NSA/CSS in May
\n2012. \n
\n",
"See Alexander Decl. at 39-\n42. While terrorists": "See Alexander Decl. at 39-
\n42. While terrorists
\n",
"First Events\n+\nLast Events": "First Events
\n+
\nLast Events
\n",
// These still start a list.
"Steps:\n1. one\n2. two": "Steps:
\n\n- one
\n- two
\n
\n",
"Steps:\n- one": "Steps:
\n\n",
"\n2008. after a blank": "\n- after a blank
\n
\n",
"5. five\n6.\n7. seven": "\n- five
\n\n- seven
\n
\n",
"1. one\n3. gap\n10. ten": "\n- one
\n- gap
\n- ten
\n
\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": "\n- zero
\n- one
\n
\n",
"20. a\n10. b\n0.": "\n- a
\n- b
\n\n
\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 != "1234567890. x
\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 != "
\n" {
t.Errorf("Render(%q) = %q, want a rule", in, got)
}
}
if got := Render("1. a\n* * *\n2. b"); got != "\n- a
\n
\n
\n\n- b
\n
\n" {
t.Errorf("a spaced rule inside a list = %q", got)
}
for _, in := range []string{"* * x", "--", "- -", "*-*"} {
if got := Render(in); got == "
\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
}
}
}
}