github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/cmd/tast-lint/internal/check/package_comment_test.go (about)

     1  // Copyright 2019 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 TestPackageCommentNoComment(t *testing.T) {
    15  	const code = `
    16  package newpackage
    17  `
    18  	const path = "newpackage/newpackage.go"
    19  	f, fs := parse(code, path)
    20  	files := make(map[string]*ast.File)
    21  	files[path] = f
    22  	pkg := ast.Package{
    23  		Name:  "newpackage",
    24  		Files: files,
    25  	}
    26  	issues := PackageComment(fs, &pkg)
    27  	expects := []string{
    28  		path + ":2:1: document of newly created package 'newpackage' is required in one of the files in this directory",
    29  	}
    30  	verifyIssues(t, issues, expects)
    31  }
    32  
    33  func TestPackageCommentMultiOK(t *testing.T) {
    34  	codepaths := []struct {
    35  		code string
    36  		path string
    37  	}{
    38  		{
    39  			code: `
    40  package newpackage
    41  `,
    42  			path: "newpackage/newpackage1.go",
    43  		},
    44  		{
    45  			code: `
    46  // Copyright
    47  
    48  // Package newpackage do nothing
    49  // do nothing
    50  package newpackage
    51  `,
    52  			path: "newpackage/newpackage2.go",
    53  		},
    54  	}
    55  	files := make(map[string]*ast.File)
    56  	fs := token.NewFileSet()
    57  	for _, pair := range codepaths {
    58  		f, err := parser.ParseFile(fs, pair.path, pair.code, parser.ParseComments)
    59  		if err != nil {
    60  			t.Errorf("Cannot parse the code: %s", err)
    61  		}
    62  		files[pair.path] = f
    63  	}
    64  	pkg := ast.Package{
    65  		Name:  "newpackage",
    66  		Files: files,
    67  	}
    68  	issues := PackageComment(fs, &pkg)
    69  	verifyIssues(t, issues, nil)
    70  }
    71  
    72  func TestPackageCommentMultiBad(t *testing.T) {
    73  	codepaths := []struct {
    74  		code string
    75  		path string
    76  	}{
    77  		{
    78  			code: `
    79  package docisneeded
    80  `,
    81  			path: "docisneeded/first.go",
    82  		},
    83  		{
    84  			code: `
    85  package docisneeded
    86  
    87  func main(){}
    88  `,
    89  			path: "docisneeded/second.go",
    90  		},
    91  		{
    92  			code: `
    93  package docisneeded
    94  
    95  func main(){}
    96  `,
    97  			path: "docisneeded/third.go",
    98  		},
    99  	}
   100  	files := make(map[string]*ast.File)
   101  	fs := token.NewFileSet()
   102  	for _, pair := range codepaths {
   103  		f, err := parser.ParseFile(fs, pair.path, pair.code, parser.ParseComments)
   104  		if err != nil {
   105  			t.Errorf("Cannot parse the code: %s", err)
   106  		}
   107  		files[pair.path] = f
   108  	}
   109  	pkg := ast.Package{
   110  		Name:  "docisneeded",
   111  		Files: files,
   112  	}
   113  	issues := PackageComment(fs, &pkg)
   114  	expects := []string{
   115  		"docisneeded/third.go:2:1: document of newly created package 'docisneeded' is required in one of the files in this directory",
   116  	}
   117  	verifyIssues(t, issues, expects)
   118  }