golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/tokeninternal/tokeninternal_test.go (about) 1 // Copyright 2023 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 tokeninternal_test 6 7 import ( 8 "fmt" 9 "go/token" 10 "strings" 11 "testing" 12 13 "golang.org/x/tools/internal/tokeninternal" 14 ) 15 16 func TestAddExistingFiles(t *testing.T) { 17 fset := token.NewFileSet() 18 19 check := func(descr, want string) { 20 t.Helper() 21 if got := fsetString(fset); got != want { 22 t.Errorf("%s: got %s, want %s", descr, got, want) 23 } 24 } 25 26 fileA := fset.AddFile("A", -1, 3) 27 fileB := fset.AddFile("B", -1, 5) 28 _ = fileB 29 check("after AddFile [AB]", "{A:1-4 B:5-10}") 30 31 tokeninternal.AddExistingFiles(fset, nil) 32 check("after AddExistingFiles []", "{A:1-4 B:5-10}") 33 34 fileC := token.NewFileSet().AddFile("C", 100, 5) 35 fileD := token.NewFileSet().AddFile("D", 200, 5) 36 tokeninternal.AddExistingFiles(fset, []*token.File{fileC, fileA, fileD, fileC}) 37 check("after AddExistingFiles [CADC]", "{A:1-4 B:5-10 C:100-105 D:200-205}") 38 39 fileE := fset.AddFile("E", -1, 3) 40 _ = fileE 41 check("after AddFile [E]", "{A:1-4 B:5-10 C:100-105 D:200-205 E:206-209}") 42 } 43 44 func fsetString(fset *token.FileSet) string { 45 var buf strings.Builder 46 buf.WriteRune('{') 47 sep := "" 48 fset.Iterate(func(f *token.File) bool { 49 fmt.Fprintf(&buf, "%s%s:%d-%d", sep, f.Name(), f.Base(), f.Base()+f.Size()) 50 sep = " " 51 return true 52 }) 53 buf.WriteRune('}') 54 return buf.String() 55 }