Skip to content

Commit 97f0e5f

Browse files
cweillclaude
andauthored
docs: add godoc comments to all exported functions (#195)
Added comprehensive godoc comments following Go conventions to all exported types, functions, methods, and variables across the codebase. This improves code documentation and makes the package more maintainable and easier to understand for contributors. Files updated: - internal/models/models.go: 25+ exported items documented - internal/goparser/goparser.go: 3 exported items documented - internal/render/render.go: 4 exported items documented - internal/output/options.go: 2 exported items documented - internal/output/helpers.go: 1 exported item documented - internal/output/imports.go: 1 exported item documented - gotests/process/process.go: 2 exported items documented - templates/embed.go: 1 exported item documented All tests pass successfully. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 44438c9 commit 97f0e5f

File tree

8 files changed

+42
-9
lines changed

8 files changed

+42
-9
lines changed

gotests/process/process.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
specifyFileMessage = "Please specify a file or directory containing the source"
2222
)
2323

24-
// Set of options to use when generating tests.
24+
// Options contains command-line configuration for generating tests.
2525
type Options struct {
2626
OnlyFuncs string // Regexp string for filter matches.
2727
ExclFuncs string // Regexp string for excluding matches.
@@ -40,9 +40,9 @@ type Options struct {
4040
UseGoCmp bool // Use cmp.Equal (google/go-cmp) instead of reflect.DeepEqual
4141
}
4242

43-
// Generates tests for the Go files defined in args with the given options.
44-
// Logs information and errors to out. By default outputs generated tests to
45-
// out unless specified by opt.
43+
// Run generates tests for the Go files defined in args with the given options.
44+
// It logs information and errors to out. By default, it outputs generated tests to
45+
// out unless WriteOutput is specified in opts.
4646
func Run(out io.Writer, args []string, opts *Options) {
4747
if opts == nil {
4848
opts = &Options{}

internal/goparser/goparser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ import (
1515
"github.com/cweill/gotests/internal/models"
1616
)
1717

18-
// ErrEmptyFile represents an empty file error.
18+
// ErrEmptyFile is returned when attempting to parse an empty Go source file.
1919
var ErrEmptyFile = errors.New("file is empty")
2020

21-
// Result representats a parsed Go file.
21+
// Result represents a parsed Go file containing the header information and function signatures.
2222
type Result struct {
2323
// The package name and imports of a Go file.
2424
Header *models.Header
2525
// All the functions and methods in a Go file.
2626
Funcs []*models.Function
2727
}
2828

29-
// Parser can parse Go files.
29+
// Parser parses Go source files into domain models for test generation.
3030
type Parser struct {
3131
// The importer to resolve packages from import paths.
3232
Importer types.Importer

internal/models/models.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"unicode"
66
)
77

8+
// Expression represents a type expression in Go code, including metadata about pointers, variadic parameters, and writers.
89
type Expression struct {
910
Value string
1011
IsStar bool
@@ -13,6 +14,7 @@ type Expression struct {
1314
Underlying string
1415
}
1516

17+
// String returns the string representation of the expression, including pointer and variadic prefixes.
1618
func (e *Expression) String() string {
1719
value := e.Value
1820
if e.IsStar {
@@ -24,20 +26,24 @@ func (e *Expression) String() string {
2426
return value
2527
}
2628

29+
// Field represents a parameter, result, or struct field in a function or method signature.
2730
type Field struct {
2831
Name string
2932
Type *Expression
3033
Index int
3134
}
3235

36+
// IsWriter returns true if the field is an io.Writer.
3337
func (f *Field) IsWriter() bool {
3438
return f.Type.IsWriter
3539
}
3640

41+
// IsStruct returns true if the field's underlying type is a struct.
3742
func (f *Field) IsStruct() bool {
3843
return strings.HasPrefix(f.Type.Underlying, "struct")
3944
}
4045

46+
// IsBasicType returns true if the field is a Go basic type (bool, string, int, etc.).
4147
func (f *Field) IsBasicType() bool {
4248
return isBasicType(f.Type.String()) || isBasicType(f.Type.Underlying)
4349
}
@@ -53,25 +59,29 @@ func isBasicType(t string) bool {
5359
}
5460
}
5561

62+
// IsNamed returns true if the field has a non-blank name.
5663
func (f *Field) IsNamed() bool {
5764
return f.Name != "" && f.Name != "_"
5865
}
5966

67+
// ShortName returns a short single-letter name based on the field's type.
6068
func (f *Field) ShortName() string {
6169
return strings.ToLower(string([]rune(f.Type.Value)[0]))
6270
}
6371

72+
// Receiver represents a method receiver, including its type and struct fields.
6473
type Receiver struct {
6574
*Field
6675
Fields []*Field
6776
}
6877

69-
// TypeParam represents a type parameter in a generic function or type
78+
// TypeParam represents a type parameter in a generic function or type.
7079
type TypeParam struct {
7180
Name string // e.g., "T", "K", "V"
7281
Constraint string // e.g., "any", "comparable", "int64 | float64"
7382
}
7483

84+
// Function represents a function or method signature with its parameters, results, and metadata.
7585
type Function struct {
7686
Name string
7787
IsExported bool
@@ -82,6 +92,7 @@ type Function struct {
8292
TypeParams []*TypeParam // Type parameters for generic functions
8393
}
8494

95+
// TestParameters returns the function's parameters excluding io.Writer parameters.
8596
func (f *Function) TestParameters() []*Field {
8697
var ps []*Field
8798
for _, p := range f.Parameters {
@@ -93,6 +104,7 @@ func (f *Function) TestParameters() []*Field {
93104
return ps
94105
}
95106

107+
// TestResults returns the function's results plus any io.Writer parameters converted to string results.
96108
func (f *Function) TestResults() []*Field {
97109
var ps []*Field
98110
ps = append(ps, f.Results...)
@@ -113,18 +125,22 @@ func (f *Function) TestResults() []*Field {
113125
return ps
114126
}
115127

128+
// ReturnsMultiple returns true if the function returns more than one value.
116129
func (f *Function) ReturnsMultiple() bool {
117130
return len(f.Results) > 1
118131
}
119132

133+
// OnlyReturnsOneValue returns true if the function returns exactly one non-error value.
120134
func (f *Function) OnlyReturnsOneValue() bool {
121135
return len(f.Results) == 1 && !f.ReturnsError
122136
}
123137

138+
// OnlyReturnsError returns true if the function returns only an error.
124139
func (f *Function) OnlyReturnsError() bool {
125140
return len(f.Results) == 0 && f.ReturnsError
126141
}
127142

143+
// FullName returns the full name of the function, including the receiver type if it's a method.
128144
func (f *Function) FullName() string {
129145
var r string
130146
if f.Receiver != nil {
@@ -133,6 +149,7 @@ func (f *Function) FullName() string {
133149
return strings.Title(r) + strings.Title(f.Name)
134150
}
135151

152+
// TestName returns the name to use for the generated test function.
136153
func (f *Function) TestName() string {
137154
if strings.HasPrefix(f.Name, "Test") {
138155
return f.Name
@@ -155,30 +172,36 @@ func (f *Function) TestName() string {
155172
return "Test" + f.Name
156173
}
157174

175+
// IsNaked returns true if the function has no receiver, parameters, or results.
158176
func (f *Function) IsNaked() bool {
159177
return f.Receiver == nil && len(f.Parameters) == 0 && len(f.Results) == 0
160178
}
161179

180+
// Import represents an import statement with an optional name and the import path.
162181
type Import struct {
163182
Name, Path string
164183
}
165184

185+
// Header represents the header of a Go file, including package name, imports, and any code between imports and declarations.
166186
type Header struct {
167187
Comments []string
168188
Package string
169189
Imports []*Import
170190
Code []byte
171191
}
172192

193+
// Path represents a file system path.
173194
type Path string
174195

196+
// TestPath returns the test file path for the given source file path.
175197
func (p Path) TestPath() string {
176198
if !p.IsTestPath() {
177199
return strings.TrimSuffix(string(p), ".go") + "_test.go"
178200
}
179201
return string(p)
180202
}
181203

204+
// IsTestPath returns true if the path is a test file path (ends with _test.go).
182205
func (p Path) IsTestPath() bool {
183206
return strings.HasSuffix(string(p), "_test.go")
184207
}

internal/output/helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package output
22

33
import "os"
44

5+
// IsFileExist checks whether a file exists at the given path.
56
func IsFileExist(path string) bool {
67
_, err := os.Stat(path)
78
return !os.IsNotExist(err)

internal/output/imports.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package output
22

3-
// we do not need support for aliases in import for now.
3+
// importsMap maps template names to their required import paths.
4+
// We do not need support for aliases in import for now.
45
var importsMap = map[string]string{
56
"testify": "github.com/stretchr/testify/assert",
67
}

internal/output/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"golang.org/x/tools/imports"
1414
)
1515

16+
// Options contains configuration for generating and formatting test output.
1617
type Options struct {
1718
PrintInputs bool
1819
Subtests bool
@@ -27,6 +28,7 @@ type Options struct {
2728
render *render.Render
2829
}
2930

31+
// Process generates formatted test code from the header and function signatures.
3032
func (o *Options) Process(head *models.Header, funcs []*models.Function) ([]byte, error) {
3133
o.render = render.New()
3234

internal/render/render.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
//go:embed templates/*
1717
var data embed.FS
1818

19+
// Render manages template rendering for generating test code.
1920
type Render struct {
2021
tmpls *template.Template
2122
}
2223

24+
// New creates a new Render instance with default templates and helper functions.
2325
func New() *Render {
2426
r := Render{
2527
tmpls: template.New("render").Funcs(map[string]interface{}{
@@ -81,6 +83,7 @@ func (r *Render) LoadFromData(templateData [][]byte) {
8183
}
8284
}
8385

86+
// Header renders the file header including package declaration and imports.
8487
func (r *Render) Header(w io.Writer, h *models.Header) error {
8588
if err := r.tmpls.ExecuteTemplate(w, "header", h); err != nil {
8689
return err
@@ -89,6 +92,7 @@ func (r *Render) Header(w io.Writer, h *models.Header) error {
8992
return err
9093
}
9194

95+
// TestFunction renders a test function for the given function signature with the specified options.
9296
func (r *Render) TestFunction(
9397
w io.Writer,
9498
f *models.Function,

templates/embed.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ package templates
22

33
import "embed"
44

5+
// FS contains embedded template files for test generation.
6+
//
57
//go:embed test/* testify/*
68
var FS embed.FS

0 commit comments

Comments
 (0)