github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/span/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 span_test
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/jhump/golang-x-tools/internal/span"
    14  )
    15  
    16  // TestURI 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        span.URI
    24  	}{
    25  		{
    26  			path:     ``,
    27  			wantFile: ``,
    28  			wantURI:  span.URI(""),
    29  		},
    30  		{
    31  			path:     `C:/Windows/System32`,
    32  			wantFile: `C:/Windows/System32`,
    33  			wantURI:  span.URI("file:///C:/Windows/System32"),
    34  		},
    35  		{
    36  			path:     `C:/Go/src/bob.go`,
    37  			wantFile: `C:/Go/src/bob.go`,
    38  			wantURI:  span.URI("file:///C:/Go/src/bob.go"),
    39  		},
    40  		{
    41  			path:     `c:/Go/src/bob.go`,
    42  			wantFile: `C:/Go/src/bob.go`,
    43  			wantURI:  span.URI("file:///C:/Go/src/bob.go"),
    44  		},
    45  		{
    46  			path:     `/path/to/dir`,
    47  			wantFile: `/path/to/dir`,
    48  			wantURI:  span.URI("file:///path/to/dir"),
    49  		},
    50  		{
    51  			path:     `/a/b/c/src/bob.go`,
    52  			wantFile: `/a/b/c/src/bob.go`,
    53  			wantURI:  span.URI("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:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    59  		},
    60  	} {
    61  		got := span.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.Filename()
    66  		if gotFilename != test.wantFile {
    67  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
    68  		}
    69  	}
    70  }
    71  
    72  func TestURIFromURI(t *testing.T) {
    73  	for _, test := range []struct {
    74  		inputURI, wantFile string
    75  		wantURI            span.URI
    76  	}{
    77  		{
    78  			inputURI: `file:///c:/Go/src/bob%20george/george/george.go`,
    79  			wantFile: `C:/Go/src/bob george/george/george.go`,
    80  			wantURI:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    81  		},
    82  		{
    83  			inputURI: `file:///C%3A/Go/src/bob%20george/george/george.go`,
    84  			wantFile: `C:/Go/src/bob george/george/george.go`,
    85  			wantURI:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    86  		},
    87  		{
    88  			inputURI: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
    89  			wantFile: `/path/to/%p%ercent%/per%cent.go`,
    90  			wantURI:  span.URI(`file:///path/to/%25p%25ercent%25/per%25cent.go`),
    91  		},
    92  		{
    93  			inputURI: `file:///C%3A/`,
    94  			wantFile: `C:/`,
    95  			wantURI:  span.URI(`file:///C:/`),
    96  		},
    97  		{
    98  			inputURI: `file:///`,
    99  			wantFile: `/`,
   100  			wantURI:  span.URI(`file:///`),
   101  		},
   102  		{
   103  			inputURI: `file://wsl%24/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
   104  			wantFile: `/wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`,
   105  			wantURI:  span.URI(`file:///wsl$/Ubuntu/home/wdcui/repo/VMEnclaves/cvm-runtime`),
   106  		},
   107  	} {
   108  		got := span.URIFromURI(test.inputURI)
   109  		if got != test.wantURI {
   110  			t.Errorf("NewURI(%q): got %q, expected %q", test.inputURI, got, test.wantURI)
   111  		}
   112  		gotFilename := got.Filename()
   113  		if gotFilename != test.wantFile {
   114  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
   115  		}
   116  	}
   117  }