github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/span/uri_windows_test.go (about)

     1  // Copyright 2020 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/powerman/golang-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: `C:\path\to\dir`,
    48  			wantURI:  span.URI("file:///C:/path/to/dir"),
    49  		},
    50  		{
    51  			path:     `\a\b\c\src\bob.go`,
    52  			wantFile: `C:\a\b\c\src\bob.go`,
    53  			wantURI:  span.URI("file:///C:/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:///c:/path/to/%25p%25ercent%25/per%25cent.go`,
    89  			wantFile: `C:\path\to\%p%ercent%\per%cent.go`,
    90  			wantURI:  span.URI(`file:///C:/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  		got := span.URIFromURI(test.inputURI)
   104  		if got != test.wantURI {
   105  			t.Errorf("NewURI(%q): got %q, expected %q", test.inputURI, got, test.wantURI)
   106  		}
   107  		gotFilename := got.Filename()
   108  		if gotFilename != test.wantFile {
   109  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
   110  		}
   111  	}
   112  }