2022-10-18 12:37:50 +00:00
|
|
|
package codegen_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
func TestMethodBuilderBuild_IgnoreReceiverNoReturn(t *testing.T) {
|
|
|
|
fb := codegen.MethodBuilder{
|
|
|
|
Receiver: codegen.MethodReceiver{Type: "User"},
|
|
|
|
Name: "Init",
|
|
|
|
Params: nil,
|
|
|
|
Returns: nil,
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("db"),
|
|
|
|
codegen.CallStatement{
|
|
|
|
FuncName: "Init",
|
|
|
|
Params: codegen.StatementList{
|
|
|
|
codegen.RawStatement("&User{}"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func (User) Init() {
|
|
|
|
db.Init(&User{})
|
|
|
|
}
|
|
|
|
`
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err := fb.Impl(buffer)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actual := buffer.String()
|
|
|
|
if err := testutils.ExpectMultiLineString(
|
|
|
|
expectedCode,
|
|
|
|
actual,
|
|
|
|
); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodBuilderBuild_IgnorePoinerReceiverOneReturn(t *testing.T) {
|
|
|
|
fb := codegen.MethodBuilder{
|
|
|
|
Receiver: codegen.MethodReceiver{
|
|
|
|
Type: "User",
|
|
|
|
Pointer: true,
|
|
|
|
},
|
|
|
|
Name: "Init",
|
|
|
|
Params: nil,
|
|
|
|
Returns: []code.Type{code.TypeError},
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ReturnStatement{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("db"),
|
|
|
|
codegen.CallStatement{
|
|
|
|
FuncName: "Init",
|
|
|
|
Params: codegen.StatementList{
|
|
|
|
codegen.RawStatement("&User{}"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func (*User) Init() error {
|
|
|
|
return db.Init(&User{})
|
|
|
|
}
|
|
|
|
`
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err := fb.Impl(buffer)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actual := buffer.String()
|
|
|
|
if err := testutils.ExpectMultiLineString(
|
|
|
|
expectedCode,
|
|
|
|
actual,
|
|
|
|
); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodBuilderBuild_UseReceiverMultiReturn(t *testing.T) {
|
|
|
|
fb := codegen.MethodBuilder{
|
|
|
|
Receiver: codegen.MethodReceiver{
|
|
|
|
Name: "u",
|
|
|
|
Type: "User",
|
|
|
|
},
|
|
|
|
Name: "WithAge",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "age", Type: code.TypeInt},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{code.SimpleType("User"), code.TypeError},
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.AssignStatement{
|
|
|
|
Vars: []string{"u.Age"},
|
|
|
|
Values: codegen.StatementList{
|
|
|
|
codegen.Identifier("age"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
codegen.ReturnStatement{
|
|
|
|
codegen.Identifier("u"),
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func (u User) WithAge(age int) (User, error) {
|
|
|
|
u.Age = age
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
`
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err := fb.Impl(buffer)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actual := buffer.String()
|
|
|
|
if err := testutils.ExpectMultiLineString(
|
|
|
|
expectedCode,
|
|
|
|
actual,
|
|
|
|
); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodBuilderBuild_UsePointerReceiverNoReturn(t *testing.T) {
|
|
|
|
fb := codegen.MethodBuilder{
|
|
|
|
Receiver: codegen.MethodReceiver{
|
|
|
|
Name: "u",
|
|
|
|
Type: "User",
|
|
|
|
Pointer: true,
|
|
|
|
},
|
|
|
|
Name: "SetAge",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "age", Type: code.TypeInt},
|
|
|
|
},
|
|
|
|
Returns: nil,
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.AssignStatement{
|
|
|
|
Vars: []string{"u.Age"},
|
|
|
|
Values: codegen.StatementList{
|
|
|
|
codegen.Identifier("age"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func (u *User) SetAge(age int) {
|
|
|
|
u.Age = age
|
|
|
|
}
|
|
|
|
`
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err := fb.Impl(buffer)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actual := buffer.String()
|
|
|
|
if err := testutils.ExpectMultiLineString(
|
|
|
|
expectedCode,
|
|
|
|
actual,
|
|
|
|
); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|