escape-code-parser_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package wcswidth
  3. import (
  4. "testing"
  5. )
  6. func TestEscapeCodeParsing(t *testing.T) {
  7. type test_parse_collection struct {
  8. actual, expected string
  9. }
  10. var d test_parse_collection
  11. add := func(prefix string, b []byte) error {
  12. d.actual += "\n" + prefix + ": " + string(b)
  13. return nil
  14. }
  15. var test_parser = EscapeCodeParser{
  16. HandleCSI: func(b []byte) error { return add("CSI", b) },
  17. HandleOSC: func(b []byte) error { return add("OSC", b) },
  18. HandleDCS: func(b []byte) error { return add("DCS", b) },
  19. HandleSOS: func(b []byte) error { return add("SOS", b) },
  20. HandlePM: func(b []byte) error { return add("PM", b) },
  21. HandleAPC: func(b []byte) error { return add("APC", b) },
  22. HandleRune: func(b rune) error { return add("CH", []byte(string(b))) },
  23. }
  24. reset_test_parser := func() {
  25. test_parser.Reset()
  26. d = test_parse_collection{}
  27. }
  28. check_test_result := func(raw string) {
  29. if d.actual != d.expected {
  30. t.Fatalf("parsing: %#v actual != expected: %#v != %#v", raw, string(d.actual), string(d.expected))
  31. }
  32. }
  33. test := func(raw string, expected string) {
  34. reset_test_parser()
  35. d.expected = "\n" + expected
  36. test_parser.Parse([]byte(raw))
  37. check_test_result(raw)
  38. }
  39. test("\x1b[31m\xc2\x9bm", "CSI: 31m\nCSI: m")
  40. test("\x1b[-31m\xc2\x9bm", "CSI: -31m\nCSI: m")
  41. test("ab\nc", "CH: a\nCH: b\nCH: \n\nCH: c")
  42. test("a\x1b[200m\x1b[mb\x1b[5:3;2;4~", "CH: a\nCSI: 200m\nCSI: m\nCH: b\nCSI: 5:3;2;4~")
  43. test("\x1b[200~a\x1b[201m\x1b[201~\x1b[x", "CH: a\nCH: \x1b\nCH: [\nCH: 2\nCH: 0\nCH: 1\nCH: m\nCSI: x")
  44. test("a\x1bPb\x1b\x1bc\x1b\\d", "CH: a\nDCS: b\x1bc\nCH: d")
  45. test("a\x1b_b\x1b\x1b\x1bc\x1b\\d", "CH: a\nAPC: b\x1b\x1bc\nCH: d")
  46. test("\x1b]X\x07\x1b]X\x1b\x07\x1b\\", "OSC: X\nOSC: X\x1b\x07")
  47. }