indent-and-wrap_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package style
  3. import (
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. )
  8. func TestFormatWithIndent(t *testing.T) {
  9. screen_width := 0
  10. opts := WrapOptions{}
  11. tx := func(text string, expected ...string) {
  12. q := opts.Indent + strings.Join(expected, "")
  13. actual := WrapText(text, screen_width, opts)
  14. if actual != q {
  15. os, _ := json.Marshal(opts)
  16. t.Fatalf("\nFailed for: %#v\nOptions: %s\nexpected: %#v\nactual: %#v", text, os, q, actual)
  17. }
  18. }
  19. opts.Indent = ""
  20. screen_width = 4
  21. tx("one two", "one \ntwo")
  22. tx("a b", "a b")
  23. screen_width = 3
  24. tx("one tw", "one\n tw")
  25. screen_width = 4
  26. opts.Trim_whitespace = true
  27. opts.Indent = "X"
  28. tx("one two", "one\nXtwo")
  29. tx("\x1b[2mone \x1b[mtwo", "\x1b[2mone\n\x1b[222mX\x1b[2m\x1b[mtwo")
  30. screen_width = 3
  31. tx("on tw", "on\nXtw")
  32. opts.Indent = ""
  33. opts.Trim_whitespace = false
  34. opts.Indent = "__"
  35. screen_width = 11
  36. tx("testing\n\ntwo", "testing\n\n__two")
  37. tx("testing\n \ntwo", "testing\n__ \n__two")
  38. a := strings.Repeat("a", screen_width-len(opts.Indent)-1)
  39. tx(a+" b", a+" \n__b")
  40. tx("123456 \x1b[31m789a", "123456 \n__\x1b[31m789a")
  41. tx("12 \x1b[31m789 abcd", "12 \x1b[31m789 \n\x1b[39m__\x1b[31mabcd")
  42. tx("bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\ two", "bb \x1b]8;;http://xyz.com\x1b\\text\x1b]8;;\x1b\\ \n__two")
  43. tx("\x1b[31maaaaaa \x1b[39mbbbbbb", "\x1b[31maaaaaa \n\x1b[39m__\x1b[31m\x1b[39mbbbbbb")
  44. tx(
  45. "\x1b[31;4:3m\x1b]8;;XXX\x1b\\combined using\x1b]8;;\x1b\\ operators",
  46. "\x1b[31;4:3m\x1b]8;;XXX\x1b\\combined \n\x1b[4:0;39m\x1b]8;;\x1b\\__\x1b[4:3;31m\x1b]8;;XXX\x1b\\using\x1b]8;;\x1b\\ \n\x1b[4:0;39m__\x1b[4:3;31moperators")
  47. opts.Indent = ""
  48. screen_width = 3
  49. tx("one", "one")
  50. tx("four", "fou\nr")
  51. tx("nl\n\n", "nl\n\n")
  52. tx("four\n\n", "fou\nr\n\n")
  53. screen_width = 8
  54. tx(
  55. "\x1b[1mbold\x1b[221m no more bold",
  56. "\x1b[1mbold\x1b[221m no \nmore \nbold",
  57. )
  58. }