|
| 1 | +package weather |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "go/parser" |
| 7 | + "go/token" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestComments(t *testing.T) { |
| 13 | + filename := "weather_forecast.go" |
| 14 | + |
| 15 | + fs := token.NewFileSet() |
| 16 | + f, err := parser.ParseFile(fs, filename, nil, parser.ParseComments) |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + |
| 21 | + wantedComments := 4 |
| 22 | + got := len(f.Comments) |
| 23 | + if got != wantedComments { |
| 24 | + t.Errorf("Incorrect number of comments: got %d, want %d", got, wantedComments) |
| 25 | + } |
| 26 | + |
| 27 | + testPackageComment(t, f) |
| 28 | + |
| 29 | + ast.Inspect(f, func(node ast.Node) bool { |
| 30 | + switch n := node.(type) { |
| 31 | + case *ast.GenDecl: |
| 32 | + if n.Lparen.IsValid() { |
| 33 | + for _, v := range n.Specs { |
| 34 | + testBlockIdentifierComment(t, v.(*ast.ValueSpec)) |
| 35 | + } |
| 36 | + } else { |
| 37 | + testIdentifierComment(t, n) |
| 38 | + } |
| 39 | + case *ast.FuncDecl: |
| 40 | + testFunctionComment(t, n) |
| 41 | + } |
| 42 | + return true |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +func testPackageComment(t *testing.T, node *ast.File) { |
| 47 | + t.Helper() |
| 48 | + if node.Doc == nil { |
| 49 | + t.Errorf("Package weather should have a comment") |
| 50 | + } |
| 51 | + |
| 52 | + packageName := node.Name.Name |
| 53 | + want := "Package " + packageName |
| 54 | + packageComment := node.Doc.Text() |
| 55 | + |
| 56 | + if ok, errStr := testComment("Package", packageName, packageComment, want); !ok { |
| 57 | + t.Error(errStr) |
| 58 | + } |
| 59 | + |
| 60 | +} |
| 61 | +func testIdentifierComment(t *testing.T, node *ast.GenDecl) { |
| 62 | + t.Helper() |
| 63 | + |
| 64 | + identifierName := node.Specs[0].(*ast.ValueSpec).Names[0].Name |
| 65 | + if node.Doc == nil { |
| 66 | + t.Errorf("Exported identifier %s should have a comment", identifierName) |
| 67 | + } |
| 68 | + |
| 69 | + identifierComment := node.Doc.Text() |
| 70 | + want := identifierName |
| 71 | + |
| 72 | + if ok, errStr := testComment("Variable", identifierName, identifierComment, want); !ok { |
| 73 | + t.Error(errStr) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func testBlockIdentifierComment(t *testing.T, node *ast.ValueSpec) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + identifierName := node.Names[0].Name |
| 81 | + if node.Doc == nil { |
| 82 | + t.Errorf("Exported identifier %s should have a comment", identifierName) |
| 83 | + } |
| 84 | + |
| 85 | + identifierComment := node.Doc.Text() |
| 86 | + want := identifierName |
| 87 | + |
| 88 | + if ok, errStr := testComment("Variable", identifierName, identifierComment, want); !ok { |
| 89 | + t.Error(errStr) |
| 90 | + } |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +func testFunctionComment(t *testing.T, node *ast.FuncDecl) { |
| 95 | + t.Helper() |
| 96 | + funcName := node.Name.Name |
| 97 | + if node.Doc == nil { |
| 98 | + t.Errorf("Exported function %s() should have a comment", funcName) |
| 99 | + } |
| 100 | + |
| 101 | + funcComment := node.Doc.Text() |
| 102 | + want := funcName |
| 103 | + |
| 104 | + if ok, errStr := testComment("Function", funcName, funcComment, want); !ok { |
| 105 | + t.Error(errStr) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func testComment(entityKind, entityName, comment, wantedPrefix string) (ok bool, errString string) { |
| 110 | + |
| 111 | + trimmedComment := strings.TrimSpace(comment) |
| 112 | + lowerEntity := strings.ToLower(entityKind) |
| 113 | + |
| 114 | + // Check if comment has wanted prefix |
| 115 | + if !strings.HasPrefix(comment, wantedPrefix) { |
| 116 | + errorString := fmt.Sprintf("%s comment for %s '%s' should start with '// %s ...': got '// %s'", |
| 117 | + entityKind, lowerEntity, entityName, wantedPrefix, trimmedComment) |
| 118 | + return false, errorString |
| 119 | + } |
| 120 | + |
| 121 | + // Check if comment content is empty |
| 122 | + commentContent := strings.TrimPrefix(trimmedComment, wantedPrefix) |
| 123 | + commentContent = strings.TrimSpace(commentContent) |
| 124 | + commentContent = strings.TrimSuffix(commentContent, ".") |
| 125 | + |
| 126 | + if commentContent == "" { |
| 127 | + lowerEntity := strings.ToLower(entityKind) |
| 128 | + errorString := fmt.Sprintf("%s comment of '%s' should provide a description of the %s, e.g '// %s <%s_description>'", |
| 129 | + entityKind, entityName, lowerEntity, wantedPrefix, lowerEntity) |
| 130 | + return false, errorString |
| 131 | + } |
| 132 | + |
| 133 | + // Check if comment ends in a period |
| 134 | + if !strings.HasSuffix(trimmedComment, ".") { |
| 135 | + return false, fmt.Sprintf("%s comment for %s '%s' should end with a period (.)", |
| 136 | + entityKind, lowerEntity, entityName) |
| 137 | + } |
| 138 | + |
| 139 | + return true, "" |
| 140 | +} |
0 commit comments