// SPDX-License-Identifier: GPL-3.0-or-later package gemtext import ( "errors" "strings" "testing" ) // identity leaves every address as it is. func identity(href string) (string, error) { return href, nil } // convert converts html with addresses left as they are and no blocks. func convert(t *testing.T, html string) string { t.Helper() got, err := Convert(html, Options{Link: identity}) if err != nil { t.Fatalf("Convert(%q) error = %v", html, err) } return got } // TestConvert checks the gemtext of every kind of element the site uses. func TestConvert(t *testing.T) { tests := []struct { name, html, want string }{ {"headings", `
Read the first and the second page.
`, "Read the first and the second page.\n=> /a.html first\n=> https://x.org/b second\n"}, {"the same link once", ``, "A and A again.\n=> /a A\n"}, {"a paragraph that is only a link", ``, "=> /x.html All news\n"}, {"line breaks", `One
Two
Three
a\n b\t c
", "a b c\n"}, {"entities", `Tom & Jerry © <ok>
`, "Tom & Jerry ©`, "> One\n>\n> Two x\n=> /x x\n"}, {"note is a quote", `One
Two x
Careful
`, "> Careful\n"}, {"preformatted", "line1\n line2\n```\n", "```\nline1\n line2\n ```\n```\n"},
{"table with a header",
`| Service | Kept |
|---|---|
| 30 days | |
| Chat | 1 year |
| a | bb |
| ccc | d |
| x |
# no
=> no
* no
> no
`, " # no\n\n => no\n\n * no\n\n > no\n"}, {"hidden and scripted things are skipped", `z
kept
`, "kept\n"}, {"containers are transparent", `A
by squid
`, "14 November 2026 by squid\n"}, {"empty input", ``, ""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := convert(t, tt.html); got != tt.want { t.Errorf("Convert() =\n%q\nwant\n%q", got, tt.want) } }) } } // TestConvertLinkMapping checks that Link rewrites and drops addresses, and that its error stops the conversion. func TestConvertLinkMapping(t *testing.T) { link := func(href string) (string, error) { switch { case strings.HasPrefix(href, "#"): return "", nil case href == "/broken.html": return "", errors.New("no such page") } target, _, _ := strings.Cut(href, "#") return strings.Replace(target, ".html", ".gmi", 1), nil } got, err := Convert(``, Options{Link: link}) if err != nil || got != "See a and top.\n=> /a.gmi a\n" { t.Errorf("Convert() = %q, %v", got, err) } _, err = Convert(``, Options{Link: link}) if err == nil || !strings.Contains(err.Error(), "/broken.html") || !strings.Contains(err.Error(), "no such page") { t.Errorf("error = %v, want one that names the address and the cause", err) } _, err = Convert(`intro
end
`, Options{Link: identity, Block: block}) if err != nil || got != "intro\n\n=> /a 2026-01-01 A\n=> /b 2025-12-31 B\n\nend\n" { t.Errorf("Convert() = %q, %v", got, err) } if _, err := Convert(`unclosed`, Options{Link: identity}); err == nil || !strings.Contains(err.Error(), "HTML") { t.Errorf("error = %v, want one about the HTML", err) } } // TestTextAndLink checks the helpers that write single lines. func TestTextAndLink(t *testing.T) { if got := Text(" two \n words "); got != "two words" { t.Errorf("Text() = %q", got) } if got := Text("# not a heading"); got != " # not a heading" { t.Errorf("Text() = %q, want a leading space", got) } if got := Link("/a.gmi", " A page "); got != "=> /a.gmi A page" { t.Errorf("Link() = %q", got) } if got := Link("/a.gmi", ""); got != "=> /a.gmi" { t.Errorf("Link() without a label = %q", got) } } // TestConvertWideTable checks that a table that does not fit in 80 columns becomes a list of records, a // heading for the first cell of each row and a bullet for each other cell, named by its column. func TestConvertWideTable(t *testing.T) { long := strings.Repeat("word ", 20) + "end" html := `
| Service | Kept | Seen |
|---|---|---|
| ` + long + ` | all | |
| Chat | some |
| a | ` + long + ` |
| b | c |