package codeview import ( "html" "regexp" "strings" "testing" ) var highlightTag = regexp.MustCompile(``) // plain is what a reader sees and copies: the highlighted markup with // its five elements removed and its entities decoded. func plain(h string) string { return html.UnescapeString(highlightTag.ReplaceAllString(h, "")) } func TestHighlightMarksTheFiveKinds(t *testing.T) { got := string(Highlight(`{{template "meter" dict "Percent" 140 "Items" .Locales}}`)) want := `{{template "meter" dict "Percent" 140 "Items" .Locales}}` if got != want { t.Errorf("call:\n got %s\nwant %s", got, want) } got = string(Highlight(`Go`)) want = `<a rst-btn href="/x">Go</a><input disabled>` if got != want { t.Errorf("markup:\n got %s\nwant %s", got, want) } } // The text on screen is the source, whatever is in it: entities, a // '>' inside a quoted value, {{ inside an attribute, upper case. func TestHighlightRoundTripsAwkwardMarkup(t *testing.T) { for _, src := range []string{ `Tom & Jerry € < 3`, `
{{x}}
`, `
a {{ "}}" }} b
`, "{{template \"x\" dict\n \"A\" \"a\\\"b\"\n \"B\" `raw`}}", `a < b > c & d`, `−3%`, } { h := string(Highlight(src)) if got := plain(h); got != src { t.Errorf("round trip lost bytes:\n src: %s\nplain: %s\n html: %s", src, got, h) } if strings.Contains(highlightTag.ReplaceAllString(h, ""), "<") { t.Errorf("an unescaped '<' reached the page: %s", h) } } }