wrapper_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package style
  3. import (
  4. "fmt"
  5. "testing"
  6. )
  7. var _ = fmt.Print
  8. func TestANSIStyleContext(t *testing.T) {
  9. var ctx = Context{AllowEscapeCodes: false}
  10. sprint := ctx.SprintFunc("bold")
  11. if sprint("test") != "test" {
  12. t.Fatal("AllowEscapeCodes=false not respected")
  13. }
  14. ctx.AllowEscapeCodes = true
  15. if sprint("test") == "test" {
  16. t.Fatal("AllowEscapeCodes=true not respected")
  17. }
  18. }
  19. func TestANSIStyleSprint(t *testing.T) {
  20. var ctx = Context{AllowEscapeCodes: true}
  21. test := func(spec string, prefix string, suffix string) {
  22. actual := ctx.SprintFunc(spec)(" ")
  23. expected := prefix + " " + suffix
  24. if actual != expected {
  25. t.Fatalf("Formatting with spec: %s failed expected != actual: %#v != %#v", spec, expected, actual)
  26. }
  27. }
  28. test("", "", "")
  29. test("bold", "\x1b[1m", "\x1b[221m")
  30. test("bold fg=red u=curly", "\x1b[1;4:3;31m", "\x1b[221;4:0;39m")
  31. test("fg=123", "\x1b[38:5:123m", "\x1b[39m")
  32. test("fg=15", "\x1b[97m", "\x1b[39m")
  33. test("bg=15", "\x1b[107m", "\x1b[49m")
  34. test("fg=#123", "\x1b[38:2:17:34:51m", "\x1b[39m")
  35. test("fg=rgb:1/2/3", "\x1b[38:2:1:2:3m", "\x1b[39m")
  36. test("bg=123", "\x1b[48:5:123m", "\x1b[49m")
  37. test("uc=123", "\x1b[58:5:123m", "\x1b[59m")
  38. test("uc=1", "\x1b[58:5:1m", "\x1b[59m")
  39. actual := ctx.UrlFunc("u=curly uc=cyan")("http://moo.com", "___")
  40. expected := "\x1b[4:3;58:5:6m\x1b]8;;http://moo.com\x1b\\___\x1b]8;;\x1b\\\x1b[4:0;59m"
  41. if actual != expected {
  42. t.Fatalf("Formatting URL failed expected != actual: %#v != %#v", expected, actual)
  43. }
  44. }