-
Notifications
You must be signed in to change notification settings - Fork 2
/
gocolor_test.go
100 lines (80 loc) · 1.97 KB
/
gocolor_test.go
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
package gocolor_test
import (
"fmt"
"reflect"
"testing"
"go.bbkane.com/gocolor"
)
// NOTE: these "tests" must be manually inspected to ensure they pass.
// I would love to use example tests ( https://go.dev/blog/examples ) but
// those seem to swallow stdout, which I need to see
func printTestName(t *testing.T, c *gocolor.Color) {
fmt.Println()
fmt.Println(
c.Add(
c.Underline+c.Bold+c.FgWhiteBright,
"# "+t.Name(),
),
)
fmt.Println()
}
// TestPrepare demos the Prepare convienience method to get colors
func TestPrepare(t *testing.T) {
color, err := gocolor.Prepare(true)
if err != nil {
t.Fatal(err)
}
printTestName(t, &color)
fmt.Println(
color.Add(color.FgRed, "FgRed"),
color.Add(color.FgCyanBright+color.Negative, "FgCyanBright+Negative"),
)
fmt.Println()
}
// TestManualCreation demos creating a Color object without using Prepare
func TestManualCreation(t *testing.T) {
err := gocolor.EnableConsole()
if err != nil {
t.Fatal(err)
}
//nolint:exhaustruct // This is too big to reasonably initialize
color := gocolor.Color{}
color.EnableAll()
printTestName(t, &color)
fmt.Println(
color.Add(color.FgYellowBright, "Hello!"),
color.Add(color.BgBlue+color.FgRed, "World!"),
)
fmt.Println()
}
// TestWithReflection demoes all colors - though not combined with each other :)
func TestWithReflection(t *testing.T) {
color, err := gocolor.Prepare(true)
if err != nil {
t.Fatal(err)
}
printTestName(t, &color)
e := reflect.ValueOf(&color).Elem()
for i := 0; i < e.NumField(); i++ {
name := e.Type().Field(i).Name
val := e.Field(i).Interface().(gocolor.Code)
fmt.Print(color.Add(val, name))
fmt.Print(" ")
if (i+1)%6 == 0 {
fmt.Println()
}
}
fmt.Println()
}
func TestFunc(t *testing.T) {
color, err := gocolor.Prepare(true)
if err != nil {
t.Fatal(err)
}
printTestName(t, &color)
green := color.Func(&color.FgGreenBright)
fmt.Println(green("Enabled!"))
color.DisableAll()
fmt.Println(green("Disabled!"))
fmt.Println()
}