2022-10-18 12:37:50 +00:00
|
|
|
package codegen_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-05-24 11:04:29 +00:00
|
|
|
"git.kmsign.ru/royalcat/repogen/internal/code"
|
|
|
|
"git.kmsign.ru/royalcat/repogen/internal/codegen"
|
|
|
|
"git.kmsign.ru/royalcat/repogen/internal/testutils"
|
2022-10-18 12:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const expectedBuildCode = `// Code generated by repogen. DO NOT EDIT.
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "context"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
_ "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
ID primitive.ObjectID ` + "`bson:\"id\" json:\"id,omitempty\"`" + `
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUser(username string) User {
|
|
|
|
return User{
|
|
|
|
ID: primitive.NewObjectID(),
|
|
|
|
Username: username,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) IDHex() string {
|
|
|
|
return u.ID.Hex()
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestBuilderBuild(t *testing.T) {
|
|
|
|
builder := codegen.NewBuilder("repogen", "user", [][]code.Import{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Name: "_",
|
|
|
|
Path: "context",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Path: "go.mongodb.org/mongo-driver/bson/primitive",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "_",
|
|
|
|
Path: "go.mongodb.org/mongo-driver/mongo/options",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
builder.AddImplementer(codegen.StructBuilder{
|
|
|
|
Name: "User",
|
|
|
|
Fields: code.StructFields{
|
|
|
|
{
|
|
|
|
Name: "ID",
|
|
|
|
Type: code.ExternalType{
|
|
|
|
PackageAlias: "primitive",
|
|
|
|
Name: "ObjectID",
|
|
|
|
},
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"id" json:"id,omitempty"`,
|
2022-10-18 12:37:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Username",
|
|
|
|
Type: code.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
builder.AddImplementer(codegen.FunctionBuilder{
|
|
|
|
Name: "NewUser",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "username", Type: code.TypeString},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{code.SimpleType("User")},
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ReturnStatement{
|
|
|
|
codegen.StructStatement{
|
|
|
|
Type: "User",
|
|
|
|
Pairs: []codegen.StructFieldPair{
|
|
|
|
{
|
|
|
|
Key: "ID",
|
|
|
|
Value: codegen.ChainStatement{
|
|
|
|
codegen.Identifier("primitive"),
|
|
|
|
codegen.CallStatement{
|
|
|
|
FuncName: "NewObjectID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "Username",
|
|
|
|
Value: codegen.Identifier("username"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
})
|
|
|
|
builder.AddImplementer(codegen.MethodBuilder{
|
|
|
|
Receiver: codegen.MethodReceiver{Name: "u", Type: code.SimpleType("User")},
|
|
|
|
Name: "IDHex",
|
|
|
|
Params: nil,
|
|
|
|
Returns: []code.Type{code.TypeString},
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ReturnStatement{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("u"),
|
|
|
|
codegen.Identifier("ID"),
|
|
|
|
codegen.CallStatement{FuncName: "Hex"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
generatedCode, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := testutils.ExpectMultiLineString(expectedBuildCode, generatedCode); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|