bar_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package door
  2. import (
  3. "testing"
  4. )
  5. func pctUpdate(pct *int64) func() int64 {
  6. return func() int64 {
  7. return *pct
  8. }
  9. }
  10. func TestBarHalfStep(t *testing.T) {
  11. // This is normally done by door init, but we're skipping that right now
  12. Unicode = true
  13. BOXES = BOXES_UNICODE
  14. BarColor := ColorText("BLUE")
  15. Bar25 := ColorText("RED")
  16. Bar50 := ColorText("BROWN")
  17. Bar75 := ColorText("BOLD YEL")
  18. Bar95 := ColorText("GREEN")
  19. Bar100 := ColorText("BRI GREEN")
  20. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  21. bar.ColorRange = []BarRange{
  22. {2500, Bar25},
  23. {5000, Bar50},
  24. {7500, Bar75},
  25. {9500, Bar95},
  26. {10100, Bar100}}
  27. BarSolid := map[int]string{0: Bar25 + " ",
  28. 5: Bar25 + " ",
  29. 10: Bar25 + "\u2588 ",
  30. 20: Bar25 + "\u2588\u2588 ",
  31. 25: Bar25 + "\u2588\u2588 ",
  32. 30: Bar50 + "\u2588\u2588\u2588 ",
  33. 50: Bar50 + "\u2588\u2588\u2588\u2588\u2588 ",
  34. 75: Bar75 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  35. 90: Bar95 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  36. 95: Bar95 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  37. 100: Bar100 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  38. }
  39. for pct, text := range BarSolid {
  40. bar.Percent = int64(pct * 100)
  41. got := bar.Output()
  42. if got != text {
  43. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  44. }
  45. }
  46. BarColor = ColorText("BLA ON WHI")
  47. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP}
  48. BarHalf := map[int]string{0: BarColor + " ",
  49. 5: BarColor + "\u258c ",
  50. 6: BarColor + "\u258c ",
  51. 10: BarColor + "\u2588 ",
  52. 20: BarColor + "\u2588\u2588 ",
  53. 25: BarColor + "\u2588\u2588\u258c ",
  54. 26: BarColor + "\u2588\u2588\u258c ",
  55. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  56. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258c ",
  57. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  58. }
  59. for pct, text := range BarHalf {
  60. bar.Percent = int64(pct * 100)
  61. got := bar.Output()
  62. if got != text {
  63. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  64. }
  65. }
  66. BarColor = ColorText("RED")
  67. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  68. BarGrad := map[int]string{0: BarColor + " ",
  69. 3: BarColor + "\u2591 ",
  70. 5: BarColor + "\u2592 ",
  71. 8: BarColor + "\u2593 ",
  72. 10: BarColor + "\u2588 ",
  73. 20: BarColor + "\u2588\u2588 ",
  74. 25: BarColor + "\u2588\u2588\u2592 ",
  75. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  76. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2592 ",
  77. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  78. }
  79. var percent int64
  80. bar.UpdateP = pctUpdate(&percent)
  81. for pct, text := range BarGrad {
  82. percent = int64(pct * 100)
  83. got := bar.Output()
  84. if got != text {
  85. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  86. }
  87. }
  88. }