bar_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 TestBars(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, PercentStyle: PERCENT_SPACE}
  48. BarHalf := map[int]string{0: BarColor + " 0% ",
  49. 5: BarColor + "\u258c 5% ",
  50. 6: BarColor + "\u258c 6% ",
  51. 10: BarColor + "\u2588 10% ",
  52. 20: BarColor + "\u2588\u2588 20% ",
  53. 25: BarColor + "\u2588\u2588\u258c 25% ",
  54. 26: BarColor + "\u2588\u2588\u258c 26% ",
  55. 50: BarColor + "\u2588\u2588\u2588 50% ",
  56. 75: BarColor + "\u2588\u2588\u2588 75% ",
  57. 90: BarColor + "\u2588\u2588\u2588 90% \u2588 ",
  58. 100: BarColor + "\u2588\u2588\u2588 100 \u2588\u2588",
  59. }
  60. for pct, text := range BarHalf {
  61. bar.Percent = int64(pct * 100)
  62. got := bar.Output()
  63. if got != text {
  64. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  65. }
  66. }
  67. BarColor = ColorText("RED")
  68. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  69. BarGrad := map[int]string{0: BarColor + " ",
  70. 3: BarColor + "\u2591 ",
  71. 5: BarColor + "\u2592 ",
  72. 8: BarColor + "\u2593 ",
  73. 10: BarColor + "\u2588 ",
  74. 20: BarColor + "\u2588\u2588 ",
  75. 25: BarColor + "\u2588\u2588\u2592 ",
  76. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  77. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2592 ",
  78. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  79. }
  80. var percent int64
  81. bar.UpdateP = pctUpdate(&percent)
  82. for pct, text := range BarGrad {
  83. percent = int64(pct * 100)
  84. got := bar.Output()
  85. if got != text {
  86. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  87. }
  88. }
  89. }