github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/inter_file_refs_test.go (about) 1 // Copyright 2018 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package check 6 7 import ( 8 "go/ast" 9 "go/parser" 10 "go/token" 11 "testing" 12 ) 13 14 func parse2(filename1, code1, filename2, code2 string) (*token.FileSet, *ast.File, *ast.File) { 15 fs := token.NewFileSet() 16 f1, err := parser.ParseFile(fs, filename1, code1, 0) 17 if err != nil { 18 panic(err) 19 } 20 f2, err := parser.ParseFile(fs, filename2, code2, 0) 21 if err != nil { 22 panic(err) 23 } 24 files := map[string]*ast.File{ 25 filename1: f1, 26 filename2: f2, 27 } 28 ast.NewPackage(fs, files, nil, nil) 29 return fs, f1, f2 30 } 31 32 func TestInterFileRefs(t *testing.T) { 33 const filename1 = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test1.go" 34 const code1 = `package example 35 36 type someType struct{} 37 const someConst = 123 38 var someVar int = 123 39 func someFunc() {} 40 41 func Test1() { 42 var x someType 43 someVar = someConst 44 someFunc() 45 } 46 ` 47 48 const filename2 = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test2.go" 49 const code2 = `package example 50 51 func Test2() { 52 var x someType 53 someVar = someConst 54 someFunc() 55 } 56 ` 57 58 expects2 := []string{ 59 filename2 + ":4:8: Tast forbids inter-file references in entry files; move type someType in test1.go to a shared package", 60 filename2 + ":5:2: Tast forbids inter-file references in entry files; move var someVar in test1.go to a shared package", 61 filename2 + ":5:12: Tast forbids inter-file references in entry files; move const someConst in test1.go to a shared package", 62 filename2 + ":6:2: Tast forbids inter-file references in entry files; move func someFunc in test1.go to a shared package", 63 } 64 65 fs, f1, f2 := parse2(filename1, code1, filename2, code2) 66 67 issues1 := InterFileRefs(fs, f1) 68 verifyIssues(t, issues1, nil) 69 70 issues2 := InterFileRefs(fs, f2) 71 verifyIssues(t, issues2, expects2) 72 } 73 74 func TestInterFileRefs_NonTestMainFile(t *testing.T) { 75 const filename1 = "src/go.chromium.org/tast-tests/cros/local/chrome/chrome1.go" 76 const code1 = `package chrome 77 78 type someType struct{} 79 const someConst = 123 80 var someVar int = 123 81 func someFunc() {} 82 ` 83 84 const filename2 = "src/go.chromium.org/tast-tests/cros/local/chrome/chrome2.go" 85 const code2 = `package chrome 86 87 func Foo() { 88 var x someType 89 someVar = someConst 90 someFunc() 91 } 92 ` 93 94 fs, _, f := parse2(filename1, code1, filename2, code2) 95 96 issues := InterFileRefs(fs, f) 97 verifyIssues(t, issues, nil) 98 } 99 100 func TestInterFileRefs_ForeignRefs(t *testing.T) { 101 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test.go" 102 const code = `package example 103 104 import "go.chromium.org/tast-tests/cros/local/bundles/cros/example/util" 105 106 func Test() { 107 var x util.SomeType 108 util.SomeVar = util.SomeConst 109 util.SomeFunc() 110 } 111 ` 112 113 f, fs := parse(code, filename) 114 115 issues := InterFileRefs(fs, f) 116 verifyIssues(t, issues, nil) 117 }