2021-01-14 16:01:00 +00:00
|
|
|
package code_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestCase struct {
|
|
|
|
Name string
|
|
|
|
Source string
|
|
|
|
ExpectedOutput code.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractComponents(t *testing.T) {
|
|
|
|
testTable := []TestCase{
|
|
|
|
{
|
|
|
|
Name: "package name",
|
|
|
|
Source: `package user`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "single line imports",
|
|
|
|
Source: `package user
|
|
|
|
|
|
|
|
import ctx "context"
|
|
|
|
import "go.mongodb.org/mongo-driver/bson/primitive"`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
|
|
|
Imports: []code.Import{
|
|
|
|
{Name: "ctx", Path: "context"},
|
|
|
|
{Path: "go.mongodb.org/mongo-driver/bson/primitive"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "multiple line imports",
|
|
|
|
Source: `package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
ctx "context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
)`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
|
|
|
Imports: []code.Import{
|
|
|
|
{Name: "ctx", Path: "context"},
|
|
|
|
{Path: "go.mongodb.org/mongo-driver/bson/primitive"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "struct declaration",
|
|
|
|
Source: `package user
|
|
|
|
|
|
|
|
type UserModel struct {
|
|
|
|
ID primitive.ObjectID ` + "`bson:\"_id,omitempty\" json:\"id\"`" + `
|
|
|
|
Username string ` + "`bson:\"username\" json:\"username\"`" + `
|
2023-04-19 12:42:08 +00:00
|
|
|
Password string ` + "`bson:\"password\" json:\"-\" note:\"This should be hidden.\"`" + `
|
2021-01-14 16:01:00 +00:00
|
|
|
}`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
2022-10-17 10:57:49 +00:00
|
|
|
Structs: []code.Struct{
|
|
|
|
{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "UserModel",
|
|
|
|
Fields: code.StructFields{
|
2022-05-16 15:37:03 +00:00
|
|
|
code.StructField{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "ID",
|
|
|
|
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"_id,omitempty" json:"id"`,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
2022-05-16 15:37:03 +00:00
|
|
|
code.StructField{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "Username",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeString,
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"username" json:"username"`,
|
|
|
|
},
|
|
|
|
code.StructField{
|
|
|
|
Name: "Password",
|
|
|
|
Type: code.TypeString,
|
|
|
|
Tag: `bson:"password" json:"-" note:"This should be hidden."`,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "interface declaration",
|
|
|
|
Source: `package user
|
|
|
|
|
|
|
|
type UserRepository interface {
|
2023-04-14 04:16:51 +00:00
|
|
|
FindByID(ctx context.Context, id primitive.ObjectID) (*UserModel, error)
|
2021-01-14 16:01:00 +00:00
|
|
|
FindAll(context.Context) ([]*UserModel, error)
|
2021-01-24 08:31:21 +00:00
|
|
|
FindByAgeBetween(ctx context.Context, fromAge, toAge int) ([]*UserModel, error)
|
2021-02-01 14:39:20 +00:00
|
|
|
InsertOne(ctx context.Context, user *UserModel) (interface{}, error)
|
2021-11-27 04:54:18 +00:00
|
|
|
UpdateAgreementByID(ctx context.Context, agreement map[string]bool, id primitive.ObjectID) (bool, error)
|
2022-05-16 15:37:03 +00:00
|
|
|
// CustomMethod does custom things.
|
2021-02-01 14:39:20 +00:00
|
|
|
CustomMethod(interface {
|
|
|
|
Run(arg1 int)
|
|
|
|
}) interface {
|
|
|
|
Do(arg2 string)
|
|
|
|
}
|
2021-01-14 16:01:00 +00:00
|
|
|
}`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
2022-10-17 10:57:49 +00:00
|
|
|
Interfaces: []code.InterfaceType{
|
|
|
|
{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "UserRepository",
|
|
|
|
Methods: []code.Method{
|
|
|
|
{
|
2023-04-14 04:16:51 +00:00
|
|
|
Name: "FindByID",
|
2021-01-14 16:01:00 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
|
|
|
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FindAll",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
2022-11-09 04:21:11 +00:00
|
|
|
code.ArrayType{
|
|
|
|
ContainedType: code.PointerType{
|
|
|
|
ContainedType: code.SimpleType("UserModel"),
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-24 08:31:21 +00:00
|
|
|
{
|
|
|
|
Name: "FindByAgeBetween",
|
|
|
|
Params: []code.Param{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
Name: "ctx",
|
|
|
|
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "fromAge",
|
|
|
|
Type: code.TypeInt,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "toAge",
|
|
|
|
Type: code.TypeInt,
|
|
|
|
},
|
2021-01-24 08:31:21 +00:00
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
2022-11-09 04:21:11 +00:00
|
|
|
code.ArrayType{
|
|
|
|
ContainedType: code.PointerType{
|
|
|
|
ContainedType: code.SimpleType("UserModel"),
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-24 08:31:21 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-01 14:39:20 +00:00
|
|
|
{
|
|
|
|
Name: "InsertOne",
|
|
|
|
Params: []code.Param{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
Name: "ctx",
|
|
|
|
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "user",
|
|
|
|
Type: code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
|
|
|
},
|
2021-02-01 14:39:20 +00:00
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.InterfaceType{},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-02-01 14:39:20 +00:00
|
|
|
},
|
|
|
|
},
|
2021-11-27 04:54:18 +00:00
|
|
|
{
|
|
|
|
Name: "UpdateAgreementByID",
|
|
|
|
Params: []code.Param{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
Name: "ctx",
|
|
|
|
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "agreement",
|
|
|
|
Type: code.MapType{KeyType: code.TypeString, ValueType: code.TypeBool},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "id",
|
|
|
|
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
|
|
|
|
},
|
2021-11-27 04:54:18 +00:00
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeBool,
|
|
|
|
code.TypeError,
|
2021-11-27 04:54:18 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-01 14:39:20 +00:00
|
|
|
{
|
2022-05-16 15:37:03 +00:00
|
|
|
Name: "CustomMethod",
|
|
|
|
Comments: []string{"CustomMethod does custom things."},
|
2021-02-01 14:39:20 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{
|
|
|
|
Type: code.InterfaceType{
|
|
|
|
Methods: []code.Method{
|
|
|
|
{
|
|
|
|
Name: "Run",
|
|
|
|
Params: []code.Param{
|
2022-10-18 12:37:50 +00:00
|
|
|
{Name: "arg1", Type: code.TypeInt},
|
2021-02-01 14:39:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.InterfaceType{
|
|
|
|
Methods: []code.Method{
|
|
|
|
{
|
|
|
|
Name: "Do",
|
|
|
|
Params: []code.Param{
|
2022-10-18 12:37:50 +00:00
|
|
|
{Name: "arg2", Type: code.TypeString},
|
2021-02-01 14:39:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "integration",
|
|
|
|
Source: `package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserModel struct {
|
|
|
|
ID primitive.ObjectID ` + "`bson:\"_id,omitempty\" json:\"id\"`" + `
|
|
|
|
Username string ` + "`bson:\"username\" json:\"username\"`" + `
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserRepository interface {
|
2023-04-14 04:16:51 +00:00
|
|
|
FindByID(ctx context.Context, id primitive.ObjectID) (*UserModel, error)
|
2021-01-14 16:01:00 +00:00
|
|
|
FindAll(ctx context.Context) ([]*UserModel, error)
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
ExpectedOutput: code.File{
|
|
|
|
PackageName: "user",
|
|
|
|
Imports: []code.Import{
|
|
|
|
{Path: "context"},
|
|
|
|
{Path: "go.mongodb.org/mongo-driver/bson/primitive"},
|
|
|
|
},
|
2022-10-17 10:57:49 +00:00
|
|
|
Structs: []code.Struct{
|
|
|
|
{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "UserModel",
|
|
|
|
Fields: code.StructFields{
|
2022-05-16 15:37:03 +00:00
|
|
|
code.StructField{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "ID",
|
|
|
|
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"_id,omitempty" json:"id"`,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
2022-05-16 15:37:03 +00:00
|
|
|
code.StructField{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "Username",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeString,
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"username" json:"username"`,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-17 10:57:49 +00:00
|
|
|
Interfaces: []code.InterfaceType{
|
|
|
|
{
|
2021-01-14 16:01:00 +00:00
|
|
|
Name: "UserRepository",
|
|
|
|
Methods: []code.Method{
|
|
|
|
{
|
2023-04-14 04:16:51 +00:00
|
|
|
Name: "FindByID",
|
2021-01-14 16:01:00 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
|
|
|
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FindAll",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
2022-11-09 04:21:11 +00:00
|
|
|
code.ArrayType{
|
|
|
|
ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-14 16:01:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testTable {
|
|
|
|
t.Run(testCase.Name, func(t *testing.T) {
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
f, _ := parser.ParseFile(fset, "", testCase.Source, parser.ParseComments)
|
|
|
|
|
|
|
|
file := code.ExtractComponents(f)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(file, testCase.ExpectedOutput) {
|
2021-03-31 11:44:31 +00:00
|
|
|
t.Errorf("Expected = %+v\nReceived = %+v", testCase.ExpectedOutput, file)
|
2021-01-14 16:01:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|