Skip to content

Commit f36f3c0

Browse files
committed
test: Increasing provider package coverage
1 parent 4d43eb0 commit f36f3c0

File tree

3 files changed

+125
-10
lines changed

3 files changed

+125
-10
lines changed

pkg/provider/request_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"errors"
66
"fmt"
7+
"net/http"
78
"reflect"
89
"testing"
910

@@ -101,6 +102,48 @@ func TestGetURI(t *testing.T) {
101102
}
102103
}
103104

105+
func TestLayoutPath(t *testing.T) {
106+
type args struct {
107+
path string
108+
}
109+
110+
var cases = []struct {
111+
intention string
112+
instance Request
113+
args args
114+
want string
115+
}{
116+
{
117+
"empty list",
118+
Request{},
119+
args{
120+
path: "/reports",
121+
},
122+
"grid",
123+
},
124+
{
125+
"empty list",
126+
Request{
127+
Preferences: Preferences{
128+
ListLayoutPath: []string{"/sheets", "/reports"},
129+
},
130+
},
131+
args{
132+
path: "/reports",
133+
},
134+
"list",
135+
},
136+
}
137+
138+
for _, tc := range cases {
139+
t.Run(tc.intention, func(t *testing.T) {
140+
if got := tc.instance.LayoutPath(tc.args.path); got != tc.want {
141+
t.Errorf("LayoutPath() = `%s`, want `%s`", got, tc.want)
142+
}
143+
})
144+
}
145+
}
146+
104147
func TestCheckPassword(t *testing.T) {
105148
password, err := bcrypt.GenerateFromPassword([]byte("test"), bcrypt.DefaultCost)
106149
if err != nil {
@@ -215,6 +258,32 @@ func TestNewError(t *testing.T) {
215258
}
216259
}
217260

261+
func TestError(t *testing.T) {
262+
type args struct {
263+
content string
264+
}
265+
266+
var cases = []struct {
267+
intention string
268+
instance *Error
269+
want string
270+
}{
271+
{
272+
"simple",
273+
NewError(http.StatusTeapot, errors.New("I'm a teapot")),
274+
"HTTP/418: I'm a teapot",
275+
},
276+
}
277+
278+
for _, tc := range cases {
279+
t.Run(tc.intention, func(t *testing.T) {
280+
if got := tc.instance.Error(); got != tc.want {
281+
t.Errorf("Error() = `%s`, want `%s`", got, tc.want)
282+
}
283+
})
284+
}
285+
}
286+
218287
func TestExtension(t *testing.T) {
219288
var cases = []struct {
220289
intention string

pkg/provider/utils.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ import (
1515
)
1616

1717
var (
18-
transformer transform.Transformer
18+
transformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
19+
transliterations = map[string]string{
20+
"Ð": "D",
21+
"Ł": "l",
22+
"Ø": "oe",
23+
"Þ": "Th",
24+
"ß": "ss",
25+
"æ": "ae",
26+
"ð": "d",
27+
"ł": "l",
28+
"ø": "oe",
29+
"þ": "th",
30+
"œ": "oe",
31+
}
32+
quotesChar = regexp.MustCompile(`["'` + "`" + `](?m)`)
1933
specialChars = regexp.MustCompile(`[^a-z0-9.\-_/](?m)`)
2034
)
2135

22-
func init() {
23-
transformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
24-
}
25-
2636
// GetPathname computes pathname for given params
2737
func GetPathname(folder, name string, share *Share) string {
2838
parts := make([]string, 0)
@@ -59,20 +69,28 @@ func GetURI(folder, name string, share *Share) string {
5969

6070
// SanitizeName return sanitized name (remove diacritics)
6171
func SanitizeName(name string, removeSlash bool) (string, error) {
62-
withoutDiacritics, _, err := transform.String(transformer, strings.ToLower(name))
72+
withoutLigatures := strings.ToLower(name)
73+
for key, value := range transliterations {
74+
if strings.Contains(withoutLigatures, key) {
75+
withoutLigatures = strings.ReplaceAll(withoutLigatures, key, value)
76+
}
77+
}
78+
79+
withoutDiacritics, _, err := transform.String(transformer, withoutLigatures)
6380
if err != nil {
6481
return "", err
6582
}
6683

6784
withoutSpaces := strings.Replace(withoutDiacritics, " ", "_", -1)
68-
withoutSpecials := specialChars.ReplaceAllString(withoutSpaces, "")
85+
withoutQuotes := quotesChar.ReplaceAllString(withoutSpaces, "_")
86+
withoutSpecials := specialChars.ReplaceAllString(withoutQuotes, "")
6987

7088
sanitized := withoutSpecials
7189
if removeSlash {
7290
sanitized = strings.Replace(sanitized, "/", "_", -1)
7391
}
7492

75-
return sanitized, nil
93+
return strings.Replace(sanitized, "__", "_", -1), nil
7694
}
7795

7896
// SafeWrite writes content to writer with error handling

pkg/provider/utils_test.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package provider
22

33
import (
44
"errors"
5+
"io"
6+
"io/ioutil"
57
"reflect"
68
"testing"
79
)
@@ -30,9 +32,9 @@ func TestSanitizeName(t *testing.T) {
3032
},
3133
{
3234
"should replace diacritics and special chars",
33-
`Le terme "où", l'ouïe fine`,
35+
`L'Œil "où", l'ouïe fine au Ø`,
3436
true,
35-
"le_terme_ou_louie_fine",
37+
"l_oeil_ou_l_ouie_fine_au_oe",
3638
nil,
3739
},
3840
{
@@ -74,6 +76,32 @@ func TestSanitizeName(t *testing.T) {
7476
}
7577
}
7678

79+
func TestSafeWrite(t *testing.T) {
80+
type args struct {
81+
writer io.Writer
82+
content string
83+
}
84+
85+
var cases = []struct {
86+
intention string
87+
args args
88+
}{
89+
{
90+
"no panic",
91+
args{
92+
writer: ioutil.Discard,
93+
content: "test",
94+
},
95+
},
96+
}
97+
98+
for _, tc := range cases {
99+
t.Run(tc.intention, func(t *testing.T) {
100+
SafeWrite(tc.args.writer, tc.args.content)
101+
})
102+
}
103+
}
104+
77105
func TestIsNotExist(t *testing.T) {
78106
var cases = []struct {
79107
intention string

0 commit comments

Comments
 (0)