File: spanformat_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (55 lines) | stat: -rw-r--r-- 1,948 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cmd

import (
	"fmt"
	"path/filepath"
	"strings"
	"testing"
)

func TestSpanFormat(t *testing.T) {
	formats := []string{"%v", "%#v", "%+v"}

	// Element 0 is the input, and the elements 0-2 are the expected
	// output in [%v %#v %+v] formats. Thus the first must be in
	// canonical form (invariant under parseSpan + fmt.Sprint).
	// The '#' form displays offsets; the '+' form outputs a URI.
	// If len=4, element 0 is a noncanonical input and 1-3 are expected outputs.
	for _, test := range [][]string{
		{"C:/file_a", "C:/file_a", "file:///C:/file_a:#0"},
		{"C:/file_b:1:2", "C:/file_b:1:2", "file:///C:/file_b:1:2"},
		{"C:/file_c:1000", "C:/file_c:1000", "file:///C:/file_c:1000:1"},
		{"C:/file_d:14:9", "C:/file_d:14:9", "file:///C:/file_d:14:9"},
		{"C:/file_e:1:2-7", "C:/file_e:1:2-7", "file:///C:/file_e:1:2-1:7"},
		{"C:/file_f:500-502", "C:/file_f:500-502", "file:///C:/file_f:500:1-502:1"},
		{"C:/file_g:3:7-8", "C:/file_g:3:7-8", "file:///C:/file_g:3:7-3:8"},
		{"C:/file_h:3:7-4:8", "C:/file_h:3:7-4:8", "file:///C:/file_h:3:7-4:8"},
		{"C:/file_i:#100", "C:/file_i:#100", "file:///C:/file_i:#100"},
		{"C:/file_j:#26-#28", "C:/file_j:#26-#28", "file:///C:/file_j:#26-0#28"}, // 0#28?
		{"C:/file_h:3:7#26-4:8#37", // not canonical
			"C:/file_h:3:7-4:8", "C:/file_h:#26-#37", "file:///C:/file_h:3:7#26-4:8#37"}} {
		input := test[0]
		spn := parseSpan(input)
		wants := test[0:3]
		if len(test) == 4 {
			wants = test[1:4]
		}
		for i, format := range formats {
			want := toPath(wants[i])
			if got := fmt.Sprintf(format, spn); got != want {
				t.Errorf("Sprintf(%q, %q) = %q, want %q", format, input, got, want)
			}
		}
	}
}

func toPath(value string) string {
	if strings.HasPrefix(value, "file://") {
		return value
	}
	return filepath.FromSlash(value)
}