File: colors_test.go

package info (click to toggle)
golang-github-theckman-yacspin 0.13.12-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 252 kB
  • sloc: sh: 8; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download
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
package yacspin

import (
	"fmt"
	"testing"

	"github.com/fatih/color"
)

func Test_validColor(t *testing.T) {
	validColors := []string{
		"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white",
		"reset", "bold", "faint", "italic", "underline", "blinkslow", "blinkrapid", "reversevideo", "concealed", "crossedout",
		"fgBlack", "fgRed", "fgGreen", "fgYellow", "fgBlue", "fgMagenta", "fgCyan", "fgWhite",
		"fgHiBlack", "fgHiRed", "fgHiGreen", "fgHiYellow", "fgHiBlue", "fgHiMagenta", "fgHiCyan", "fgHiWhite",
		"bgBlack", "bgRed", "bgGreen", "bgYellow", "bgBlue", "bgMagenta", "bgCyan", "bgWhite",
		"bgHiBlack", "bgHiRed", "bgHiGreen", "bgHiYellow", "bgHiBlue", "bgHiMagenta", "bgHiCyan", "bgHiWhite",
	}

	tests := []struct {
		name  string
		color string
		want  bool
	}{
		{
			name:  "invalid",
			color: "invalid",
			want:  false,
		},
	}

	for _, c := range validColors {
		tests = append(tests, struct {
			name  string
			color string
			want  bool
		}{
			name:  c,
			color: c,
			want:  true,
		})
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := validColor(tt.color); got != tt.want {
				t.Fatalf("validColor(%q) = %t, want %t", tt.color, got, tt.want)
			}
		})
	}
}

func Test_colorFunc(t *testing.T) {
	tests := []struct {
		name   string
		colors []string
		err    string
	}{
		{
			name: "no_color",
		},
		{
			name:   "color",
			colors: []string{"fgHiGreen"},
		},
		{
			name:   "colors",
			colors: []string{"fgHiGreen", "bgRed"},
		},
		{
			name:   "invalid_color",
			colors: []string{"fgHiGreen", "invalid", "bgRed"},
			err:    "invalid is not a valid color",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var tfn func(format string, a ...interface{}) string

			if len(tt.colors) == 0 {
				tfn = fmt.Sprintf
			} else {
				a := make([]color.Attribute, len(tt.colors))

				for i, c := range tt.colors {
					ca, ok := colorAttributeMap[c]
					if !ok {
						continue
					}
					a[i] = ca
				}

				tfn = color.New(a...).SprintfFunc()
			}

			fn, err := colorFunc(tt.colors...)

			if cont := testErrCheck(t, "colorFunc()", tt.err, err); !cont {
				return
			}

			if fn == nil {
				t.Fatal("fn is nil")
			}

			got, want := fn("%s: %d", "test value", 42), tfn("%s: %d", "test value", 42)

			if got != want {
				t.Fatalf(`fn("%%s: %%d", "test value", 42) = %q, want %q`, got, want)
			}
		})
	}
}