panel_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package door
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. )
  7. func TestPanel(t *testing.T) {
  8. var p Panel = Panel{X: 5, Y: 7, Width: 10, Style: DOUBLE, Title: "Test"}
  9. p.Lines = append(p.Lines, Line{Text: "1234567890"})
  10. p.Lines = append(p.Lines, Line{Text: "abcdefghij"})
  11. var expected string = Goto(5, 7) + "╔Test══════╗" + Goto(5, 8) + "║1234567890║" + Goto(5, 9) + "║abcdefghij║" + Goto(5, 10) + "╚══════════╝"
  12. var got string = p.Output()
  13. var es []string = strings.SplitAfter(expected, "5H")
  14. var gs []string = strings.SplitAfter(got, "5H")
  15. for idx, exp := range es {
  16. var g string = gs[idx]
  17. if g != exp {
  18. t.Errorf("Panel expected %#v, got %#v", exp, g)
  19. }
  20. }
  21. p.TitleOffset = 3
  22. got = p.Output()
  23. gs = strings.SplitAfter(got, "5H")
  24. es[0] = "╔═══Test═══╗" + Goto(5, 8)
  25. if gs[1] != es[0] {
  26. t.Errorf("Panel TitleOffset=3 expected %#v, got %#v", es[0], gs[1])
  27. }
  28. }
  29. func TestPanelSpacer(t *testing.T) {
  30. var p Panel = Panel{X: 1, Y: 1, Width: 5, Style: SINGLE}
  31. p.Lines = append(p.Lines, p.Spacer())
  32. var expected string = Goto(1, 1) + "┌─────┐" + Goto(1, 2) + "├─────┤" + Goto(1, 3) + "└─────┘"
  33. var got string = p.Output()
  34. if expected != got {
  35. t.Errorf("Panel Spacer expected %#v, got %#v", expected, got)
  36. }
  37. }
  38. func TestPanelUpdate(t *testing.T) {
  39. var p Panel = Panel{X: 2, Y: 2, Width: 3, Style: DOUBLE_SINGLE}
  40. var x int = 0
  41. var updater Updater = func() string {
  42. return fmt.Sprintf("%3d", x)
  43. }
  44. var l Line = Line{UpdateF: updater}
  45. l.Update()
  46. p.Lines = append(p.Lines, l)
  47. var expected string = Goto(2, 2) + "╒═══╕" + Goto(2, 3) + "│ 0│" + Goto(2, 4) + "╘═══╛"
  48. var got string = p.Output()
  49. if expected != got {
  50. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  51. }
  52. x += 3
  53. got = p.Update()
  54. expected = Goto(3, 3) + " 3"
  55. if expected != got {
  56. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  57. }
  58. got = p.Update()
  59. if got != "" {
  60. t.Errorf("Panel Update expected '', got %#v", got)
  61. }
  62. got = p.UpdateLine(0)
  63. if expected != got {
  64. t.Errorf("Panel UpdateLine expected %#v, got %#v", expected, got)
  65. }
  66. got = p.GotoEnd()
  67. expected = Goto(7, 4)
  68. if got != expected {
  69. t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
  70. }
  71. }