Skip to content

Commit 49eddf5

Browse files
committed
test: add godoclint tests
Signed-off-by: Babak K. Shandiz <[email protected]>
1 parent 3dc0816 commit 49eddf5

14 files changed

+629
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package godoclint
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/test/testshared/integration"
7+
)
8+
9+
func TestFromTestdata(t *testing.T) {
10+
integration.RunTestdata(t)
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an unusual configuration assigning all options with non-default values
2+
# to ensure they are correctly parsed.
3+
4+
version: "2"
5+
6+
linters:
7+
settings:
8+
godoclint:
9+
exclude:
10+
- _excluded.go$
11+
- _excluded_test.go$
12+
enable:
13+
- pkg-doc
14+
- require-pkg-doc
15+
- start-with-name
16+
- require-doc
17+
- no-unused-link
18+
- max-len
19+
options:
20+
pkg-doc:
21+
start-with: PACKAGE
22+
include-tests: true
23+
require-pkg-doc:
24+
include-tests: true
25+
start-with-name:
26+
pattern: "GODOC %"
27+
include-unexported: true
28+
include-tests: true
29+
require-doc:
30+
include-tests: true
31+
ignore-exported: false
32+
ignore-unexported: false
33+
max-len:
34+
length: 127
35+
include-tests: true
36+
no-unused-link:
37+
include-tests: true
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//golangcitest:args -Egodoclint
2+
3+
// bad godoc // want `package godoc should start with "Package testdata "`
4+
package testdata
5+
6+
// This is a special stdlib import because the package itself has issues that
7+
// godoclint can, but must not, detect.
8+
import "go/ast"
9+
10+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
11+
type FooType struct{}
12+
13+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
14+
type FooAlias = ast.Comment
15+
16+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
17+
const FooConst = 1
18+
19+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
20+
var FooVar = 1
21+
22+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
23+
func FooFunc() {}
24+
25+
// bad godoc // want `godoc should start with symbol name \(pattern "\(\(A\|a\|An\|an\|THE\|The\|the\) \)\?%"\)`
26+
func (FooType) FooFunc() {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//golangcitest:args -Egodoclint
2+
//golangcitest:expected_exitcode 0
3+
4+
// Package testdata has good godoc.
5+
package testdata
6+
7+
// This is a special stdlib import because the package itself has issues that
8+
// godoclint can, but must not, detect.
9+
import "go/ast"
10+
11+
// FooType is a type.
12+
type FooType struct{}
13+
14+
// FooAlias is an alias.
15+
type FooAlias = ast.Comment
16+
17+
// FooConst is a constant.
18+
const FooConst = 1
19+
20+
// FooVar is a variable.
21+
var FooVar = 1
22+
23+
// FooFunc is a function.
24+
func FooFunc() {}
25+
26+
// FooFunc is a method.
27+
func (FooType) FooFunc() {}
28+
29+
// bad godoc on unexported symbol
30+
type fooType struct{}
31+
32+
// bad godoc on unexported symbol
33+
type fooAlias = ast.Comment
34+
35+
// bad godoc on unexported symbol
36+
const fooConst = 1
37+
38+
// bad godoc on unexported symbol
39+
var fooVar = 1
40+
41+
// bad godoc on unexported symbol
42+
func fooFunc() {}
43+
44+
// bad godoc on unexported symbol
45+
func (FooType) fooFunc() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//golangcitest:args -Egodoclint
2+
//golangcitest:config_path testdata/godoclint.yml
3+
//golangcitest:expected_exitcode 0
4+
5+
// Since this file is excluded in the config, godoclint should not report any
6+
// issues (i.e.exit code 0).
7+
8+
// bad godoc
9+
package testdata
10+
11+
// This is a special stdlib import because the package itself has issues that
12+
// godoclint can, but must not, detect.
13+
import "go/ast"
14+
15+
// bad godoc
16+
type FooAlias = ast.Comment
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//golangcitest:args -Egodoclint
2+
//golangcitest:config_path testdata/godoclint.yml
3+
//golangcitest:expected_exitcode 0
4+
5+
// Since this file is excluded in the config, godoclint should not report any
6+
// issues (i.e.exit code 0).
7+
8+
// bad godoc
9+
package testdata
10+
11+
// This is a special stdlib import because the package itself has issues that
12+
// godoclint can, but must not, detect.
13+
import "go/ast"
14+
15+
// bad godoc
16+
type FooAlias = ast.Comment
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//golangcitest:args -Egodoclint
2+
//golangcitest:config_path testdata/godoclint.yml
3+
4+
// Asserting rule "pkg-doc"
5+
6+
// bad godoc // want `package godoc should start with "PACKAGE testdata "`
7+
package testdata
8+
9+
// This is a special stdlib import because the package itself has issues that
10+
// godoclint can, but must not, detect.
11+
import "go/ast"
12+
13+
// Asserting rule "start-with-name"
14+
15+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
16+
type FooType struct{}
17+
18+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
19+
type FooAlias = ast.Comment
20+
21+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
22+
const FooConst = 1
23+
24+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
25+
var FooVar = 1
26+
27+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
28+
func FooFunc() {}
29+
30+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
31+
func (FooType) FooFunc() {}
32+
33+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
34+
type fooType struct{}
35+
36+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
37+
type fooAlias = ast.Comment
38+
39+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
40+
const fooConst = 1
41+
42+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
43+
var fooVar = 1
44+
45+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
46+
func fooFunc() {}
47+
48+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
49+
func (FooType) fooFunc() {}
50+
51+
// Asserting rule "require-doc"
52+
53+
// The //foo:bar directives mark the trailing comment as a directive so they're
54+
// not parsed as a normal trailing comment group.
55+
56+
type BarType struct{} //foo:bar // want `symbol should have a godoc \(BarType\)`
57+
58+
type BarAlias = ast.Comment //foo:bar // want `symbol should have a godoc \(BarAlias\)`
59+
60+
const BarConst = 1 //foo:bar // want `symbol should have a godoc \(BarConst\)`
61+
62+
var BarVar = 1 //foo:bar // want `symbol should have a godoc \(BarVar\)`
63+
64+
func BarFunc() {} //foo:bar // want `symbol should have a godoc \(BarFunc\)`
65+
66+
func (BarType) BarFunc() {} //foo:bar // want `symbol should have a godoc \(BarFunc\)`
67+
68+
type barType struct{} //foo:bar // want `symbol should have a godoc \(barType\)`
69+
70+
type barAlias = ast.Comment //foo:bar // want `symbol should have a godoc \(barAlias\)`
71+
72+
const barConst = 1 //foo:bar // want `symbol should have a godoc \(barConst\)`
73+
74+
var barVar = 1 //foo:bar // want `symbol should have a godoc \(barVar\)`
75+
76+
func barFunc() {} //foo:bar // want `symbol should have a godoc \(barFunc\)`
77+
78+
func (BarType) barFunc() {} //foo:bar // want `symbol should have a godoc \(barFunc\)`
79+
80+
// Asserting rule "no-unused-link"
81+
82+
// GODOC constWithUnusedLink point to [used] and unused links. // want `godoc has unused link \(unused\)`
83+
//
84+
// [used]: https://example.com
85+
//
86+
// [unused]: https://example.com
87+
const constWithUnusedLink = 1
88+
89+
// Asserting rule "max-len"
90+
91+
// GODOC constWithTooLongGodoc has a very long godoc that exceeds the maximum allowed length for godoc comments in this test setup. // want `godoc exceeds max length \(177 > 127\)`
92+
const constWithTooLongGodoc = 1
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//golangcitest:args -Egodoclint
2+
//golangcitest:config_path testdata/godoclint.yml
3+
4+
// Asserting rule "pkg-doc"
5+
6+
// bad godoc // want `package godoc should start with "PACKAGE testdata_test "`
7+
package testdata_test
8+
9+
// This is a special stdlib import because the package itself has issues that
10+
// godoclint can, but must not, detect.
11+
import "go/ast"
12+
13+
// Asserting rule "start-with-name"
14+
15+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
16+
type FooType struct{}
17+
18+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
19+
type FooAlias = ast.Comment
20+
21+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
22+
const FooConst = 1
23+
24+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
25+
var FooVar = 1
26+
27+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
28+
func FooFunc() {}
29+
30+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
31+
func (FooType) FooFunc() {}
32+
33+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
34+
type fooType struct{}
35+
36+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
37+
type fooAlias = ast.Comment
38+
39+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
40+
const fooConst = 1
41+
42+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
43+
var fooVar = 1
44+
45+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
46+
func fooFunc() {}
47+
48+
// bad godoc // want `godoc should start with symbol name \(pattern "GODOC %"\)`
49+
func (FooType) fooFunc() {}
50+
51+
// Asserting rule "require-doc"
52+
53+
// The //foo:bar directives mark the trailing comment as a directive so they're
54+
// not parsed as a normal trailing comment group.
55+
56+
type BarType struct{} //foo:bar // want `symbol should have a godoc \(BarType\)`
57+
58+
type BarAlias = ast.Comment //foo:bar // want `symbol should have a godoc \(BarAlias\)`
59+
60+
const BarConst = 1 //foo:bar // want `symbol should have a godoc \(BarConst\)`
61+
62+
var BarVar = 1 //foo:bar // want `symbol should have a godoc \(BarVar\)`
63+
64+
func BarFunc() {} //foo:bar // want `symbol should have a godoc \(BarFunc\)`
65+
66+
func (BarType) BarFunc() {} //foo:bar // want `symbol should have a godoc \(BarFunc\)`
67+
68+
type barType struct{} //foo:bar // want `symbol should have a godoc \(barType\)`
69+
70+
type barAlias = ast.Comment //foo:bar // want `symbol should have a godoc \(barAlias\)`
71+
72+
const barConst = 1 //foo:bar // want `symbol should have a godoc \(barConst\)`
73+
74+
var barVar = 1 //foo:bar // want `symbol should have a godoc \(barVar\)`
75+
76+
func barFunc() {} //foo:bar // want `symbol should have a godoc \(barFunc\)`
77+
78+
func (BarType) barFunc() {} //foo:bar // want `symbol should have a godoc \(barFunc\)`
79+
80+
// Asserting rule "no-unused-link"
81+
82+
// GODOC constWithUnusedLink point to [used] and unused links. // want `godoc has unused link \(unused\)`
83+
//
84+
// [used]: https://example.com
85+
//
86+
// [unused]: https://example.com
87+
const constWithUnusedLink = 1
88+
89+
// Asserting rule "max-len"
90+
91+
// GODOC constWithTooLongGodoc has a very long godoc that exceeds the maximum allowed length for godoc comments in this test setup. // want `godoc exceeds max length \(177 > 127\)`
92+
const constWithTooLongGodoc = 1

0 commit comments

Comments
 (0)