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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package text
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
EnableColors()
}
func TestColor_EnableAndDisable(t *testing.T) {
defer EnableColors()
EnableColors()
assert.Equal(t, "\x1b[31mtest\x1b[0m", FgRed.Sprint("test"))
DisableColors()
assert.Equal(t, "test", FgRed.Sprint("test"))
EnableColors()
assert.Equal(t, "\x1b[31mtest\x1b[0m", FgRed.Sprint("test"))
}
func ExampleColor_EscapeSeq() {
fmt.Printf("Black Background: %#v\n", BgBlack.EscapeSeq())
fmt.Printf("Black Foreground: %#v\n", FgBlack.EscapeSeq())
// Output: Black Background: "\x1b[40m"
// Black Foreground: "\x1b[30m"
}
func TestColor_EscapeSeq(t *testing.T) {
assert.Equal(t, "\x1b[40m", BgBlack.EscapeSeq())
}
func ExampleColor_HTMLProperty() {
fmt.Printf("Bold: %#v\n", Bold.HTMLProperty())
fmt.Printf("Black Background: %#v\n", BgBlack.HTMLProperty())
fmt.Printf("Black Foreground: %#v\n", FgBlack.HTMLProperty())
// Output: Bold: "class=\"bold\""
// Black Background: "class=\"bg-black\""
// Black Foreground: "class=\"fg-black\""
}
func TestColor_HTMLProperty(t *testing.T) {
assert.Equal(t, "class=\"bold\"", Bold.HTMLProperty())
assert.Equal(t, "class=\"bg-black\"", BgBlack.HTMLProperty())
assert.Equal(t, "class=\"fg-black\"", FgBlack.HTMLProperty())
}
func ExampleColor_Sprint() {
fmt.Printf("%#v\n", BgBlack.Sprint("Black Background"))
fmt.Printf("%#v\n", FgBlack.Sprint("Black Foreground"))
// Output: "\x1b[40mBlack Background\x1b[0m"
// "\x1b[30mBlack Foreground\x1b[0m"
}
func TestColor_Sprint(t *testing.T) {
assert.Equal(t, "\x1b[31mtest true\x1b[0m", FgRed.Sprint("test ", true))
assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31mtrue\x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m", true))
assert.Equal(t, "\x1b[32mtest true\x1b[0m", FgRed.Sprint("\x1b[32mtest ", true))
assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31m \x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m "))
assert.Equal(t, "\x1b[32mtest\x1b[0m", FgRed.Sprint("\x1b[32mtest\x1b[0m"))
}
func ExampleColor_Sprintf() {
fmt.Printf("%#v\n", BgBlack.Sprintf("%s %s", "Black", "Background"))
fmt.Printf("%#v\n", FgBlack.Sprintf("%s %s", "Black", "Foreground"))
// Output: "\x1b[40mBlack Background\x1b[0m"
// "\x1b[30mBlack Foreground\x1b[0m"
}
func TestColor_Sprintf(t *testing.T) {
assert.Equal(t, "\x1b[31mtest true\x1b[0m", FgRed.Sprintf("test %s", "true"))
}
func ExampleColors_EscapeSeq() {
fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.EscapeSeq())
fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.EscapeSeq())
fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.EscapeSeq())
fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.EscapeSeq())
// Output: Black Background: "\x1b[40m"
// Black Foreground: "\x1b[30m"
// Black Background, White Foreground: "\x1b[40;37m"
// Black Foreground, White Background: "\x1b[30;47m"
}
func TestColors_EscapeSeq(t *testing.T) {
assert.Equal(t, "", Colors{}.EscapeSeq())
assert.Equal(t, "\x1b[40;37m", Colors{BgBlack, FgWhite}.EscapeSeq())
}
func ExampleColors_HTMLProperty() {
fmt.Printf("Black Background: %#v\n", Colors{BgBlack}.HTMLProperty())
fmt.Printf("Black Foreground: %#v\n", Colors{FgBlack}.HTMLProperty())
fmt.Printf("Black Background, White Foreground: %#v\n", Colors{BgBlack, FgWhite}.HTMLProperty())
fmt.Printf("Black Foreground, White Background: %#v\n", Colors{FgBlack, BgWhite}.HTMLProperty())
fmt.Printf("Bold Italic Underline Red Text: %#v\n", Colors{Bold, Italic, Underline, FgRed}.HTMLProperty())
// Output: Black Background: "class=\"bg-black\""
// Black Foreground: "class=\"fg-black\""
// Black Background, White Foreground: "class=\"bg-black fg-white\""
// Black Foreground, White Background: "class=\"bg-white fg-black\""
// Bold Italic Underline Red Text: "class=\"bold fg-red italic underline\""
}
func TestColors_HTMLProperty(t *testing.T) {
assert.Equal(t, "", Colors{}.HTMLProperty())
assert.Equal(t, "class=\"bg-black fg-white\"", Colors{BgBlack, FgWhite}.HTMLProperty())
assert.Equal(t, "class=\"bold fg-red\"", Colors{Bold, FgRed}.HTMLProperty())
}
func ExampleColors_Sprint() {
fmt.Printf("%#v\n", Colors{BgBlack}.Sprint("Black Background"))
fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprint("Black Background, White Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack}.Sprint("Black Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprint("Black Foreground, White Background"))
// Output: "\x1b[40mBlack Background\x1b[0m"
// "\x1b[40;37mBlack Background, White Foreground\x1b[0m"
// "\x1b[30mBlack Foreground\x1b[0m"
// "\x1b[30;47mBlack Foreground, White Background\x1b[0m"
}
func TestColors_Sprint(t *testing.T) {
assert.Equal(t, "test true", Colors{}.Sprint("test ", true))
assert.Equal(t, "\x1b[31mtest true\x1b[0m", Colors{FgRed}.Sprint("test ", true))
assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31mtrue\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m", true))
assert.Equal(t, "\x1b[32mtest true\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest ", true))
assert.Equal(t, "\x1b[32mtest\x1b[0m\x1b[31m \x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m "))
assert.Equal(t, "\x1b[32mtest\x1b[0m", Colors{FgRed}.Sprint("\x1b[32mtest\x1b[0m"))
}
func ExampleColors_Sprintf() {
fmt.Printf("%#v\n", Colors{BgBlack}.Sprintf("%s %s", "Black", "Background"))
fmt.Printf("%#v\n", Colors{BgBlack, FgWhite}.Sprintf("%s, %s", "Black Background", "White Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack}.Sprintf("%s %s", "Black", "Foreground"))
fmt.Printf("%#v\n", Colors{FgBlack, BgWhite}.Sprintf("%s, %s", "Black Foreground", "White Background"))
// Output: "\x1b[40mBlack Background\x1b[0m"
// "\x1b[40;37mBlack Background, White Foreground\x1b[0m"
// "\x1b[30mBlack Foreground\x1b[0m"
// "\x1b[30;47mBlack Foreground, White Background\x1b[0m"
}
func TestColors_Sprintf(t *testing.T) {
assert.Equal(t, "test true", Colors{}.Sprintf("test %s", "true"))
assert.Equal(t, "\x1b[31mtest true\x1b[0m", Colors{FgRed}.Sprintf("test %s", "true"))
}
|