github.com/jd-ly/tools@v0.5.7/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  // +build windows
     6  
     7  package span_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/jd-ly/tools/internal/span"
    13  )
    14  
    15  // TestURI tests the conversion between URIs and filenames. The test cases
    16  // include Windows-style URIs and filepaths, but we avoid having OS-specific
    17  // tests by using only forward slashes, assuming that the standard library
    18  // functions filepath.ToSlash and filepath.FromSlash do not need testing.
    19  func TestURIFromPath(t *testing.T) {
    20  	for _, test := range []struct {
    21  		path, wantFile string
    22  		wantURI        span.URI
    23  	}{
    24  		{
    25  			path:     ``,
    26  			wantFile: ``,
    27  			wantURI:  span.URI(""),
    28  		},
    29  		{
    30  			path:     `C:\Windows\System32`,
    31  			wantFile: `C:\Windows\System32`,
    32  			wantURI:  span.URI("file:///C:/Windows/System32"),
    33  		},
    34  		{
    35  			path:     `C:\Go\src\bob.go`,
    36  			wantFile: `C:\Go\src\bob.go`,
    37  			wantURI:  span.URI("file:///C:/Go/src/bob.go"),
    38  		},
    39  		{
    40  			path:     `c:\Go\src\bob.go`,
    41  			wantFile: `C:\Go\src\bob.go`,
    42  			wantURI:  span.URI("file:///C:/Go/src/bob.go"),
    43  		},
    44  		{
    45  			path:     `\path\to\dir`,
    46  			wantFile: `C:\path\to\dir`,
    47  			wantURI:  span.URI("file:///C:/path/to/dir"),
    48  		},
    49  		{
    50  			path:     `\a\b\c\src\bob.go`,
    51  			wantFile: `C:\a\b\c\src\bob.go`,
    52  			wantURI:  span.URI("file:///C:/a/b/c/src/bob.go"),
    53  		},
    54  		{
    55  			path:     `c:\Go\src\bob george\george\george.go`,
    56  			wantFile: `C:\Go\src\bob george\george\george.go`,
    57  			wantURI:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    58  		},
    59  	} {
    60  		got := span.URIFromPath(test.path)
    61  		if got != test.wantURI {
    62  			t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
    63  		}
    64  		gotFilename := got.Filename()
    65  		if gotFilename != test.wantFile {
    66  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
    67  		}
    68  	}
    69  }
    70  
    71  func TestURIFromURI(t *testing.T) {
    72  	for _, test := range []struct {
    73  		inputURI, wantFile string
    74  		wantURI            span.URI
    75  	}{
    76  		{
    77  			inputURI: `file:///c:/Go/src/bob%20george/george/george.go`,
    78  			wantFile: `C:\Go\src\bob george\george\george.go`,
    79  			wantURI:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    80  		},
    81  		{
    82  			inputURI: `file:///C%3A/Go/src/bob%20george/george/george.go`,
    83  			wantFile: `C:\Go\src\bob george\george\george.go`,
    84  			wantURI:  span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
    85  		},
    86  		{
    87  			inputURI: `file:///c:/path/to/%25p%25ercent%25/per%25cent.go`,
    88  			wantFile: `C:\path\to\%p%ercent%\per%cent.go`,
    89  			wantURI:  span.URI(`file:///C:/path/to/%25p%25ercent%25/per%25cent.go`),
    90  		},
    91  		{
    92  			inputURI: `file:///C%3A/`,
    93  			wantFile: `C:\`,
    94  			wantURI:  span.URI(`file:///C:/`),
    95  		},
    96  		{
    97  			inputURI: `file:///`,
    98  			wantFile: `\`,
    99  			wantURI:  span.URI(`file:///`),
   100  		},
   101  	} {
   102  		got := span.URIFromURI(test.inputURI)
   103  		if got != test.wantURI {
   104  			t.Errorf("NewURI(%q): got %q, expected %q", test.inputURI, got, test.wantURI)
   105  		}
   106  		gotFilename := got.Filename()
   107  		if gotFilename != test.wantFile {
   108  			t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
   109  		}
   110  	}
   111  }