You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

295 lines
6.8 KiB

  1. package color
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "github.com/mattn/go-colorable"
  8. )
  9. // Testing colors is kinda different. First we test for given colors and their
  10. // escaped formatted results. Next we create some visual tests to be tested.
  11. // Each visual test includes the color name to be compared.
  12. func TestColor(t *testing.T) {
  13. rb := new(bytes.Buffer)
  14. Output = rb
  15. NoColor = false
  16. testColors := []struct {
  17. text string
  18. code Attribute
  19. }{
  20. {text: "black", code: FgBlack},
  21. {text: "red", code: FgRed},
  22. {text: "green", code: FgGreen},
  23. {text: "yellow", code: FgYellow},
  24. {text: "blue", code: FgBlue},
  25. {text: "magent", code: FgMagenta},
  26. {text: "cyan", code: FgCyan},
  27. {text: "white", code: FgWhite},
  28. {text: "hblack", code: FgHiBlack},
  29. {text: "hred", code: FgHiRed},
  30. {text: "hgreen", code: FgHiGreen},
  31. {text: "hyellow", code: FgHiYellow},
  32. {text: "hblue", code: FgHiBlue},
  33. {text: "hmagent", code: FgHiMagenta},
  34. {text: "hcyan", code: FgHiCyan},
  35. {text: "hwhite", code: FgHiWhite},
  36. }
  37. for _, c := range testColors {
  38. New(c.code).Print(c.text)
  39. line, _ := rb.ReadString('\n')
  40. scannedLine := fmt.Sprintf("%q", line)
  41. colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
  42. escapedForm := fmt.Sprintf("%q", colored)
  43. fmt.Printf("%s\t: %s\n", c.text, line)
  44. if scannedLine != escapedForm {
  45. t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
  46. }
  47. }
  48. for _, c := range testColors {
  49. line := New(c.code).Sprintf("%s", c.text)
  50. scannedLine := fmt.Sprintf("%q", line)
  51. colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
  52. escapedForm := fmt.Sprintf("%q", colored)
  53. fmt.Printf("%s\t: %s\n", c.text, line)
  54. if scannedLine != escapedForm {
  55. t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
  56. }
  57. }
  58. }
  59. func TestColorEquals(t *testing.T) {
  60. fgblack1 := New(FgBlack)
  61. fgblack2 := New(FgBlack)
  62. bgblack := New(BgBlack)
  63. fgbgblack := New(FgBlack, BgBlack)
  64. fgblackbgred := New(FgBlack, BgRed)
  65. fgred := New(FgRed)
  66. bgred := New(BgRed)
  67. if !fgblack1.Equals(fgblack2) {
  68. t.Error("Two black colors are not equal")
  69. }
  70. if fgblack1.Equals(bgblack) {
  71. t.Error("Fg and bg black colors are equal")
  72. }
  73. if fgblack1.Equals(fgbgblack) {
  74. t.Error("Fg black equals fg/bg black color")
  75. }
  76. if fgblack1.Equals(fgred) {
  77. t.Error("Fg black equals Fg red")
  78. }
  79. if fgblack1.Equals(bgred) {
  80. t.Error("Fg black equals Bg red")
  81. }
  82. if fgblack1.Equals(fgblackbgred) {
  83. t.Error("Fg black equals fg black bg red")
  84. }
  85. }
  86. func TestNoColor(t *testing.T) {
  87. rb := new(bytes.Buffer)
  88. Output = rb
  89. testColors := []struct {
  90. text string
  91. code Attribute
  92. }{
  93. {text: "black", code: FgBlack},
  94. {text: "red", code: FgRed},
  95. {text: "green", code: FgGreen},
  96. {text: "yellow", code: FgYellow},
  97. {text: "blue", code: FgBlue},
  98. {text: "magent", code: FgMagenta},
  99. {text: "cyan", code: FgCyan},
  100. {text: "white", code: FgWhite},
  101. {text: "hblack", code: FgHiBlack},
  102. {text: "hred", code: FgHiRed},
  103. {text: "hgreen", code: FgHiGreen},
  104. {text: "hyellow", code: FgHiYellow},
  105. {text: "hblue", code: FgHiBlue},
  106. {text: "hmagent", code: FgHiMagenta},
  107. {text: "hcyan", code: FgHiCyan},
  108. {text: "hwhite", code: FgHiWhite},
  109. }
  110. for _, c := range testColors {
  111. p := New(c.code)
  112. p.DisableColor()
  113. p.Print(c.text)
  114. line, _ := rb.ReadString('\n')
  115. if line != c.text {
  116. t.Errorf("Expecting %s, got '%s'\n", c.text, line)
  117. }
  118. }
  119. // global check
  120. NoColor = true
  121. defer func() {
  122. NoColor = false
  123. }()
  124. for _, c := range testColors {
  125. p := New(c.code)
  126. p.Print(c.text)
  127. line, _ := rb.ReadString('\n')
  128. if line != c.text {
  129. t.Errorf("Expecting %s, got '%s'\n", c.text, line)
  130. }
  131. }
  132. }
  133. func TestColorVisual(t *testing.T) {
  134. // First Visual Test
  135. Output = colorable.NewColorableStdout()
  136. New(FgRed).Printf("red\t")
  137. New(BgRed).Print(" ")
  138. New(FgRed, Bold).Println(" red")
  139. New(FgGreen).Printf("green\t")
  140. New(BgGreen).Print(" ")
  141. New(FgGreen, Bold).Println(" green")
  142. New(FgYellow).Printf("yellow\t")
  143. New(BgYellow).Print(" ")
  144. New(FgYellow, Bold).Println(" yellow")
  145. New(FgBlue).Printf("blue\t")
  146. New(BgBlue).Print(" ")
  147. New(FgBlue, Bold).Println(" blue")
  148. New(FgMagenta).Printf("magenta\t")
  149. New(BgMagenta).Print(" ")
  150. New(FgMagenta, Bold).Println(" magenta")
  151. New(FgCyan).Printf("cyan\t")
  152. New(BgCyan).Print(" ")
  153. New(FgCyan, Bold).Println(" cyan")
  154. New(FgWhite).Printf("white\t")
  155. New(BgWhite).Print(" ")
  156. New(FgWhite, Bold).Println(" white")
  157. fmt.Println("")
  158. // Second Visual test
  159. Black("black")
  160. Red("red")
  161. Green("green")
  162. Yellow("yellow")
  163. Blue("blue")
  164. Magenta("magenta")
  165. Cyan("cyan")
  166. White("white")
  167. // Third visual test
  168. fmt.Println()
  169. Set(FgBlue)
  170. fmt.Println("is this blue?")
  171. Unset()
  172. Set(FgMagenta)
  173. fmt.Println("and this magenta?")
  174. Unset()
  175. // Fourth Visual test
  176. fmt.Println()
  177. blue := New(FgBlue).PrintlnFunc()
  178. blue("blue text with custom print func")
  179. red := New(FgRed).PrintfFunc()
  180. red("red text with a printf func: %d\n", 123)
  181. put := New(FgYellow).SprintFunc()
  182. warn := New(FgRed).SprintFunc()
  183. fmt.Fprintf(Output, "this is a %s and this is %s.\n", put("warning"), warn("error"))
  184. info := New(FgWhite, BgGreen).SprintFunc()
  185. fmt.Fprintf(Output, "this %s rocks!\n", info("package"))
  186. notice := New(FgBlue).FprintFunc()
  187. notice(os.Stderr, "just a blue notice to stderr")
  188. // Fifth Visual Test
  189. fmt.Println()
  190. fmt.Fprintln(Output, BlackString("black"))
  191. fmt.Fprintln(Output, RedString("red"))
  192. fmt.Fprintln(Output, GreenString("green"))
  193. fmt.Fprintln(Output, YellowString("yellow"))
  194. fmt.Fprintln(Output, BlueString("blue"))
  195. fmt.Fprintln(Output, MagentaString("magenta"))
  196. fmt.Fprintln(Output, CyanString("cyan"))
  197. fmt.Fprintln(Output, WhiteString("white"))
  198. }
  199. func TestNoFormat(t *testing.T) {
  200. fmt.Printf("%s %%s = ", BlackString("Black"))
  201. Black("%s")
  202. fmt.Printf("%s %%s = ", RedString("Red"))
  203. Red("%s")
  204. fmt.Printf("%s %%s = ", GreenString("Green"))
  205. Green("%s")
  206. fmt.Printf("%s %%s = ", YellowString("Yellow"))
  207. Yellow("%s")
  208. fmt.Printf("%s %%s = ", BlueString("Blue"))
  209. Blue("%s")
  210. fmt.Printf("%s %%s = ", MagentaString("Magenta"))
  211. Magenta("%s")
  212. fmt.Printf("%s %%s = ", CyanString("Cyan"))
  213. Cyan("%s")
  214. fmt.Printf("%s %%s = ", WhiteString("White"))
  215. White("%s")
  216. }
  217. func TestNoFormatString(t *testing.T) {
  218. tests := []struct {
  219. f func(string, ...interface{}) string
  220. format string
  221. args []interface{}
  222. want string
  223. }{
  224. {BlackString, "%s", nil, "\x1b[30m%s\x1b[0m"},
  225. {RedString, "%s", nil, "\x1b[31m%s\x1b[0m"},
  226. {GreenString, "%s", nil, "\x1b[32m%s\x1b[0m"},
  227. {YellowString, "%s", nil, "\x1b[33m%s\x1b[0m"},
  228. {BlueString, "%s", nil, "\x1b[34m%s\x1b[0m"},
  229. {MagentaString, "%s", nil, "\x1b[35m%s\x1b[0m"},
  230. {CyanString, "%s", nil, "\x1b[36m%s\x1b[0m"},
  231. {WhiteString, "%s", nil, "\x1b[37m%s\x1b[0m"},
  232. }
  233. for i, test := range tests {
  234. s := fmt.Sprintf("%s", test.f(test.format, test.args...))
  235. if s != test.want {
  236. t.Errorf("[%d] want: %q, got: %q", i, test.want, s)
  237. }
  238. }
  239. }