mirror of
https://github.com/laravel-ls/uri
synced 2026-07-05 00:43:41 +02:00
uri: add more testcases
This commit is contained in:
parent
2f8f142487
commit
979cfefb51
1 changed files with 201 additions and 12 deletions
201
uri_test.go
201
uri_test.go
|
|
@ -18,16 +18,46 @@ func TestParse(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
s string
|
s string
|
||||||
want *uri.URI
|
want *uri.URI
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid",
|
name: "ValidFileScheme",
|
||||||
s: "https://code.visualstudio.com/docs/extensions/overview#frag",
|
s: "file://code.visualstudio.com/docs/extensions/overview.md",
|
||||||
want: &uri.URI{
|
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",
|
Authority: "code.visualstudio.com",
|
||||||
Path: "/docs/extensions/overview",
|
Path: "/docs/extensions/overview",
|
||||||
Fragment: "frag",
|
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 {
|
for _, tt := range tests {
|
||||||
|
|
@ -35,7 +65,7 @@ func TestParse(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
t.Errorf("%s: (-got, +want)\n%s", tt.name, diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -47,15 +77,101 @@ func TestFile(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
path string
|
path string
|
||||||
want *uri.URI
|
want *uri.URI
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Valid",
|
name: "ValidFileScheme",
|
||||||
path: "/users/me/c#-projects/",
|
path: "/users/me/c#-projects/",
|
||||||
want: &uri.URI{
|
want: &uri.URI{
|
||||||
Scheme: uri.FileScheme,
|
Scheme: uri.FileScheme,
|
||||||
Path: "/users/me/c#-projects/",
|
Path: "/users/me/c#-projects/",
|
||||||
FsPath: filepath.FromSlash("/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 {
|
for _, tt := range tests {
|
||||||
|
|
@ -63,9 +179,82 @@ func TestFile(t *testing.T) {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
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)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue