line_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package door
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestLine(t *testing.T) {
  7. var line Line = Line{Text: "Test Me"}
  8. var output string = line.Output()
  9. var expect string = "Test Me"
  10. if output != expect {
  11. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  12. }
  13. if line.Update() {
  14. t.Error("Line: No updater, should return false")
  15. }
  16. line.DefaultColor = Color(0)
  17. line.Width = 8
  18. output = line.Output()
  19. expect = "\x1b[0mTest Me "
  20. if output != expect {
  21. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  22. }
  23. // leave the default color, it is ignored when there's a render function
  24. // line.DefaultColor = ""
  25. line.RenderF = RenderBlueYellow
  26. output = line.Output()
  27. var blue string = ColorText("BOLD BLUE")
  28. var yellow string = ColorText("BOLD YELLOW")
  29. expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e "
  30. if output != expect {
  31. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  32. }
  33. }
  34. func TestLineUpdate(t *testing.T) {
  35. var counter int = 0
  36. uf := func() string {
  37. return fmt.Sprintf("Count: %d", counter)
  38. }
  39. var line Line = Line{Text: "", UpdateF: uf}
  40. line.Update()
  41. var output string = line.Output()
  42. var expect string = "Count: 0"
  43. if output != expect {
  44. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  45. }
  46. if line.Update() {
  47. t.Error("Unexpected Update: should have returned false. (no change)")
  48. }
  49. counter++
  50. if !line.Update() {
  51. t.Error("Missing Update: value was changed, Text should have changed")
  52. }
  53. output = line.Output()
  54. expect = "Count: 1"
  55. if output != expect {
  56. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  57. }
  58. }
  59. func TestLineUnicode(t *testing.T) {
  60. Unicode = true
  61. // code point > FFFF, use \U00000000 (not \u).
  62. var line Line = Line{Text: "Howdy \U0001f920"}
  63. var output string = line.Output()
  64. var expect string = "Howdy 🤠"
  65. if output != expect {
  66. t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
  67. }
  68. if StringLen(expect) != 8 {
  69. t.Errorf("LineUnicode Strlen: Expected 8, got %d", StringLen(expect))
  70. }
  71. // 🤠 = 2 chars.
  72. line.Width = 9
  73. output = line.Output()
  74. expect = "Howdy 🤠 "
  75. if output != expect {
  76. t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
  77. }
  78. }
  79. func TestLineCP437(t *testing.T) {
  80. Unicode = false
  81. var tests []string = []string{"\xdb TEXT \xdb",
  82. "\xf1 F1", "\xf2 F2", "\xf3 F3"}
  83. for _, test := range tests {
  84. var line Line = Line{Text: test}
  85. var output string = line.Output()
  86. var expect string = test
  87. if output != expect {
  88. t.Errorf("LineCP437: Expected %#v, got %#v", expect, output)
  89. }
  90. }
  91. }