X Tutup
Skip to content

Commit 8f27cb3

Browse files
committed
Add tests
1 parent f0d4fbf commit 8f27cb3

File tree

3 files changed

+147
-2
lines changed

3 files changed

+147
-2
lines changed

pkg/iostreams/iostreams.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func (s *IOStreams) ColorSupport256() bool {
5555
}
5656

5757
func (s *IOStreams) DetectTerminalTheme() string {
58-
style := os.Getenv("GLAMOUR_STYLE")
5958
if !s.ColorEnabled() {
6059
s.terminalTheme = "none"
6160
return "none"
@@ -66,6 +65,7 @@ func (s *IOStreams) DetectTerminalTheme() string {
6665
return "none"
6766
}
6867

68+
style := os.Getenv("GLAMOUR_STYLE")
6969
if style != "" && style != "auto" {
7070
s.terminalTheme = "none"
7171
return "none"

pkg/markdown/markdown.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Render(text, style string) (string, error) {
2525
}
2626

2727
func GetStyle(defaultStyle string) string {
28-
style := os.Getenv("GLAMOUR_STYLE")
28+
style := fromEnv()
2929
if style != "" && style != "auto" {
3030
return style
3131
}
@@ -36,3 +36,7 @@ func GetStyle(defaultStyle string) string {
3636

3737
return "notty"
3838
}
39+
40+
var fromEnv = func() string {
41+
return os.Getenv("GLAMOUR_STYLE")
42+
}

pkg/markdown/markdown_test.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package markdown
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Render(t *testing.T) {
10+
type input struct {
11+
text string
12+
style string
13+
}
14+
type output struct {
15+
wantsErr bool
16+
}
17+
tests := []struct {
18+
name string
19+
input input
20+
output output
21+
}{
22+
{
23+
name: "invalid glamour style",
24+
input: input{
25+
text: "some text",
26+
style: "invalid",
27+
},
28+
output: output{
29+
wantsErr: true,
30+
},
31+
},
32+
{
33+
name: "valid glamour style",
34+
input: input{
35+
text: "some text",
36+
style: "dark",
37+
},
38+
output: output{
39+
wantsErr: false,
40+
},
41+
},
42+
}
43+
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
_, err := Render(tt.input.text, tt.input.style)
47+
if tt.output.wantsErr {
48+
assert.Error(t, err)
49+
return
50+
}
51+
assert.NoError(t, err)
52+
})
53+
}
54+
}
55+
56+
func Test_GetStyle(t *testing.T) {
57+
type input struct {
58+
glamourStyle string
59+
terminalTheme string
60+
}
61+
type output struct {
62+
style string
63+
}
64+
tests := []struct {
65+
name string
66+
input input
67+
output output
68+
}{
69+
{
70+
name: "no glamour style and no terminal theme",
71+
input: input{
72+
glamourStyle: "",
73+
terminalTheme: "none",
74+
},
75+
output: output{
76+
style: "notty",
77+
},
78+
},
79+
{
80+
name: "auto glamour style and no terminal theme",
81+
input: input{
82+
glamourStyle: "auto",
83+
terminalTheme: "none",
84+
},
85+
output: output{
86+
style: "notty",
87+
},
88+
},
89+
{
90+
name: "user glamour style and no terminal theme",
91+
input: input{
92+
glamourStyle: "somestyle",
93+
terminalTheme: "none",
94+
},
95+
output: output{
96+
style: "somestyle",
97+
},
98+
},
99+
{
100+
name: "no glamour style and light terminal theme",
101+
input: input{
102+
glamourStyle: "",
103+
terminalTheme: "light",
104+
},
105+
output: output{
106+
style: "light",
107+
},
108+
},
109+
{
110+
name: "no glamour style and dark terminal theme",
111+
input: input{
112+
glamourStyle: "",
113+
terminalTheme: "dark",
114+
},
115+
output: output{
116+
style: "dark",
117+
},
118+
},
119+
{
120+
name: "no glamour style and unknown terminal theme",
121+
input: input{
122+
glamourStyle: "",
123+
terminalTheme: "unknown",
124+
},
125+
output: output{
126+
style: "notty",
127+
},
128+
},
129+
}
130+
131+
for _, tt := range tests {
132+
fromEnv = func() string {
133+
return tt.input.glamourStyle
134+
}
135+
136+
t.Run(tt.name, func(t *testing.T) {
137+
style := GetStyle(tt.input.terminalTheme)
138+
assert.Equal(t, tt.output.style, style)
139+
})
140+
}
141+
}

0 commit comments

Comments
 (0)
X Tutup