panel_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 TestX int = 2
  40. var TestY int = 2
  41. var p Panel = Panel{X: TestX, Y: TestY, Width: 3, Style: DOUBLE_SINGLE}
  42. var x int = 0
  43. var updater Updater = func() string {
  44. return fmt.Sprintf("%3d", x)
  45. }
  46. var l Line = Line{UpdateF: updater}
  47. l.Update()
  48. p.Lines = append(p.Lines, l)
  49. var expected string = Goto(TestX, TestY) + "╒═══╕" + Goto(TestX, TestY+1) + "│ 0│" + Goto(TestX, TestY+2) + "╘═══╛"
  50. var got string = p.Output()
  51. if expected != got {
  52. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  53. }
  54. x += 3
  55. got = p.Update()
  56. expected = Goto(TestX+1, TestY+1) + " 3"
  57. if expected != got {
  58. t.Errorf("Panel Update expected %#v, got %#v", expected, got)
  59. }
  60. got = p.Update()
  61. if got != "" {
  62. t.Errorf("Panel Update expected '', got %#v", got)
  63. }
  64. got = p.UpdateLine(0)
  65. if expected != got {
  66. t.Errorf("Panel UpdateLine expected %#v, got %#v", expected, got)
  67. }
  68. // Expected: Width = 3, Length = 1
  69. if p.Width != 3 {
  70. t.Errorf("Panel Width, expected %d, got %d\n", 3, p.Width)
  71. }
  72. if p.Length() != 1 {
  73. t.Errorf("Panel Length, expected %d, got %d\n", 1, p.Length())
  74. }
  75. got = p.GotoEnd()
  76. expected = Goto(p.X+p.Width+2, p.Y+p.Length()+2)
  77. if got != expected {
  78. t.Errorf("Panel GotoEnd expected %#v, got %#v", expected, got)
  79. }
  80. }