2014-02-27 18:32:09 -05:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-09-03 15:05:44 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-02-27 18:32:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPretty(t *testing.T) {
|
|
|
|
assert.Equal(t, PrettifyPath("/section/name.html"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyPath("/section/sub/name.html"), "/section/sub/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyPath("/section/name/"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyPath("/section/name/index.html"), "/section/name/index.html")
|
|
|
|
assert.Equal(t, PrettifyPath("/index.html"), "/index.html")
|
|
|
|
assert.Equal(t, PrettifyPath("/name.xml"), "/name/index.xml")
|
|
|
|
assert.Equal(t, PrettifyPath("/"), "/")
|
|
|
|
assert.Equal(t, PrettifyPath(""), "/")
|
|
|
|
assert.Equal(t, PrettifyUrl("/section/name.html"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyUrl("/section/sub/name.html"), "/section/sub/name")
|
|
|
|
assert.Equal(t, PrettifyUrl("/section/name/"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyUrl("/section/name/index.html"), "/section/name")
|
|
|
|
assert.Equal(t, PrettifyUrl("/index.html"), "/")
|
|
|
|
assert.Equal(t, PrettifyUrl("/name.xml"), "/name/index.xml")
|
|
|
|
assert.Equal(t, PrettifyUrl("/"), "/")
|
|
|
|
assert.Equal(t, PrettifyUrl(""), "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUgly(t *testing.T) {
|
|
|
|
assert.Equal(t, Uglify("/section/name.html"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/sub/name.html"), "/section/sub/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/name/"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/section/name/index.html"), "/section/name.html")
|
|
|
|
assert.Equal(t, Uglify("/index.html"), "/index.html")
|
|
|
|
assert.Equal(t, Uglify("/name.xml"), "/name.xml")
|
|
|
|
assert.Equal(t, Uglify("/"), "/")
|
|
|
|
assert.Equal(t, Uglify(""), "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakePath(t *testing.T) {
|
2014-08-21 19:01:34 -04:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" Foo bar ", "Foo-bar"},
|
|
|
|
{"Foo.Bar/foo_Bar-Foo", "Foo.Bar/foo_Bar-Foo"},
|
|
|
|
{"fOO,bar:foo%bAR", "fOObarfoobAR"},
|
|
|
|
{"FOo/BaR.html", "FOo/BaR.html"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
2014-09-03 15:05:44 -04:00
|
|
|
{"은행", "은행"},
|
|
|
|
{"Банковский кассир", "Банковский-кассир"},
|
2014-08-21 19:01:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
output := MakePath(test.input)
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeToLower(t *testing.T) {
|
2014-02-27 18:32:09 -05:00
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" foo bar ", "foo-bar"},
|
2014-09-03 15:05:44 -04:00
|
|
|
{" Foo Bar ", "foo-bar"},
|
2014-02-27 18:32:09 -05:00
|
|
|
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
|
|
|
{"foo,bar:foo%bar", "foobarfoobar"},
|
|
|
|
{"foo/bar.html", "foo/bar.html"},
|
|
|
|
{"трям/трям", "трям/трям"},
|
2014-09-03 15:05:44 -04:00
|
|
|
{"은행", "은행"},
|
2014-02-27 18:32:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2014-09-03 15:05:44 -04:00
|
|
|
output := MakePathToLower(test.input)
|
2014-02-27 18:32:09 -05:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUrlize(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{" foo bar ", "foo-bar"},
|
2014-09-03 15:05:44 -04:00
|
|
|
{"Foo And BAR", "foo-and-bar"},
|
2014-02-27 18:32:09 -05:00
|
|
|
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
|
|
|
{"foo,bar:foo%bar", "foobarfoobar"},
|
|
|
|
{"foo/bar.html", "foo/bar.html"},
|
|
|
|
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
output := Urlize(test.input)
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 07:59:59 -04:00
|
|
|
|
|
|
|
func TestMakePermalink(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
host, link, output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{"http://abc.com/foo", "post/bar", "http://abc.com/foo/post/bar"},
|
|
|
|
{"http://abc.com/foo/", "post/bar", "http://abc.com/foo/post/bar"},
|
|
|
|
{"http://abc.com", "post/bar", "http://abc.com/post/bar"},
|
|
|
|
{"http://abc.com", "bar", "http://abc.com/bar"},
|
|
|
|
{"http://abc.com/foo/bar", "post/bar", "http://abc.com/foo/bar/post/bar"},
|
|
|
|
{"http://abc.com/foo/bar", "post/bar/", "http://abc.com/foo/bar/post/bar/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
|
|
|
output := MakePermalink(d.host, d.link).String()
|
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 06:21:22 -04:00
|
|
|
|
|
|
|
func TestMakePathRelative(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
inPath, path1, path2, output string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []test{
|
|
|
|
{"/abc/bcd/ab.css", "/abc/bcd", "/bbc/bcd", "/ab.css"},
|
|
|
|
{"/abc/bcd/ab.css", "/abcd/bcd", "/abc/bcd", "/ab.css"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range data {
|
|
|
|
output, _ := MakePathRelative(d.inPath, d.path1, d.path2)
|
|
|
|
if d.output != output {
|
|
|
|
t.Errorf("Test #%d failed. Expected %q got %q", i, d.output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, error := MakePathRelative("a/b/c.ss", "/a/c", "/d/c", "/e/f")
|
|
|
|
|
|
|
|
if error == nil {
|
|
|
|
t.Errorf("Test #%d failed. Expected error")
|
|
|
|
}
|
|
|
|
}
|