55 "unicode"
66)
77
8+ // Expression represents a type expression in Go code, including metadata about pointers, variadic parameters, and writers.
89type 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.
1618func (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.
2730type Field struct {
2831 Name string
2932 Type * Expression
3033 Index int
3134}
3235
36+ // IsWriter returns true if the field is an io.Writer.
3337func (f * Field ) IsWriter () bool {
3438 return f .Type .IsWriter
3539}
3640
41+ // IsStruct returns true if the field's underlying type is a struct.
3742func (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.).
4147func (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.
5663func (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.
6068func (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.
6473type 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.
7079type 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.
7585type 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.
8596func (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.
96108func (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.
116129func (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.
120134func (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.
124139func (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.
128144func (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.
136153func (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.
158176func (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.
162181type 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.
166186type Header struct {
167187 Comments []string
168188 Package string
169189 Imports []* Import
170190 Code []byte
171191}
172192
193+ // Path represents a file system path.
173194type Path string
174195
196+ // TestPath returns the test file path for the given source file path.
175197func (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).
182205func (p Path ) IsTestPath () bool {
183206 return strings .HasSuffix (string (p ), "_test.go" )
184207}
0 commit comments