2016-06-03 08:06:54 -05:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-08-09 03:36:41 -05:00
|
|
|
"time"
|
2016-06-03 08:06:54 -05:00
|
|
|
|
2020-08-13 04:10:48 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-06-03 08:06:54 -05:00
|
|
|
)
|
|
|
|
|
2020-08-13 04:10:48 -05:00
|
|
|
func TestStringsFallback2(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
val1 string
|
|
|
|
val2 string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// testing every scenario
|
|
|
|
{"", "", ""},
|
|
|
|
{"1", "", "1"},
|
|
|
|
{"1", "2", "1"},
|
|
|
|
{"", "2", "2"},
|
|
|
|
}
|
|
|
|
for _, testcase := range tests {
|
|
|
|
assert.EqualValues(t, testcase.expected, StringsFallback2(testcase.val1, testcase.val2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringsFallback3(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
val1 string
|
|
|
|
val2 string
|
|
|
|
val3 string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"", "", "", ""},
|
|
|
|
{"1", "", "", "1"},
|
|
|
|
{"1", "2", "", "1"},
|
|
|
|
{"1", "2", "3", "1"},
|
|
|
|
{"", "2", "", "2"},
|
|
|
|
{"", "2", "3", "2"},
|
|
|
|
{"", "", "3", "3"},
|
|
|
|
}
|
|
|
|
for _, testcase := range tests {
|
|
|
|
assert.EqualValues(t, testcase.expected, StringsFallback3(testcase.val1, testcase.val2, testcase.val3))
|
|
|
|
}
|
2016-06-03 08:06:54 -05:00
|
|
|
}
|
2017-04-25 02:14:29 -05:00
|
|
|
|
|
|
|
func TestSplitString(t *testing.T) {
|
2020-08-13 04:10:48 -05:00
|
|
|
tests := map[string][]string{
|
|
|
|
"": {},
|
|
|
|
"test": {"test"},
|
|
|
|
"test1 test2 test3": {"test1", "test2", "test3"},
|
|
|
|
"test1,test2,test3": {"test1", "test2", "test3"},
|
|
|
|
"test1, test2, test3": {"test1", "test2", "test3"},
|
|
|
|
"test1 , test2 test3": {"test1", "test2", "test3"},
|
|
|
|
}
|
|
|
|
for input, expected := range tests {
|
|
|
|
assert.EqualValues(t, expected, SplitString(input))
|
|
|
|
}
|
2017-04-25 02:14:29 -05:00
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
|
|
|
|
func TestDateAge(t *testing.T) {
|
2020-08-13 04:10:48 -05:00
|
|
|
assert.Equal(t, "?", GetAgeString(time.Time{})) // base case
|
|
|
|
|
|
|
|
tests := map[time.Duration]string{
|
2021-09-01 08:53:58 -05:00
|
|
|
-1 * time.Hour: "< 1 minute", // one hour in the future
|
|
|
|
0: "< 1 minute",
|
|
|
|
2 * time.Second: "< 1 minute",
|
|
|
|
2 * time.Minute: "2 minutes",
|
|
|
|
2 * time.Hour: "2 hours",
|
|
|
|
3 * 24 * time.Hour: "3 days",
|
|
|
|
67 * 24 * time.Hour: "2 months",
|
|
|
|
409 * 24 * time.Hour: "1 year",
|
2020-08-13 04:10:48 -05:00
|
|
|
}
|
|
|
|
for elapsed, expected := range tests {
|
|
|
|
assert.Equalf(
|
|
|
|
t,
|
|
|
|
expected,
|
|
|
|
GetAgeString(time.Now().Add(-elapsed)),
|
|
|
|
"duration '%s'",
|
|
|
|
elapsed.String(),
|
|
|
|
)
|
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
}
|
2019-05-27 03:47:21 -05:00
|
|
|
|
|
|
|
func TestToCamelCase(t *testing.T) {
|
2020-08-13 04:10:48 -05:00
|
|
|
tests := map[string]string{
|
|
|
|
"kebab-case-string": "kebabCaseString",
|
|
|
|
"snake_case_string": "snakeCaseString",
|
|
|
|
"mixed-case_string": "mixedCaseString",
|
|
|
|
"alreadyCamelCase": "alreadyCamelCase",
|
|
|
|
}
|
|
|
|
for input, expected := range tests {
|
|
|
|
assert.Equal(t, expected, ToCamelCase(input))
|
|
|
|
}
|
2019-05-27 03:47:21 -05:00
|
|
|
}
|