golang.org/x/tools/gopls@v0.15.3/internal/cmd/spanformat_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  package cmd
     6  
     7  import (
     8  	"fmt"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestSpanFormat(t *testing.T) {
    15  	formats := []string{"%v", "%#v", "%+v"}
    16  
    17  	// Element 0 is the input, and the elements 0-2 are the expected
    18  	// output in [%v %#v %+v] formats. Thus the first must be in
    19  	// canonical form (invariant under parseSpan + fmt.Sprint).
    20  	// The '#' form displays offsets; the '+' form outputs a URI.
    21  	// If len=4, element 0 is a noncanonical input and 1-3 are expected outputs.
    22  	for _, test := range [][]string{
    23  		{"C:/file_a", "C:/file_a", "file:///C:/file_a:#0"},
    24  		{"C:/file_b:1:2", "C:/file_b:1:2", "file:///C:/file_b:1:2"},
    25  		{"C:/file_c:1000", "C:/file_c:1000", "file:///C:/file_c:1000:1"},
    26  		{"C:/file_d:14:9", "C:/file_d:14:9", "file:///C:/file_d:14:9"},
    27  		{"C:/file_e:1:2-7", "C:/file_e:1:2-7", "file:///C:/file_e:1:2-1:7"},
    28  		{"C:/file_f:500-502", "C:/file_f:500-502", "file:///C:/file_f:500:1-502:1"},
    29  		{"C:/file_g:3:7-8", "C:/file_g:3:7-8", "file:///C:/file_g:3:7-3:8"},
    30  		{"C:/file_h:3:7-4:8", "C:/file_h:3:7-4:8", "file:///C:/file_h:3:7-4:8"},
    31  		{"C:/file_i:#100", "C:/file_i:#100", "file:///C:/file_i:#100"},
    32  		{"C:/file_j:#26-#28", "C:/file_j:#26-#28", "file:///C:/file_j:#26-0#28"}, // 0#28?
    33  		{"C:/file_h:3:7#26-4:8#37", // not canonical
    34  			"C:/file_h:3:7-4:8", "C:/file_h:#26-#37", "file:///C:/file_h:3:7#26-4:8#37"}} {
    35  		input := test[0]
    36  		spn := parseSpan(input)
    37  		wants := test[0:3]
    38  		if len(test) == 4 {
    39  			wants = test[1:4]
    40  		}
    41  		for i, format := range formats {
    42  			want := toPath(wants[i])
    43  			if got := fmt.Sprintf(format, spn); got != want {
    44  				t.Errorf("Sprintf(%q, %q) = %q, want %q", format, input, got, want)
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func toPath(value string) string {
    51  	if strings.HasPrefix(value, "file://") {
    52  		return value
    53  	}
    54  	return filepath.FromSlash(value)
    55  }