Files
golink/golink_test.go
T
Will NorrisandWill Norris 113923a9af all: librarify golink and make importable
Export LastSnapshot var so that snapshots can be stored in separate repo
from the golink code.
2022-08-20 07:25:26 -07:00

81 lines
2.2 KiB
Go

package golink
import (
"testing"
"time"
)
func TestExpandLink(t *testing.T) {
tests := []struct {
name string
long string
now time.Time
remainder string
want string
}{
{
name: "dont-mangle-escapes",
long: "http://host.com/foo%2f/bar",
want: "http://host.com/foo%2f/bar",
},
{
name: "dont-mangle-escapes-and-remainder",
long: "http://host.com/foo%2f/bar",
remainder: "extra",
want: "http://host.com/foo%2f/bar/extra",
},
{
name: "remainder-insert-slash",
long: "http://host.com/foo",
remainder: "extra",
want: "http://host.com/foo/extra",
},
{
name: "remainder-long-as-trailing-slash",
long: "http://host.com/foo/",
remainder: "extra",
want: "http://host.com/foo/extra",
},
{
name: "var-expansions-time",
long: `https://roamresearch.com/#/app/ts-corp/page/{{.Now.Format "01-02-2006"}}`,
want: "https://roamresearch.com/#/app/ts-corp/page/06-02-2022",
now: time.Date(2022, 06, 02, 1, 2, 3, 4, time.UTC),
},
{
name: "template-no-path",
long: "https://calendar.google.com/{{with .Path}}calendar/embed?mode=week&src={{.}}@tailscale.com{{end}}",
want: "https://calendar.google.com/",
},
{
name: "template-with-path",
long: "https://calendar.google.com/{{with .Path}}calendar/embed?mode=week&src={{.}}@tailscale.com{{end}}",
remainder: "amelie",
want: "https://calendar.google.com/calendar/embed?mode=week&src=amelie@tailscale.com",
},
{
name: "template-with-pathescape-func",
long: "http://host.com/{{QueryEscape .Path}}",
remainder: "a/b",
want: "http://host.com/a%2Fb",
},
{
name: "template-with-queryescape-func",
long: "http://host.com/{{QueryEscape .Path}}",
remainder: "a+b",
want: "http://host.com/a%2Bb",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := expandLink(tt.long, expandEnv{Now: tt.now, Path: tt.remainder})
if err != nil {
t.Fatalf("expandLink(%q): %v", tt.long, err)
}
if got != tt.want {
t.Errorf("expandLink(%q) = %q; want %q", tt.long, got, tt.want)
}
})
}
}