2017-07-09 22:17:20 +02:00
|
|
|
package utils
|
2017-06-11 22:00:03 +02:00
|
|
|
|
|
|
|
import (
|
2017-07-03 15:37:14 +02:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2017-06-11 22:00:03 +02:00
|
|
|
"testing"
|
|
|
|
|
2017-06-16 22:47:49 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-11 22:00:03 +02:00
|
|
|
)
|
|
|
|
|
2017-07-03 15:37:14 +02:00
|
|
|
func TestLeftPad(t *testing.T) {
|
|
|
|
require.Equal(t, "abc", LeftPad("abc", 0))
|
|
|
|
require.Equal(t, "abc", LeftPad("abc", 3))
|
|
|
|
require.Equal(t, " abc", LeftPad("abc", 6))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSafeReplaceAllStringFunc(t *testing.T) {
|
|
|
|
re := regexp.MustCompile(`\w+`)
|
|
|
|
out, err := SafeReplaceAllStringFunc(re, "1 > 0", func(m string) (string, error) {
|
|
|
|
return fmt.Sprint(m == "1"), nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "true > false", out)
|
|
|
|
|
2017-07-03 19:03:45 +02:00
|
|
|
_, err = SafeReplaceAllStringFunc(re, "1 > 0", func(m string) (string, error) {
|
2017-07-03 15:37:14 +02:00
|
|
|
return "", fmt.Errorf("an expected error")
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "an expected error", err.Error())
|
|
|
|
}
|
|
|
|
|
2017-06-16 01:49:04 +02:00
|
|
|
func TestSlugify(t *testing.T) {
|
2017-06-16 22:47:49 +02:00
|
|
|
require.Equal(t, "abc", Slugify("abc"))
|
|
|
|
require.Equal(t, "ab-c", Slugify("ab.c"))
|
|
|
|
require.Equal(t, "ab-c", Slugify("ab-c"))
|
|
|
|
require.Equal(t, "ab-c", Slugify("ab()[]c"))
|
|
|
|
require.Equal(t, "ab123-cde-f-g", Slugify("ab123(cde)[]f.g"))
|
2017-06-16 01:49:04 +02:00
|
|
|
}
|
2017-06-11 22:00:03 +02:00
|
|
|
|
|
|
|
func TestStringArrayToMap(t *testing.T) {
|
|
|
|
input := []string{"a", "b", "c"}
|
|
|
|
expected := map[string]bool{"a": true, "b": true, "c": true}
|
2017-06-17 02:06:55 +02:00
|
|
|
actual := StringArrayToMap(input)
|
2017-06-16 22:47:49 +02:00
|
|
|
require.Equal(t, expected, actual)
|
2017-06-11 22:00:03 +02:00
|
|
|
}
|