Skip to content

Commit

Permalink
test: Increasing provider package coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh committed Dec 21, 2020
1 parent 4d43eb0 commit f36f3c0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 10 deletions.
69 changes: 69 additions & 0 deletions pkg/provider/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"net/http"
"reflect"
"testing"

Expand Down Expand Up @@ -101,6 +102,48 @@ func TestGetURI(t *testing.T) {
}
}

func TestLayoutPath(t *testing.T) {
type args struct {
path string
}

var cases = []struct {
intention string
instance Request
args args
want string
}{
{
"empty list",
Request{},
args{
path: "/reports",
},
"grid",
},
{
"empty list",
Request{
Preferences: Preferences{
ListLayoutPath: []string{"/sheets", "/reports"},
},
},
args{
path: "/reports",
},
"list",
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
if got := tc.instance.LayoutPath(tc.args.path); got != tc.want {
t.Errorf("LayoutPath() = `%s`, want `%s`", got, tc.want)
}
})
}
}

func TestCheckPassword(t *testing.T) {
password, err := bcrypt.GenerateFromPassword([]byte("test"), bcrypt.DefaultCost)
if err != nil {
Expand Down Expand Up @@ -215,6 +258,32 @@ func TestNewError(t *testing.T) {
}
}

func TestError(t *testing.T) {
type args struct {
content string
}

var cases = []struct {
intention string
instance *Error
want string
}{
{
"simple",
NewError(http.StatusTeapot, errors.New("I'm a teapot")),
"HTTP/418: I'm a teapot",
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
if got := tc.instance.Error(); got != tc.want {
t.Errorf("Error() = `%s`, want `%s`", got, tc.want)
}
})
}
}

func TestExtension(t *testing.T) {
var cases = []struct {
intention string
Expand Down
34 changes: 26 additions & 8 deletions pkg/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ import (
)

var (
transformer transform.Transformer
transformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
transliterations = map[string]string{
"Ð": "D",
"Ł": "l",
"Ø": "oe",
"Þ": "Th",
"ß": "ss",
"æ": "ae",
"ð": "d",
"ł": "l",
"ø": "oe",
"þ": "th",
"œ": "oe",
}
quotesChar = regexp.MustCompile(`["'` + "`" + `](?m)`)
specialChars = regexp.MustCompile(`[^a-z0-9.\-_/](?m)`)
)

func init() {
transformer = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
}

// GetPathname computes pathname for given params
func GetPathname(folder, name string, share *Share) string {
parts := make([]string, 0)
Expand Down Expand Up @@ -59,20 +69,28 @@ func GetURI(folder, name string, share *Share) string {

// SanitizeName return sanitized name (remove diacritics)
func SanitizeName(name string, removeSlash bool) (string, error) {
withoutDiacritics, _, err := transform.String(transformer, strings.ToLower(name))
withoutLigatures := strings.ToLower(name)
for key, value := range transliterations {
if strings.Contains(withoutLigatures, key) {
withoutLigatures = strings.ReplaceAll(withoutLigatures, key, value)
}
}

withoutDiacritics, _, err := transform.String(transformer, withoutLigatures)
if err != nil {
return "", err
}

withoutSpaces := strings.Replace(withoutDiacritics, " ", "_", -1)
withoutSpecials := specialChars.ReplaceAllString(withoutSpaces, "")
withoutQuotes := quotesChar.ReplaceAllString(withoutSpaces, "_")
withoutSpecials := specialChars.ReplaceAllString(withoutQuotes, "")

sanitized := withoutSpecials
if removeSlash {
sanitized = strings.Replace(sanitized, "/", "_", -1)
}

return sanitized, nil
return strings.Replace(sanitized, "__", "_", -1), nil
}

// SafeWrite writes content to writer with error handling
Expand Down
32 changes: 30 additions & 2 deletions pkg/provider/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package provider

import (
"errors"
"io"
"io/ioutil"
"reflect"
"testing"
)
Expand Down Expand Up @@ -30,9 +32,9 @@ func TestSanitizeName(t *testing.T) {
},
{
"should replace diacritics and special chars",
`Le terme "où", l'ouïe fine`,
`L'Œil "où", l'ouïe fine au Ø`,
true,
"le_terme_ou_louie_fine",
"l_oeil_ou_l_ouie_fine_au_oe",
nil,
},
{
Expand Down Expand Up @@ -74,6 +76,32 @@ func TestSanitizeName(t *testing.T) {
}
}

func TestSafeWrite(t *testing.T) {
type args struct {
writer io.Writer
content string
}

var cases = []struct {
intention string
args args
}{
{
"no panic",
args{
writer: ioutil.Discard,
content: "test",
},
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
SafeWrite(tc.args.writer, tc.args.content)
})
}
}

func TestIsNotExist(t *testing.T) {
var cases = []struct {
intention string
Expand Down

0 comments on commit f36f3c0

Please sign in to comment.