File: span_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (74 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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 span_test

import (
	"fmt"
	"go/token"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/tools/gopls/internal/span"
)

var (
	tests = [][]string{
		{"C:/file_a", "C:/file_a", "file:///C:/file_a:1:1#0"},
		{"C:/file_b:1:2", "C:/file_b:#1", "file:///C:/file_b:1:2#1"},
		{"C:/file_c:1000", "C:/file_c:#9990", "file:///C:/file_c:1000:1#9990"},
		{"C:/file_d:14:9", "C:/file_d:#138", "file:///C:/file_d:14:9#138"},
		{"C:/file_e:1:2-7", "C:/file_e:#1-#6", "file:///C:/file_e:1:2#1-1:7#6"},
		{"C:/file_f:500-502", "C:/file_f:#4990-#5010", "file:///C:/file_f:500:1#4990-502:1#5010"},
		{"C:/file_g:3:7-8", "C:/file_g:#26-#27", "file:///C:/file_g:3:7#26-3:8#27"},
		{"C:/file_h:3:7-4:8", "C:/file_h:#26-#37", "file:///C:/file_h:3:7#26-4:8#37"},
	}
)

func TestFormat(t *testing.T) {
	converter := lines(10)
	for _, test := range tests {
		for ti, text := range test[:2] {
			spn := span.Parse(text)
			if ti <= 1 {
				// we can check %v produces the same as the input
				expect := toPath(test[ti])
				if got := fmt.Sprintf("%v", spn); got != expect {
					t.Errorf("printing %q got %q expected %q", text, got, expect)
				}
			}
			complete, err := spn.WithAll(converter)
			if err != nil {
				t.Error(err)
			}
			for fi, format := range []string{"%v", "%#v", "%+v"} {
				expect := toPath(test[fi])
				if got := fmt.Sprintf(format, complete); got != expect {
					t.Errorf("printing completed %q as %q got %q expected %q [%+v]", text, format, got, expect, spn)
				}
			}
		}
	}
}

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

// lines creates a new tokenConverter for a file with 1000 lines, each width
// bytes wide.
func lines(width int) *token.File {
	fset := token.NewFileSet()
	f := fset.AddFile("", -1, 1000*width)
	var lines []int
	for i := 0; i < 1000; i++ {
		lines = append(lines, i*width)
	}
	f.SetLines(lines)
	return f
}