bar_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 TestBarsUnicode(t *testing.T) {
  11. // verify that the BARS_CP437 matches BARS_UNICODE
  12. cp437 := BARS_CP437.solid
  13. convert := CP437_to_Unicode(cp437)
  14. unicode := BARS_UNICODE.solid
  15. if convert != unicode {
  16. t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
  17. }
  18. for half := 0; half < 2; half++ {
  19. cp437 := BARS_CP437.half[half]
  20. convert := CP437_to_Unicode(cp437)
  21. unicode := BARS_UNICODE.half[half]
  22. if convert != unicode {
  23. t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
  24. }
  25. }
  26. for grad := 0; grad < 4; grad++ {
  27. cp437 := BARS_CP437.gradient[grad]
  28. convert := CP437_to_Unicode(cp437)
  29. unicode := BARS_UNICODE.gradient[grad]
  30. if convert != unicode {
  31. t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
  32. }
  33. }
  34. }
  35. func TestBars(t *testing.T) {
  36. // This is normally done by door init, but we're skipping that right now
  37. Unicode = true
  38. BARS = BARS_UNICODE
  39. BarColor := ColorText("BLUE")
  40. Bar25 := ColorText("RED")
  41. Bar50 := ColorText("BROWN")
  42. Bar75 := ColorText("BOLD YEL")
  43. Bar95 := ColorText("GREEN")
  44. Bar100 := ColorText("BRI GREEN")
  45. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  46. bar.ColorRange = []BarRange{
  47. {2500, Bar25},
  48. {5000, Bar50},
  49. {7500, Bar75},
  50. {9500, Bar95},
  51. {10100, Bar100}}
  52. BarSolid := map[int]string{0: Bar25 + " ",
  53. 5: Bar25 + " ",
  54. 10: Bar25 + "\u2588 ",
  55. 20: Bar25 + "\u2588\u2588 ",
  56. 25: Bar25 + "\u2588\u2588 ",
  57. 30: Bar50 + "\u2588\u2588\u2588 ",
  58. 50: Bar50 + "\u2588\u2588\u2588\u2588\u2588 ",
  59. 75: Bar75 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  60. 90: Bar95 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  61. 95: Bar95 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588 ",
  62. 100: Bar100 + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  63. }
  64. for pct, text := range BarSolid {
  65. bar.Percent = int64(pct * 100)
  66. got := bar.Output()
  67. if got != text {
  68. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  69. }
  70. }
  71. BarColor = ColorText("BLA ON WHI")
  72. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  73. BarHalf := map[int]string{0: BarColor + " 0% ",
  74. 5: BarColor + "\u258c 5% ",
  75. 6: BarColor + "\u258c 6% ",
  76. 10: BarColor + "\u2588 10% ",
  77. 20: BarColor + "\u2588\u2588 20% ",
  78. 25: BarColor + "\u2588\u2588\u258c 25% ",
  79. 26: BarColor + "\u2588\u2588\u258c 26% ",
  80. 50: BarColor + "\u2588\u2588\u2588 50% ",
  81. 75: BarColor + "\u2588\u2588\u2588 75% ",
  82. 90: BarColor + "\u2588\u2588\u2588 90% \u2588 ",
  83. 100: BarColor + "\u2588\u2588\u2588 100 \u2588\u2588",
  84. }
  85. for pct, text := range BarHalf {
  86. bar.Percent = int64(pct * 100)
  87. got := bar.Output()
  88. if got != text {
  89. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  90. }
  91. }
  92. BarColor = ColorText("RED")
  93. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  94. BarGrad := map[int]string{0: BarColor + " ",
  95. 3: BarColor + "\u2591 ",
  96. 5: BarColor + "\u2592 ",
  97. 8: BarColor + "\u2593 ",
  98. 10: BarColor + "\u2588 ",
  99. 20: BarColor + "\u2588\u2588 ",
  100. 25: BarColor + "\u2588\u2588\u2592 ",
  101. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  102. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2592 ",
  103. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  104. }
  105. var percent int64
  106. bar.UpdateP = pctUpdate(&percent)
  107. for pct, text := range BarGrad {
  108. percent = int64(pct * 100)
  109. got := bar.Output()
  110. if got != text {
  111. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  112. }
  113. }
  114. }