Download
40 lines 1.4 KiBGoModified 2026-10-31 raw
12345678910111213141516171819202122232425262728293031323334353637383940
package tui

import "testing"

func TestSanitize(t *testing.T) {
	cases := map[string]string{
		"plain\ntext":                              "plain\ntext",
		"\x1b[31mred\x1b[0m":                       "red",
		"\x1b]0;evil title\x07after":               "after",
		"\x1b]8;;http://x\x1b\\link\x1b]8;;\x1b\\": "link",
		"a\rb\x00c\x07d":                           "abcd",
		"tab\there":                                "tab    here",
		"\x1bPdevice\x1b\\x":                       "x",
		"bidi\u202eevil":                           "bidi\ufffdevil",
		"c1\u009bcontrol":                          "c1control",
		"unterminated \x1b[":                       "unterminated ",
	}
	for in, want := range cases {
		if got := Sanitize(in); got != want {
			t.Errorf("Sanitize(%q) = %q, want %q", in, got, want)
		}
	}
}

func TestSanitizeColors(t *testing.T) {
	cases := map[string]string{
		"\x1b[38;2;1;2;3mred\x1b[0m":    "\x1b[38;2;1;2;3mred\x1b[0m",
		"\x1b[48;2;0;40;0m\x1b[0K":      "\x1b[48;2;0;40;0m\x1b[0K",
		"\x1b[2Jclear\x1b[Hhome\x1b[2K": "clearhome",
		"\x1b]0;title\x07\x1b[1mbold":   "\x1b[1mbold",
		"\x1b]8;;\x1b[31m\x07x":         "\x1b[31mx",
		"a\rb\x00c\tz":                  "abc    z",
		"\x1b[?25lhidden cursor\x1b[31": "hidden cursor",
	}
	for in, want := range cases {
		if got := SanitizeColors(in); got != want {
			t.Errorf("SanitizeColors(%q) = %q, want %q", in, got, want)
		}
	}
}