bar_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package door
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func pctUpdate(pct *int64) func() int64 {
  7. return func() int64 {
  8. return *pct
  9. }
  10. }
  11. func TestBarsMatch(t *testing.T) {
  12. // verify that the BARS_CP437 matches BARS_UNICODE
  13. // When translated via CP437_to_Unicode().
  14. cp437 := BARS_CP437.solid
  15. convert := CP437_to_Unicode(cp437)
  16. unicode := BARS_UNICODE.solid
  17. if convert != unicode {
  18. t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
  19. }
  20. for half := 0; half < 2; half++ {
  21. cp437 := BARS_CP437.half[half]
  22. convert := CP437_to_Unicode(cp437)
  23. unicode := BARS_UNICODE.half[half]
  24. if convert != unicode {
  25. t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
  26. }
  27. }
  28. for grad := 0; grad < 4; grad++ {
  29. cp437 := BARS_CP437.gradient[grad]
  30. convert := CP437_to_Unicode(cp437)
  31. unicode := BARS_UNICODE.gradient[grad]
  32. if convert != unicode {
  33. t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
  34. }
  35. }
  36. }
  37. func TestBarsUnicode(t *testing.T) {
  38. // Init as if we're in Unicode mode
  39. Unicode = true
  40. CP437 = false
  41. BARS = BARS_UNICODE
  42. testBars(t)
  43. }
  44. func TestBarsCP437(t *testing.T) {
  45. // Init as if we're in CP437 mode
  46. Unicode = false
  47. CP437 = true
  48. BARS = BARS_CP437
  49. testBars(t)
  50. }
  51. func testBars(t *testing.T) {
  52. // Generic function that tests output by building results
  53. // from BARS structure. (Can test Unicode and CP437.)
  54. BarColor := ColorText("BLUE")
  55. Bar25 := ColorText("RED")
  56. Bar50 := ColorText("BROWN")
  57. Bar75 := ColorText("BOLD YEL")
  58. Bar95 := ColorText("GREEN")
  59. Bar100 := ColorText("BRI GREEN")
  60. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  61. bar.ColorRange = []BarRange{
  62. {2500, Bar25},
  63. {5000, Bar50},
  64. {7500, Bar75},
  65. {9500, Bar95},
  66. {10100, Bar100}}
  67. BarSolid := map[int]string{0: Bar25 + " ",
  68. 5: Bar25 + " ",
  69. 10: Bar25 + BARS.solid + " ",
  70. 20: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  71. 25: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  72. 30: Bar50 + strings.Repeat(BARS.solid, 3) + " ",
  73. 50: Bar50 + strings.Repeat(BARS.solid, 5) + " ",
  74. 75: Bar75 + strings.Repeat(BARS.solid, 7) + " ",
  75. 90: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  76. 95: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  77. 100: Bar100 + strings.Repeat(BARS.solid, 10) + "",
  78. }
  79. for pct, text := range BarSolid {
  80. bar.Percent = int64(pct * 100)
  81. got := bar.Output()
  82. if got != text {
  83. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  84. }
  85. }
  86. BarColor = ColorText("BLA ON WHI")
  87. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  88. BarHalf := map[int]string{0: BarColor + " 0% ",
  89. 5: BarColor + BARS.half[1] + " 5% ",
  90. 6: BarColor + BARS.half[1] + " 6% ",
  91. 10: BarColor + BARS.half[0] + " 10% ",
  92. 20: BarColor + BARS.half[0] + BARS.half[0] + " 20% ",
  93. 25: BarColor + BARS.half[0] + BARS.half[0] + BARS.half[1] + " 25% ",
  94. 26: BarColor + BARS.half[0] + BARS.half[0] + BARS.half[1] + " 26% ",
  95. 50: BarColor + strings.Repeat(BARS.half[0], 3) + " 50% ",
  96. 75: BarColor + strings.Repeat(BARS.half[0], 3) + " 75% ",
  97. 90: BarColor + strings.Repeat(BARS.half[0], 3) + " 90% " + BARS.half[0] + " ",
  98. 100: BarColor + strings.Repeat(BARS.half[0], 3) + " 100 " + strings.Repeat(BARS.half[0], 2),
  99. }
  100. for pct, text := range BarHalf {
  101. bar.Percent = int64(pct * 100)
  102. got := bar.Output()
  103. if got != text {
  104. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  105. }
  106. }
  107. BarColor = ColorText("RED")
  108. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  109. BarGrad := map[int]string{0: BarColor + " ",
  110. 3: BarColor + BARS.gradient[1] + " ",
  111. 5: BarColor + BARS.gradient[2] + " ",
  112. 8: BarColor + BARS.gradient[3] + " ",
  113. 10: BarColor + BARS.gradient[0] + " ",
  114. 20: BarColor + BARS.gradient[0] + BARS.gradient[0] + " ",
  115. 25: BarColor + BARS.gradient[0] + BARS.gradient[0] + BARS.gradient[2] + " ",
  116. 50: BarColor + strings.Repeat(BARS.gradient[0], 5) + " ",
  117. 75: BarColor + strings.Repeat(BARS.gradient[0], 7) + BARS.gradient[2] + " ",
  118. 100: BarColor + strings.Repeat(BARS.gradient[0], 10),
  119. }
  120. var percent int64
  121. bar.UpdateP = pctUpdate(&percent)
  122. for pct, text := range BarGrad {
  123. percent = int64(pct * 100)
  124. got := bar.Output()
  125. if got != text {
  126. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  127. }
  128. }
  129. }