github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/exports_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 "testing" 8 9 func TestExports(t *testing.T) { 10 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test.go" 11 const code = `package example 12 13 type SomeType struct{} 14 const SomeConst = 123 15 var SomeVar int = 123 16 func SomeFunc1() {} 17 func SomeFunc2() {} 18 ` 19 expects := []string{ 20 filename + ":3:6: Tast requires exactly one symbol (test function or service type) to be exported in an entry file; unexport type SomeType if it is not one", 21 filename + ":4:7: Tast requires exactly one symbol (test function or service type) to be exported in an entry file; unexport const SomeConst", 22 filename + ":5:5: Tast requires exactly one symbol (test function or service type) to be exported in an entry file; unexport var SomeVar", 23 filename + ":6:6: Tast requires exactly one symbol (test function or service type) to be exported in an entry file; unexport func SomeFunc1 if it is not one", 24 filename + ":7:6: Tast requires exactly one symbol (test function or service type) to be exported in an entry file; unexport func SomeFunc2 if it is not one", 25 } 26 27 f, fs := parse(code, filename) 28 issues := Exports(fs, f) 29 verifyIssues(t, issues, expects) 30 } 31 32 func TestExports_ZeroFunc(t *testing.T) { 33 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test.go" 34 const code = `package example 35 ` 36 expects := []string{ 37 filename + ": Tast requires exactly one symbol (test function or service type) to be exported in an entry file", 38 } 39 40 f, fs := parse(code, filename) 41 issues := Exports(fs, f) 42 verifyIssues(t, issues, expects) 43 } 44 45 func TestExports_OneFunc(t *testing.T) { 46 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test.go" 47 const code = `package example 48 49 func SomeFunc1() {} // considered the test main function 50 ` 51 52 f, fs := parse(code, filename) 53 issues := Exports(fs, f) 54 verifyIssues(t, issues, nil) 55 } 56 57 func TestExports_NonTestMainFile(t *testing.T) { 58 const filename = "src/go.chromium.org/tast-tests/cros/local/chrome/const.go" 59 const code = `package example 60 61 type SomeType struct{} 62 const SomeConst = 123 63 var SomeVar int = 123 64 func SomeFunc1() {} 65 func SomeFunc2() {} 66 ` 67 f, fs := parse(code, filename) 68 issues := Exports(fs, f) 69 verifyIssues(t, issues, nil) 70 } 71 72 func TestExports_DocFile(t *testing.T) { 73 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/doc.go" 74 const code = `// Package example demonstrates how to do things. 75 package example 76 ` 77 f, fs := parse(code, filename) 78 issues := Exports(fs, f) 79 verifyIssues(t, issues, nil) 80 } 81 82 func TestExports_Methods(t *testing.T) { 83 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/test.go" 84 const code = `package example 85 86 type someType int 87 func (x someType) Close() {} 88 89 func Test() {} 90 ` 91 f, fs := parse(code, filename) 92 issues := Exports(fs, f) 93 verifyIssues(t, issues, nil) 94 } 95 96 func TestExports_Service(t *testing.T) { 97 const filename = "src/go.chromium.org/tast-tests/cros/local/bundles/cros/example/service.go" 98 const code = `package example 99 100 type Service struct {} 101 ` 102 f, fs := parse(code, filename) 103 issues := Exports(fs, f) 104 verifyIssues(t, issues, nil) 105 }