bar_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. cp437 := BARS_CP437.solid
  14. convert := CP437_to_Unicode(cp437)
  15. unicode := BARS_UNICODE.solid
  16. if convert != unicode {
  17. t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
  18. }
  19. for half := 0; half < 2; half++ {
  20. cp437 := BARS_CP437.half[half]
  21. convert := CP437_to_Unicode(cp437)
  22. unicode := BARS_UNICODE.half[half]
  23. if convert != unicode {
  24. t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
  25. }
  26. }
  27. for grad := 0; grad < 4; grad++ {
  28. cp437 := BARS_CP437.gradient[grad]
  29. convert := CP437_to_Unicode(cp437)
  30. unicode := BARS_UNICODE.gradient[grad]
  31. if convert != unicode {
  32. t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
  33. }
  34. }
  35. }
  36. func TestBarsUnicode(t *testing.T) {
  37. // This is normally done by door init, but we're skipping that right now
  38. Unicode = true
  39. BARS = BARS_UNICODE
  40. BarColor := ColorText("BLUE")
  41. Bar25 := ColorText("RED")
  42. Bar50 := ColorText("BROWN")
  43. Bar75 := ColorText("BOLD YEL")
  44. Bar95 := ColorText("GREEN")
  45. Bar100 := ColorText("BRI GREEN")
  46. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  47. bar.ColorRange = []BarRange{
  48. {2500, Bar25},
  49. {5000, Bar50},
  50. {7500, Bar75},
  51. {9500, Bar95},
  52. {10100, Bar100}}
  53. BarSolid := map[int]string{0: Bar25 + " ",
  54. 5: Bar25 + " ",
  55. 10: Bar25 + BARS.solid + " ",
  56. 20: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  57. 25: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  58. 30: Bar50 + strings.Repeat(BARS.solid, 3) + " ",
  59. 50: Bar50 + strings.Repeat(BARS.solid, 5) + " ",
  60. 75: Bar75 + strings.Repeat(BARS.solid, 7) + " ",
  61. 90: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  62. 95: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  63. 100: Bar100 + strings.Repeat(BARS.solid, 10) + "",
  64. }
  65. for pct, text := range BarSolid {
  66. bar.Percent = int64(pct * 100)
  67. got := bar.Output()
  68. if got != text {
  69. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  70. }
  71. }
  72. BarColor = ColorText("BLA ON WHI")
  73. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  74. BarHalf := map[int]string{0: BarColor + " 0% ",
  75. 5: BarColor + BARS.half[1] + " 5% ",
  76. 6: BarColor + BARS.half[1] + " 6% ",
  77. 10: BarColor + BARS.half[0] + " 10% ",
  78. 20: BarColor + BARS.solid + BARS.half[0] + " 20% ",
  79. 25: BarColor + BARS.solid + BARS.half[0] + BARS.half[1] + " 25% ",
  80. 26: BarColor + BARS.solid + BARS.half[0] + BARS.half[1] + " 26% ",
  81. 50: BarColor + strings.Repeat(BARS.half[0], 3) + " 50% ",
  82. 75: BarColor + strings.Repeat(BARS.half[0], 3) + " 75% ",
  83. 90: BarColor + strings.Repeat(BARS.half[0], 3) + " 90% \u2588 ",
  84. 100: BarColor + "\u2588\u2588\u2588 100 \u2588\u2588",
  85. }
  86. for pct, text := range BarHalf {
  87. bar.Percent = int64(pct * 100)
  88. got := bar.Output()
  89. if got != text {
  90. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  91. }
  92. }
  93. BarColor = ColorText("RED")
  94. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  95. BarGrad := map[int]string{0: BarColor + " ",
  96. 3: BarColor + "\u2591 ",
  97. 5: BarColor + "\u2592 ",
  98. 8: BarColor + "\u2593 ",
  99. 10: BarColor + "\u2588 ",
  100. 20: BarColor + "\u2588\u2588 ",
  101. 25: BarColor + "\u2588\u2588\u2592 ",
  102. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  103. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2592 ",
  104. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  105. }
  106. var percent int64
  107. bar.UpdateP = pctUpdate(&percent)
  108. for pct, text := range BarGrad {
  109. percent = int64(pct * 100)
  110. got := bar.Output()
  111. if got != text {
  112. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  113. }
  114. }
  115. }
  116. func TestBarsCP437(t *testing.T) {
  117. // This is normally done by door init, but we're skipping that right now
  118. Unicode = false
  119. CP437 = true
  120. BARS = BARS_CP437
  121. BarColor := ColorText("BLUE")
  122. Bar25 := ColorText("RED")
  123. Bar50 := ColorText("BROWN")
  124. Bar75 := ColorText("BOLD YEL")
  125. Bar95 := ColorText("GREEN")
  126. Bar100 := ColorText("BRI GREEN")
  127. bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID}
  128. bar.ColorRange = []BarRange{
  129. {2500, Bar25},
  130. {5000, Bar50},
  131. {7500, Bar75},
  132. {9500, Bar95},
  133. {10100, Bar100}}
  134. BarSolid := map[int]string{0: Bar25 + " ",
  135. 5: Bar25 + " ",
  136. 10: Bar25 + BARS.solid + " ",
  137. 20: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  138. 25: Bar25 + strings.Repeat(BARS.solid, 2) + " ",
  139. 30: Bar50 + strings.Repeat(BARS.solid, 3) + " ",
  140. 50: Bar50 + strings.Repeat(BARS.solid, 5) + " ",
  141. 75: Bar75 + strings.Repeat(BARS.solid, 7) + " ",
  142. 90: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  143. 95: Bar95 + strings.Repeat(BARS.solid, 9) + " ",
  144. 100: Bar100 + strings.Repeat(BARS.solid, 10) + "",
  145. }
  146. for pct, text := range BarSolid {
  147. bar.Percent = int64(pct * 100)
  148. got := bar.Output()
  149. if got != text {
  150. t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got)
  151. }
  152. }
  153. BarColor = ColorText("BLA ON WHI")
  154. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
  155. BarHalf := map[int]string{0: BarColor + " 0% ",
  156. 5: BarColor + "\u258c 5% ",
  157. 6: BarColor + "\u258c 6% ",
  158. 10: BarColor + "\u2588 10% ",
  159. 20: BarColor + "\u2588\u2588 20% ",
  160. 25: BarColor + "\u2588\u2588\u258c 25% ",
  161. 26: BarColor + "\u2588\u2588\u258c 26% ",
  162. 50: BarColor + "\u2588\u2588\u2588 50% ",
  163. 75: BarColor + "\u2588\u2588\u2588 75% ",
  164. 90: BarColor + "\u2588\u2588\u2588 90% \u2588 ",
  165. 100: BarColor + "\u2588\u2588\u2588 100 \u2588\u2588",
  166. }
  167. for pct, text := range BarHalf {
  168. bar.Percent = int64(pct * 100)
  169. got := bar.Output()
  170. if got != text {
  171. t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got)
  172. }
  173. }
  174. BarColor = ColorText("RED")
  175. bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
  176. BarGrad := map[int]string{0: BarColor + " ",
  177. 3: BarColor + "\u2591 ",
  178. 5: BarColor + "\u2592 ",
  179. 8: BarColor + "\u2593 ",
  180. 10: BarColor + "\u2588 ",
  181. 20: BarColor + "\u2588\u2588 ",
  182. 25: BarColor + "\u2588\u2588\u2592 ",
  183. 50: BarColor + "\u2588\u2588\u2588\u2588\u2588 ",
  184. 75: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2592 ",
  185. 100: BarColor + "\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588",
  186. }
  187. var percent int64
  188. bar.UpdateP = pctUpdate(&percent)
  189. for pct, text := range BarGrad {
  190. percent = int64(pct * 100)
  191. got := bar.Output()
  192. if got != text {
  193. t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got)
  194. }
  195. }
  196. }