1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
package color
import (
"reflect"
"testing"
)
func TestFormat(t *testing.T) {
want := "\x1b[102;95mHello World\x1b[0m"
got := Format(BgHiGreen, FgHiMagenta, "Hello World")
if got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestMalformedFormat(t *testing.T) {
want := "\x1b[102mHello World(EXTRA color.Attribute=95)\x1b[0m"
got := Format(BgHiGreen, "Hello World", FgHiMagenta)
if got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestMalformedSliceFormat(t *testing.T) {
want := "\x1b[102mHello World(EXTRA color.Attribute=[95 41])\x1b[0m"
got := Format(BgHiGreen, "Hello World", []Attribute{FgHiMagenta, BgRed})
if got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestFormatSlice(t *testing.T) {
format := []Attribute{BgHiGreen, FgHiMagenta}
want := "\x1b[102;95mHello World\x1b[0m"
got := Format(format, "Hello World")
if got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestEmpty(t *testing.T) {
if want, got := "", Format(); got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestEmptyColorString(t *testing.T) {
if want, got := "", Format(BgBlack); got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestNoFormat(t *testing.T) {
want := "\x1b[mHello World\x1b[0m"
got := Format("Hello World")
if got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestFormatStartingWithNumber(t *testing.T) {
want := "\x1b[102;95m100 forks\x1b[0m"
number := 100
if reflect.TypeOf(number).String() != "int" {
t.Errorf("Must be integer; not a similar like Attribute")
}
if got := Format(BgHiGreen, FgHiMagenta, number, " forks"); got != want {
t.Errorf("Expecting %s, got '%s'\n", want, got)
}
}
func TestFormatCtrlChar(t *testing.T) {
if want, got := "\x1b[ma%b\x1b[0m", Format("a%b"); got != want {
t.Errorf(`expected Format(a%%b) to be %q, got %q instead`, want, got)
}
if want, got := "\\x1b[34;46ma%b\\x1b[0m", Escape(Format(FgBlue, BgCyan, "a%b")); got != want {
t.Errorf(`expected escaped formatted a%%b to be %q, got %q instead`, want, got)
}
}
func TestEscape(t *testing.T) {
unescaped := "\x1b[32mGreen"
escaped := "\\x1b[32mGreen"
if got := Escape(unescaped); got != escaped {
t.Errorf("Expecting %s, got '%s'\n", escaped, got)
}
}
func TestStripAttributes(t *testing.T) {
want := "this is a regular string"
got := StripAttributes(FgCyan, []Attribute{FgBlack}, "this is a regular string")
if got != want {
t.Errorf("StripAttributes(input) = %s, wanted %s", got, want)
}
}
func TestStripAttributesEmpty(t *testing.T) {
if got := StripAttributes(); got != "" {
t.Errorf("StripAttributes() should work")
}
}
func TestStripAttributesFirstParam(t *testing.T) {
want := "foo (EXTRA color.Attribute=32)"
got := StripAttributes("foo ", FgGreen)
if got != want {
t.Errorf(`expected StripAttributes = %v, got %v instead`, want, got)
}
}
func TestStripAttributesSame(t *testing.T) {
want := "this is a regular string"
got := StripAttributes(want)
if got != want {
t.Errorf("StripAttributes(%s) = %s, wanted %s", want, got, want)
}
}
func TestStripAttributesWithExtraColorAttribute(t *testing.T) {
want := "this is a regular string (EXTRA color.Attribute=91) with an invalid color Attribute field"
got := StripAttributes(BgCyan, []Attribute{FgBlack}, "this is a regular string ", FgHiRed, " with an invalid color Attribute field")
if got != want {
t.Errorf("StripAttributes(input) = %s, wanted %s", got, want)
}
}
|