1
0
Fork 0
mirror of https://github.com/laravel-ls/uri synced 2026-06-16 01:54:57 +02:00

uri: add more testcases

This commit is contained in:
Koichi Shiraishi 2019-06-05 12:10:11 +09:00
parent 2f8f142487
commit 979cfefb51
No known key found for this signature in database
GPG key ID: A71DFD3B4DA7A79B

View file

@ -15,19 +15,49 @@ import (
func TestParse(t *testing.T) {
tests := []struct {
name string
s string
want *uri.URI
name string
s string
want *uri.URI
wantErr bool
}{
{
name: "Valid",
s: "https://code.visualstudio.com/docs/extensions/overview#frag",
name: "ValidFileScheme",
s: "file://code.visualstudio.com/docs/extensions/overview.md",
want: &uri.URI{
Scheme: "https",
Scheme: uri.FileScheme,
Authority: "code.visualstudio.com",
Path: "/docs/extensions/overview.md",
FsPath: filepath.FromSlash("/docs/extensions/overview.md"),
},
wantErr: false,
},
{
name: "ValidHTTPScheme",
s: "http://code.visualstudio.com/docs/extensions/overview#frag",
want: &uri.URI{
Scheme: uri.HTTPScheme,
Authority: "code.visualstudio.com",
Path: "/docs/extensions/overview",
Fragment: "frag",
},
wantErr: false,
},
{
name: "ValidHTTPSScheme",
s: "https://code.visualstudio.com/docs/extensions/overview#frag",
want: &uri.URI{
Scheme: uri.HTTPSScheme,
Authority: "code.visualstudio.com",
Path: "/docs/extensions/overview",
Fragment: "frag",
},
wantErr: false,
},
{
name: "Invalid",
s: "foo://user@example.com:8042/over/there?name=ferret#nose",
want: new(uri.URI),
wantErr: true,
},
}
for _, tt := range tests {
@ -35,7 +65,7 @@ func TestParse(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if diff := cmp.Diff(uri.Parse(tt.s), tt.want); diff != "" {
if diff := cmp.Diff(uri.Parse(tt.s), tt.want, cmp.AllowUnexported(*tt.want)); (diff != "") != tt.wantErr {
t.Errorf("%s: (-got, +want)\n%s", tt.name, diff)
}
})
@ -44,18 +74,104 @@ func TestParse(t *testing.T) {
func TestFile(t *testing.T) {
tests := []struct {
name string
path string
want *uri.URI
name string
path string
want *uri.URI
wantErr bool
}{
{
name: "Valid",
name: "ValidFileScheme",
path: "/users/me/c#-projects/",
want: &uri.URI{
Scheme: uri.FileScheme,
Path: "/users/me/c#-projects/",
FsPath: filepath.FromSlash("/users/me/c#-projects/"),
},
wantErr: false,
},
{
name: "Invalid",
path: "users-me-c#-projects",
want: new(uri.URI),
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if diff := cmp.Diff(uri.File(tt.path), tt.want, cmp.AllowUnexported(*tt.want)); (diff != "") != tt.wantErr {
t.Errorf("%s: (-got, +want)\n%s", tt.name, diff)
}
})
}
}
func TestFrom(t *testing.T) {
type args struct {
scheme string
authority string
path string
query string
fragment string
}
tests := []struct {
name string
args args
want *uri.URI
}{
{
name: "ValidFileScheme",
args: args{
scheme: "file",
authority: "example.com",
path: "/over/there",
query: "name=ferret",
fragment: "nose",
},
want: &uri.URI{
Scheme: uri.FileScheme,
Authority: "example.com",
Path: "/over/there",
FsPath: filepath.FromSlash("/over/there"),
Query: "name=ferret",
Fragment: "nose",
},
},
{
name: "ValidHTTPScheme",
args: args{
scheme: "http",
authority: "example.com:8042",
path: "/over/there",
query: "name=ferret",
fragment: "nose",
},
want: &uri.URI{
Scheme: uri.HTTPScheme,
Authority: "example.com:8042",
Path: "/over/there",
Query: "name=ferret",
Fragment: "nose",
},
},
{
name: "ValidHTTPSScheme",
args: args{
scheme: "https",
authority: "example.com:8042",
path: "/over/there",
query: "name=ferret",
fragment: "nose",
},
want: &uri.URI{
Scheme: uri.HTTPSScheme,
Authority: "example.com:8042",
Path: "/over/there",
Query: "name=ferret",
Fragment: "nose",
},
},
}
for _, tt := range tests {
@ -63,9 +179,82 @@ func TestFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if diff := cmp.Diff(uri.File(tt.path), tt.want); diff != "" {
if diff := cmp.Diff(uri.From(tt.args.scheme, tt.args.authority, tt.args.path, tt.args.query, tt.args.fragment), tt.want, cmp.AllowUnexported(*tt.want)); diff != "" {
t.Errorf("%s: (-got, +want)\n%s", tt.name, diff)
}
})
}
}
func TestURI_String(t *testing.T) {
type fields struct {
Authority string
Fragment string
FsPath string
Path string
Query string
Scheme string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "ValidFileScheme",
fields: fields{
Authority: "code.visualstudio.com",
Path: "/docs/extensions/overview.md",
FsPath: filepath.FromSlash("/docs/extensions/overview.md"),
Scheme: string(uri.FileScheme),
},
want: "file://code.visualstudio.com/docs/extensions/overview.md",
},
{
name: "ValidHTTPScheme",
fields: fields{
Authority: "code.visualstudio.com",
Fragment: "frag",
Path: "/docs/extensions/overview",
FsPath: filepath.FromSlash("/docs/extensions/overview"),
Query: "test",
Scheme: string(uri.HTTPScheme),
},
want: "http://code.visualstudio.com/docs/extensions/overview?test#frag",
},
{
name: "ValidHTTPSScheme",
fields: fields{
Authority: "code.visualstudio.com",
Fragment: "frag",
Path: "/docs/extensions/overview",
FsPath: filepath.FromSlash("/docs/extensions/overview"),
Query: "test",
Scheme: string(uri.HTTPSScheme),
},
want: "https://code.visualstudio.com/docs/extensions/overview?test#frag",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
u := &uri.URI{
Authority: tt.fields.Authority,
Fragment: tt.fields.Fragment,
FsPath: tt.fields.FsPath,
Path: tt.fields.Path,
Query: tt.fields.Query,
Scheme: tt.fields.Scheme,
}
if got := u.String(); !cmp.Equal(got, tt.want) {
t.Errorf("URI.String() = %v, want %v", got, tt.want)
}
if got2 := u.String(); !cmp.Equal(got2, tt.want) { // cache with u.formatted
t.Errorf("URI.String() = %v, want %v", got2, tt.want)
}
})
}
}