line_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package door
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "unicode"
  7. )
  8. func TestLine(t *testing.T) {
  9. var line Line = Line{Text: "Test Me"}
  10. var output string = line.Output()
  11. var expect string = "Test Me"
  12. if output != expect {
  13. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  14. }
  15. if line.Update() {
  16. t.Error("Line: No updater, should return false")
  17. }
  18. line.DefaultColor = Color([]int{0})
  19. line.Width = 8
  20. output = line.Output()
  21. expect = "\x1b[0mTest Me "
  22. if output != expect {
  23. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  24. }
  25. // leave the default color, it is ignored when there's a render function
  26. // line.DefaultColor = ""
  27. line.RenderF = RenderBlueYellow
  28. output = line.Output()
  29. var blue string = ColorText("BOLD BLUE")
  30. var yellow string = ColorText("BOLD YELLOW")
  31. expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e "
  32. if output != expect {
  33. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  34. }
  35. }
  36. func TestLineUpdate(t *testing.T) {
  37. var counter int = 0
  38. uf := func() string {
  39. return fmt.Sprintf("Count: %d", counter)
  40. }
  41. var line Line = Line{Text: "", UpdateF: uf}
  42. line.Update()
  43. var output string = line.Output()
  44. var expect string = "Count: 0"
  45. if output != expect {
  46. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  47. }
  48. if line.Update() {
  49. t.Error("Unexpected Update: should have returned false. (no change)")
  50. }
  51. counter++
  52. if !line.Update() {
  53. t.Error("Missing Update: value was changed, Text should have changed")
  54. }
  55. output = line.Output()
  56. expect = "Count: 1"
  57. if output != expect {
  58. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  59. }
  60. }
  61. func TestLineUnicode(t *testing.T) {
  62. Unicode = true
  63. // code point > FFFF, use \U00000000 (not \u).
  64. var line Line = Line{Text: "Howdy \U0001f920"}
  65. var output string = line.Output()
  66. var expect string = "Howdy 🤠"
  67. if output != expect {
  68. t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
  69. }
  70. if StringLen(expect) != 8 {
  71. t.Errorf("LineUnicode Strlen: Expected 8, got %d", StringLen(expect))
  72. }
  73. // 🤠 = 2 chars.
  74. line.Width = 9
  75. output = line.Output()
  76. expect = "Howdy 🤠 "
  77. if output != expect {
  78. t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
  79. }
  80. }
  81. func TestLineCP437(t *testing.T) {
  82. Unicode = false
  83. var tests []string = []string{"\xdb TEXT \xdb",
  84. "\xf1 F1", "\xf2 F2", "\xf3 F3"}
  85. for _, test := range tests {
  86. var line Line = Line{Text: test}
  87. var output string = line.Output()
  88. var expect string = test
  89. if output != expect {
  90. t.Errorf("LineCP437: Expected %#v, got %#v", expect, output)
  91. }
  92. }
  93. }
  94. // Benchmarks / profiling
  95. func BenchmarkLine(b *testing.B) {
  96. var render = func(text string) string {
  97. var output strings.Builder
  98. var last *string
  99. // var r Render = Render{Line: text}
  100. var Up string = ColorText("BLUE")
  101. var Down string = ColorText("BOLD BLUE")
  102. var Num string = ColorText("BRI GREEN")
  103. var Sym string = ColorText("CYAN")
  104. for _, letter := range text {
  105. if unicode.IsUpper(letter) {
  106. if last != &Up {
  107. output.WriteString(Up)
  108. last = &Up
  109. }
  110. // r.Append(Up, 1)
  111. } else if unicode.IsLower(letter) {
  112. if last != &Down {
  113. output.WriteString(Down)
  114. last = &Down
  115. }
  116. // r.Append(Down, 1)
  117. } else if unicode.IsDigit(letter) {
  118. if last != &Num {
  119. output.WriteString(Num)
  120. last = &Num
  121. }
  122. // r.Append(Num, 1)
  123. } else {
  124. if last != &Sym {
  125. output.WriteString(Sym)
  126. last = &Sym
  127. }
  128. //r.Append(Sym, 1)
  129. }
  130. output.WriteRune(letter)
  131. // output.WriteString(string(letter))
  132. }
  133. return output.String()
  134. // return r.Result
  135. }
  136. for i := 0; i < b.N; i++ {
  137. var up = 0
  138. var updater = func() string {
  139. up++
  140. return fmt.Sprintf("The Value: %d", up)
  141. }
  142. var line Line = Line{Text: updater(),
  143. UpdateF: updater,
  144. RenderF: render,
  145. Width: 18}
  146. line.Output()
  147. for l := 0; l < 10; l++ {
  148. line.Update()
  149. }
  150. }
  151. }