panel_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package door
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. )
  8. func TestPanel(t *testing.T) {
  9. var p Panel = Panel{X: 5, Y: 7, Width: 10, Style: DOUBLE, Title: "Test"}
  10. p.Lines = append(p.Lines, NewLine("1234567890"))
  11. p.Lines = append(p.Lines, NewLine("abcdefghij"))
  12. var expected string = string(Goto(5, 7)) + "╔Test══════╗" +
  13. string(Goto(5, 8)) + "║1234567890║" +
  14. string(Goto(5, 9)) + "║abcdefghij║" +
  15. string(Goto(5, 10)) + "╚══════════╝"
  16. var got string = string(p.Output())
  17. var es []string = strings.SplitAfter(expected, "5H")
  18. var gs []string = strings.SplitAfter(got, "5H")
  19. for idx, exp := range es {
  20. var g string = gs[idx]
  21. if g != exp {
  22. t.Errorf("Panel expected %#v, got %#v", exp, g)
  23. }
  24. }
  25. p.TitleOffset = 3
  26. got = string(p.Output())
  27. gs = strings.SplitAfter(got, "5H")
  28. es[0] = "╔═══Test═══╗" + string(Goto(5, 8))
  29. if gs[1] != es[0] {
  30. t.Errorf("Panel TitleOffset=3 expected %#v, got %#v", es[0], gs[1])
  31. }
  32. }
  33. func TestPanelSpacer(t *testing.T) {
  34. var p Panel = Panel{X: 1, Y: 1, Width: 5, Style: SINGLE}
  35. p.Lines = append(p.Lines, p.Spacer())
  36. var expected string = string(Goto(1, 1)) + "┌─────┐" +
  37. string(Goto(1, 2)) + "├─────┤" +
  38. string(Goto(1, 3)) + "└─────┘"
  39. var got string = string(p.Output())
  40. if expected != got {
  41. t.Errorf("Panel Spacer expected %#v, got %#v", expected, got)
  42. }
  43. }
  44. func TestPanelUpdate(t *testing.T) {
  45. var TestX int = 2
  46. var TestY int = 2
  47. var p Panel = Panel{X: TestX, Y: TestY, Width: 3, Style: DOUBLE_SINGLE}
  48. var x int = 0
  49. var updater Updater = func(u *bytes.Buffer) {
  50. u.Reset()
  51. fmt.Fprintf(u, "%3d", x)
  52. }
  53. var l *Line = &Line{UpdateF: updater}
  54. l.Update()
  55. p.Lines = append(p.Lines, l)
  56. var expected string = string(Goto(TestX, TestY)) + "╒═══╕" +
  57. string(Goto(TestX, TestY+1)) + "│ 0│" +
  58. string(Goto(TestX, TestY+2)) + "╘═══╛"
  59. var got string = string(p.Output())
  60. if expected != got {
  61. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  62. }
  63. x += 3
  64. got = string(p.Update())
  65. expected = string(Goto(TestX+1, TestY+1)) + " 3"
  66. if expected != got {
  67. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  68. }
  69. got = string(p.Update())
  70. if got != "" {
  71. t.Errorf("Panel Update expected '', got %#v", got)
  72. }
  73. got = string(p.UpdateLine(0))
  74. if expected != got {
  75. t.Errorf("Panel UpdateLine expected %#v, got %#v", expected, got)
  76. }
  77. // Expected: Width = 3, Length = 1
  78. if p.Width != 3 {
  79. t.Errorf("Panel Width, expected %d, got %d\n", 3, p.Width)
  80. }
  81. if p.Length() != 1 {
  82. t.Errorf("Panel Length, expected %d, got %d\n", 1, p.Length())
  83. }
  84. got = string(p.GotoEnd())
  85. expected = string(Goto(p.X+p.Width+2, p.Y+p.Length()+1))
  86. if got != expected {
  87. t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
  88. }
  89. }
  90. func BenchmarkPanel(b *testing.B) {
  91. Unicode = false
  92. var TestX int = 2
  93. var TestY int = 2
  94. var p Panel = Panel{X: TestX, Y: TestY, Width: 3, Style: DOUBLE_SINGLE}
  95. var x int = 0
  96. var updater Updater = func(u *bytes.Buffer) {
  97. u.Reset()
  98. fmt.Fprintf(u, "%3d", x)
  99. }
  100. var l *Line = &Line{UpdateF: updater}
  101. l.Update()
  102. p.Lines = append(p.Lines, l)
  103. p.Lines = append(p.Lines, l)
  104. p.Lines = append(p.Lines, l)
  105. for n := 0; n < b.N; n++ {
  106. x = n
  107. p.Update()
  108. }
  109. }
  110. func BenchmarkPanelRender(b *testing.B) {
  111. Unicode = false
  112. var TestX int = 2
  113. var TestY int = 2
  114. var p Panel = Panel{X: TestX, Y: TestY, Width: 8, Style: DOUBLE_SINGLE}
  115. var x int = 0
  116. var updater Updater = func(u *bytes.Buffer) {
  117. u.Reset()
  118. // Give this text that it will have to do something with (MiXeD CaSe)
  119. if x%2 == 0 {
  120. fmt.Fprintf(u, "TeSt %3d", x)
  121. } else {
  122. fmt.Fprintf(u, "tEsT %3d", x)
  123. }
  124. }
  125. var l *Line = &Line{UpdateF: updater, RenderF: RenderUppercase("BRI WHI ON BLUE", "GREEN")}
  126. l.Update()
  127. p.Lines = append(p.Lines, l)
  128. p.Lines = append(p.Lines, l)
  129. p.Lines = append(p.Lines, l)
  130. for n := 0; n < b.N; n++ {
  131. x = n
  132. p.Update()
  133. }
  134. }