package parser import "testing" func streamed(src string) string { d := New(Default) d.SetStreaming(true) d.Set(src) return d.HTML() } func TestHealing(t *testing.T) { for _, c := range []struct{ src, want string }{ {"Some **bol", "
Some bol
\n"}, {"Some **bold** and *ita", "Some bold and ita
\n"}, {"**a *b", "a b
\n"}, {"a ~~gone", "a gone
run go te
run a
hello
\n"}, {"hello *", "hello
\n"}, {"hello `", "hello
\n"}, {"2*3 is", "2*3 is
\n"}, {"snake_case_name", "snake_case_name
\n"}, {"see [the docs](https://exa", `see the docs
` + "\n"}, {"see [the docs](", `see the docs
` + "\n"}, {"math $$x^2", `math x^2
` + "\n"}, {"A paragraph\n-", "A paragraph
\n"}, {"A paragraph\n==", "A paragraph
\n"}, {"A paragraph\n---\n", "Intro
\n"}, {"Intro\n\n| a | b |\n|--", "Intro
\n"}, {"| a | b |\n|---|---|\n| 1 | 2", "| a | \nb | \n
|---|---|
| 1 | \n2 | \n
| a | \nb | \n
|---|---|
| 1 | \n2 | \n
func main() {\n\n"},
{"caf\xc3", "caf
\n"}, {"Done **bold**\n\nnext", "Done bold
\nnext
\n"}, } { if got := streamed(c.src); got != c.want { t.Errorf("%q:\ngot %q\nwant %q", c.src, got, c.want) } } } // TestHealingOnlyTail checks that blocks already finished show as // written. func TestHealingOnlyTail(t *testing.T) { got := streamed("a **b\n\nc **d") want := "a **b
\nc d
\n" if got != want { t.Errorf("got %q, want %q", got, want) } } func TestPendingLink(t *testing.T) { d := New(Default) d.SetStreaming(true) d.Set("see [docs](https://exa") in := d.Inlines(1) if len(in) < 2 || in[1].Kind != Link || !in[1].Pending { t.Fatalf("inlines %+v", in) } d.Append("mple.com) now") in = d.Inlines(1) if in[1].Kind != Link || in[1].Pending || in[1].Dest != "https://example.com" { t.Fatalf("inlines %+v", in) } } // TestHealingDollarMath heals math a single dollar opens (ExtDollarMath) // as it streams, whose _ and * are TeX rather than emphasis, while money, // escaped dollars and finished text read as before. func TestHealingDollarMath(t *testing.T) { for _, c := range []struct{ src, want string }{ {"where $x_i", `where x_i
` + "\n"}, {"where $a*b", `where a*b
` + "\n"}, {"where $x_i^2 + y_", `where x_i^2 + y_
` + "\n"}, {"where $\\frac{a}{b", `where \frac{a}{b
` + "\n"}, {"where $x \\", `where x
` + "\n"}, {"where $x $", `where x
` + "\n"}, {"*see $x_i", `see x_i
` + "\n"}, {"**see $a_b", `see a_b
` + "\n"}, {"$a$ and $b_", `a and b_
` + "\n"}, {"see [link $x", `see link x
` + "\n"}, {"math $$x^2", `math x^2
` + "\n"}, {"math $$x_i *y", `math x_i *y
` + "\n"}, // Money and escapes open no math. {"costs $5 and *more", "costs $5 and more
\n"}, {"costs $ 5 and _more", "costs $ 5 and more
\n"}, {"costs \\$x and *more", "costs $x and more
\n"}, {"costs $", "costs
\n"}, {"costs 5$", "costs 5$
\n"}, } { d := New(Default | ExtDollarMath) d.SetStreaming(true) d.Set(c.src) if got := d.HTML(); got != c.want { t.Errorf("%q:\ngot %q\nwant %q", c.src, got, c.want) } } // Without ExtDollarMath, a dollar is text. if got := streamed("where $x_i *y"); got != "where $x_i y
\n" { t.Errorf("without ExtDollarMath: %q", got) } // Finished, unclosed math is text, as before. for _, c := range []struct{ src, want string }{ {"where $x_i", "where $x_i
\n"}, {"where $a *b*", "where $a b
\n"}, {"costs $5 and $x$", `costs $5 and x
` + "\n"}, } { d := New(Default | ExtDollarMath) d.Set(c.src) if got := d.HTML(); got != c.want { t.Errorf("finished %q:\ngot %q\nwant %q", c.src, got, c.want) } } }