-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtint_test.go
117 lines (93 loc) · 3.04 KB
/
tint_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tint
import (
"reflect"
"strings"
"testing"
)
var mod = Init()
func TestInit(t *testing.T) {
if !tintTypeCheck(*mod) {
t.Errorf("Init() not returning Tint struct %v", mod)
}
}
func TestLoggerInstance(t *testing.T) {
if mod.LogInstance == nil {
t.Error("Log instance is nil on module init.")
}
}
// In general test.Raw uses apply() which is the core function of tint module
// So in theory if we extensively test Raw() versions of all the functions
// we will gain the same results
// for all the printing functions like Println, Swatch etc ....
func TestRaw(t *testing.T) {
sameTest := mod.Raw("Hello world")
if sameTest != "Hello world" {
t.Error("Raw with normal text color does not return back same string")
}
}
func TestRawColor(t *testing.T) {
greenText := mod.Raw("Yes", Green)
if !strings.HasPrefix(greenText, Green.open) {
t.Error("Raw color test failed because it is not prefixed with green color from color map")
}
if !strings.HasSuffix(greenText, Green.close) {
t.Error("Raw color test failed because it is not suffixed with green color from color map")
}
}
// TestRawFandB tests foreground and background colors
func TestRawFandB(t *testing.T) {
greenText := mod.Raw("Yes", Green, BgWhite)
if !strings.HasPrefix(greenText, BgWhite.open+Green.open) {
t.Error("Foreground and Background tests failed order of prefix not correct.")
}
if !strings.HasSuffix(greenText, Green.close+BgWhite.close) {
t.Error("Foreground and Background tests failed order of suffix not correct.")
}
}
func TestSwatch(t *testing.T) {
yellowSwatchFunc := mod.Swatch(Yellow)
swatchType := reflect.TypeOf(yellowSwatchFunc).Kind()
if swatchType != reflect.Func {
t.Error("Swatch did not return function.")
}
}
func TestSwatchRawToReturnFunc(t *testing.T) {
yellowSwatchFunc := mod.SwatchRaw(Yellow)
swatchType := reflect.TypeOf(yellowSwatchFunc).Kind()
if swatchType != reflect.Func {
t.Error("SwatchRaw did not return function.")
}
}
func TestSwatchRaw(t *testing.T) {
yellowSwatchFunc := mod.SwatchRaw(Yellow)
result := yellowSwatchFunc("Yes")
if !strings.HasPrefix(result, Yellow.open) {
t.Error("Swatch did not have prefix of Yellow color", result)
}
if !strings.HasSuffix(result, Yellow.close) {
t.Error("Swatch did not have suffix of Yellow color", result)
}
}
func TestExp(t *testing.T) {
estr := mod.Exp("@(Ashish)", Yellow)
if !strings.Contains(estr, Yellow.open) || !strings.Contains(estr, Yellow.close) {
t.Error("Yellow color is not applied properly by tint.Expr(). Both brackets must be present.")
}
}
func TestExpf(t *testing.T) {
estr := mod.Expf("@(%s)", []Color{Yellow}, "Ashish")
if !strings.Contains(estr, Yellow.open) || !strings.Contains(estr, Yellow.close) {
t.Error("Yellow color is not applied properly by tint.Expr(). Both brackets must be present.")
}
if !strings.Contains(estr, "Ashish") {
t.Errorf("formatting strings did not work, couldn't find Ashish in estr: %s", estr)
}
}
func tintTypeCheck(value interface{}) bool {
switch value.(type) {
case Tint:
return true
default:
return false
}
}