76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package templer
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestRelativePath(t *testing.T) {
|
|
tests := []struct {
|
|
name string // description of this test case
|
|
// Named input parameters for target function.
|
|
base string
|
|
relPath string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
// TODO: Add test cases.
|
|
{
|
|
name: "with no relative path stays the same",
|
|
base: "/foo/bar",
|
|
relPath: "",
|
|
want: "/foo/bar",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "with . relative path stays the same",
|
|
base: "/foo/bar",
|
|
relPath: ".",
|
|
want: "/foo/bar",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "with ./ relative path stays the same",
|
|
base: "/foo/bar",
|
|
relPath: "./",
|
|
want: "/foo/bar",
|
|
wantErr: false,
|
|
},
|
|
|
|
{
|
|
name: "with .. relative path goes one up",
|
|
base: "/foo/bar",
|
|
relPath: "..",
|
|
want: "/foo",
|
|
wantErr: false,
|
|
},
|
|
|
|
{
|
|
name: "with ../baz relative path replaces last element with baz",
|
|
base: "/foo/bar",
|
|
relPath: "../baz",
|
|
want: "/foo/baz",
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ctx := context.WithValue(t.Context(), pathKey, tt.base)
|
|
got, gotErr := RelativePath(ctx, tt.relPath)
|
|
if gotErr != nil {
|
|
if !tt.wantErr {
|
|
t.Errorf("RelativePath() failed: %v", gotErr)
|
|
}
|
|
return
|
|
}
|
|
if tt.wantErr {
|
|
t.Fatal("RelativePath() succeeded unexpectedly")
|
|
}
|
|
// TODO: update the condition below to compare got with tt.want.
|
|
if got != tt.want {
|
|
t.Errorf("RelativePath() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|