line_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package door
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestLine(t *testing.T) {
  7. var line Line = Line{Text: "Test Me"}
  8. var output string = line.Output()
  9. var expect string = "Test Me"
  10. if output != expect {
  11. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  12. }
  13. if line.Update() {
  14. t.Error("Line: No updater, should return false")
  15. }
  16. line.DefaultColor = Color(0)
  17. output = line.Output()
  18. expect = "\x1b[0mTest Me"
  19. if output != expect {
  20. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  21. }
  22. // leave the default color, it is ignored when there's a render function
  23. // line.DefaultColor = ""
  24. line.RenderF = RenderBlueYellow
  25. output = line.Output()
  26. var blue string = ColorText("BOLD BLUE")
  27. var yellow string = ColorText("BOLD YELLOW")
  28. expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e"
  29. if output != expect {
  30. t.Errorf("Line: Expected %#v, got %#v", expect, output)
  31. }
  32. }
  33. func TestLineUpdate(t *testing.T) {
  34. var counter int = 0
  35. uf := func() string {
  36. return fmt.Sprintf("Count: %d", counter)
  37. }
  38. var line Line = Line{Text: "", UpdateF: uf}
  39. line.Update()
  40. var output string = line.Output()
  41. var expect string = "Count: 0"
  42. if output != expect {
  43. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  44. }
  45. if line.Update() {
  46. t.Error("Unexpected Update: should have returned false. (no change)")
  47. }
  48. counter++
  49. if !line.Update() {
  50. t.Error("Missing Update: value was changed, Text should have changed")
  51. }
  52. output = line.Output()
  53. expect = "Count: 1"
  54. if output != expect {
  55. t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
  56. }
  57. }