Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Generic Fields does not handle Arrays (#1311)
* fix: Generic Fields does not handle Arrays - Support for *ast.IndexExpr added - tests extended to cover use cases fixes #1306 * test: Extend tests to improve code coverage
- Loading branch information
1 parent
007219f
commit cf1c4a7
Showing
9 changed files
with
419 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//go:build !go1.18 | ||
// +build !go1.18 | ||
|
||
package swag | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"go/ast" | ||
"testing" | ||
) | ||
|
||
type testLogger struct { | ||
Messages []string | ||
} | ||
|
||
func (t *testLogger) Printf(format string, v ...interface{}) { | ||
t.Messages = append(t.Messages, fmt.Sprintf(format, v...)) | ||
} | ||
|
||
func TestParametrizeStruct(t *testing.T) { | ||
t.Parallel() | ||
|
||
pd := PackagesDefinitions{ | ||
packages: make(map[string]*PackageDefinitions), | ||
} | ||
|
||
tSpec := &TypeSpecDef{ | ||
TypeSpec: &ast.TypeSpec{ | ||
Name: &ast.Ident{Name: "Field"}, | ||
Type: &ast.StructType{Struct: 100, Fields: &ast.FieldList{Opening: 101, Closing: 102}}, | ||
}, | ||
} | ||
|
||
tr := pd.parametrizeStruct(&ast.File{}, tSpec, "", false) | ||
assert.Equal(t, tr, tSpec) | ||
|
||
tr = pd.parametrizeStruct(&ast.File{}, tSpec, "", true) | ||
assert.Equal(t, tr, tSpec) | ||
} | ||
|
||
func TestParseGenericTypeExpr(t *testing.T) { | ||
t.Parallel() | ||
|
||
parser := New() | ||
logger := &testLogger{} | ||
SetDebugger(logger)(parser) | ||
|
||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.InterfaceType{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StructType{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.Ident{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StarExpr{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.SelectorExpr{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.ArrayType{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.MapType{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.FuncType{}) | ||
assert.Empty(t, logger.Messages) | ||
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.BadExpr{}) | ||
assert.NotEmpty(t, logger.Messages) | ||
} |
Oops, something went wrong.