2017-07-09 16:17:20 -04:00
|
|
|
package utils
|
2017-06-23 09:32:08 -04:00
|
|
|
|
|
|
|
import (
|
2017-08-20 13:34:47 -04:00
|
|
|
"os"
|
2017-07-03 11:48:06 -04:00
|
|
|
"strings"
|
2017-06-23 09:32:08 -04:00
|
|
|
"testing"
|
2017-07-03 10:39:55 -04:00
|
|
|
"time"
|
2017-06-23 09:32:08 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-07-03 10:39:55 -04:00
|
|
|
func timeMustParse(s string) time.Time {
|
|
|
|
t, err := time.Parse(time.RFC3339, s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-07-03 11:48:06 -04:00
|
|
|
func TestMustAbs(t *testing.T) {
|
|
|
|
require.True(t, strings.HasPrefix(MustAbs("."), "/"))
|
|
|
|
}
|
|
|
|
|
2017-08-20 12:22:58 -04:00
|
|
|
func TestParseFilenameDate(t *testing.T) {
|
2017-08-20 13:34:47 -04:00
|
|
|
os.Setenv("TZ", "America/New_York") // nolint: errcheck
|
2017-08-20 12:22:58 -04:00
|
|
|
d, title, found := ParseFilenameDateTitle("2017-07-02-post.html")
|
2017-07-03 10:39:55 -04:00
|
|
|
require.True(t, found)
|
2017-08-20 12:22:58 -04:00
|
|
|
require.Equal(t, "Post", title)
|
2017-08-20 12:12:06 -04:00
|
|
|
require.Equal(t, timeMustParse("2017-07-02T00:00:00-04:00"), d)
|
2017-07-03 10:39:55 -04:00
|
|
|
|
2017-08-20 12:22:58 -04:00
|
|
|
_, _, found = ParseFilenameDateTitle("not-post.html")
|
2017-07-03 10:39:55 -04:00
|
|
|
require.False(t, found)
|
|
|
|
}
|
|
|
|
|
2017-06-23 09:32:08 -04:00
|
|
|
func TestTrimExt(t *testing.T) {
|
|
|
|
require.Equal(t, "/a/b", TrimExt("/a/b.c"))
|
|
|
|
require.Equal(t, "/a/b", TrimExt("/a/b"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestURLPathClean(t *testing.T) {
|
|
|
|
require.Equal(t, "/a/b", URLPathClean("/a/b"))
|
|
|
|
require.Equal(t, "/a/b/", URLPathClean("/a/b/"))
|
|
|
|
require.Equal(t, "/a/b", URLPathClean("/a//b"))
|
|
|
|
require.Equal(t, "/b", URLPathClean("/a/../b"))
|
|
|
|
require.Equal(t, "/", URLPathClean("/"))
|
|
|
|
}
|