panel_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package door
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. )
  7. func TestPanel(t *testing.T) {
  8. p := 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. expected := Goto(5, 7) + "╔Test══════╗" + Goto(5, 8) + "║1234567890║" + Goto(5, 9) + "║abcdefghij║" + Goto(5, 10) + "╚══════════╝"
  12. got := p.Output()
  13. es := strings.SplitAfter(expected, "5H")
  14. gs := strings.SplitAfter(got, "5H")
  15. for idx, exp := range es {
  16. g := 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. p := Panel{X: 1, Y: 1, Width: 5, Style: SINGLE}
  31. p.Lines = append(p.Lines, p.Spacer())
  32. expected := Goto(1, 1) + "┌─────┐" + Goto(1, 2) + "├─────┤" + Goto(1, 3) + "└─────┘"
  33. got := 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. p := Panel{X: 2, Y: 2, Width: 3, Style: DOUBLE_SINGLE}
  40. var x int = 0
  41. updater := func() string {
  42. return fmt.Sprintf("%3d", x)
  43. }
  44. l := Line{UpdateF: updater}
  45. l.Update()
  46. p.Lines = append(p.Lines, l)
  47. expected := Goto(2, 2) + "╒═══╕" + Goto(2, 3) + "│ 0│" + Goto(2, 4) + "╘═══╛"
  48. got := 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. }