golang.org/x/tools/gopls@v0.15.3/internal/protocol/uri_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !windows
     6  // +build !windows
     7  
     8  package protocol_test
     9  
    10  import (
    11  	"testing"
    12  
    13  	"golang.org/x/tools/gopls/internal/protocol"
    14  )
    15  
    16  // TestURIFromPath tests the conversion between URIs and filenames. The test cases
    17  // include Windows-style URIs and filepaths, but we avoid having OS-specific
    18  // tests by using only forward slashes, assuming that the standard library
    19  // functions filepath.ToSlash and filepath.FromSlash do not need testing.
    20  func TestURIFromPath(t *testing.T) {
    21  	for _, test := range []struct {
    22  		path, wantFile string
    23  		wantURI        protocol.DocumentURI
    24  	}{
    25  		{
    26  			path:     ``,
    27  			wantFile: ``,
    28  			wantURI:  protocol.DocumentURI(""),
    29  		},
    30  		{
    31  			path:     `C:/Windows/System32`,
    32  			wantFile: `C:/Windows/System32`,
    33  			wantURI:  protocol.DocumentURI("file:///C:/Windows/System32"),
    34  		},
    35  		{
    36  			path:     `C:/Go/src/bob.go`,
    37  			wantFile: `C:/Go/src/bob.go`,
    38  			wantURI:  protocol.DocumentURI("file:///C:/Go/src/bob.go"),
    39  		},
    40  		{
    41  			path:     `c:/Go/src/bob.go`,
    42  			wantFile: `C:/Go/src/bob.go`,
    43  			wantURI:  protocol.DocumentURI("file:///C:/Go/src/bob.go"),
    44  		},
    45  		{
    46  			path:     `/path/to/dir`,
    47  			wantFile: `/path/to/dir`,
    48  			wantURI:  protocol.DocumentURI("file:///path/to/dir"),
    49  		},
    50  		{
    51  			path:     `/a/b/c/src/bob.go`,
    52  			wantFile: `/a/b/c/src/bob.go`,
    53  			wantURI:  protocol.DocumentURI("file:///a/b/c/src/bob.go"),
    54  		},
    55  		{
    56  			path:     `c:/Go/src/bob george/george/george.go`,
    57  			wantFile: `C:/Go/src/bob george/george/george.go`,
    58  			wantURI:  protocol.DocumentURI("file:///C:/Go/src/bob%20george/george/george.go"),
    59  		},
    60  	} {
    61  		got := protocol.URIFromPath(test.path)
    62  		if got != test.wantURI {
    63  			t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
    64  		}
    65  		gotFilename := got.Path()
    66  		if gotFilename != test.wantFile {
    67  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
    68  		}
    69  	}
    70  }
    71  
    72  func TestParseDocumentURI(t *testing.T) {
    73  	for _, test := range []struct {
    74  		input    string
    75  		want     string // string(DocumentURI) on success or error.Error() on failure
    76  		wantPath string // expected DocumentURI.Path on success
    77  	}{
    78  		{
    79  			input:    `file:///c:/Go/src/bob%20george/george/george.go`,
    80  			want:     "file:///C:/Go/src/bob%20george/george/george.go",
    81  			wantPath: `C:/Go/src/bob george/george/george.go`,
    82  		},
    83  		{
    84  			input:    `file:///C%3A/Go/src/bob%20george/george/george.go`,
    85  			want:     "file:///C:/Go/src/bob%20george/george/george.go",
    86  			wantPath: `C:/Go/src/bob george/george/george.go`,
    87  		},
    88  		{
    89  			input:    `file:///path/to/%25p%25ercent%25/per%25cent.go`,
    90  			want:     `file:///path/to/%25p%25ercent%25/per%25cent.go`,
    91  			wantPath: `/path/to/%p%ercent%/per%cent.go`,
    92  		},
    93  		{
    94  			input:    `file:///C%3A/`,
    95  			want:     `file:///C:/`,
    96  			wantPath: `C:/`,
    97  		},
    98  		{
    99  			input:    `file:///`,
   100  			want:     `file:///`,
   101  			wantPath: `/`,
   102  		},
   103  		{
   104  			input:    `file://wsl%24/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
   105  			want:     `file:///wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
   106  			wantPath: `/wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
   107  		},
   108  		{
   109  			input:    "",
   110  			want:     "",
   111  			wantPath: "",
   112  		},
   113  		// Errors:
   114  		{
   115  			input: "https://go.dev/",
   116  			want:  "DocumentURI scheme is not 'file': https://go.dev/",
   117  		},
   118  	} {
   119  		uri, err := protocol.ParseDocumentURI(test.input)
   120  		var got string
   121  		if err != nil {
   122  			got = err.Error()
   123  		} else {
   124  			got = string(uri)
   125  		}
   126  		if got != test.want {
   127  			t.Errorf("ParseDocumentURI(%q): got %q, want %q", test.input, got, test.want)
   128  		}
   129  		if err == nil && uri.Path() != test.wantPath {
   130  			t.Errorf("DocumentURI(%s).Path = %q, want %q", uri,
   131  				uri.Path(), test.wantPath)
   132  		}
   133  	}
   134  }