2022-10-18 12:37:50 +00:00
|
|
|
package codegen_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/codegen"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/testutils"
|
2022-10-18 12:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFunctionBuilderBuild_NoReturn(t *testing.T) {
|
|
|
|
fb := codegen.FunctionBuilder{
|
|
|
|
Name: "init",
|
|
|
|
Params: nil,
|
|
|
|
Returns: nil,
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("logrus"),
|
|
|
|
codegen.CallStatement{
|
|
|
|
FuncName: "SetLevel",
|
|
|
|
Params: codegen.StatementList{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("logrus"),
|
|
|
|
codegen.Identifier("DebugLevel"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
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 TestFunctionBuilderBuild_OneReturn(t *testing.T) {
|
|
|
|
fb := codegen.FunctionBuilder{
|
|
|
|
Name: "NewUser",
|
|
|
|
Params: []code.Param{
|
|
|
|
{
|
|
|
|
Name: "username",
|
|
|
|
Type: code.TypeString,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "age",
|
|
|
|
Type: code.TypeInt,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "parent",
|
|
|
|
Type: code.PointerType{ContainedType: code.SimpleType("User")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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: "Username", Value: codegen.Identifier("username")},
|
|
|
|
{Key: "Age", Value: codegen.Identifier("age")},
|
|
|
|
{Key: "Parent", Value: codegen.Identifier("parent")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func NewUser(username string, age int, parent *User) User {
|
|
|
|
return User{
|
|
|
|
Username: username,
|
|
|
|
Age: age,
|
2023-04-18 13:21:46 +00:00
|
|
|
Parent: parent,
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
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 TestFunctionBuilderBuild_MultiReturn(t *testing.T) {
|
|
|
|
fb := codegen.FunctionBuilder{
|
|
|
|
Name: "Save",
|
|
|
|
Params: []code.Param{
|
|
|
|
{
|
|
|
|
Name: "user",
|
|
|
|
Type: code.SimpleType("User"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.SimpleType("User"),
|
|
|
|
code.TypeError,
|
|
|
|
},
|
2023-04-18 13:21:46 +00:00
|
|
|
Body: codegen.FunctionBody{
|
|
|
|
codegen.ReturnStatement{
|
|
|
|
codegen.ChainStatement{
|
|
|
|
codegen.Identifier("collection"),
|
|
|
|
codegen.CallStatement{
|
|
|
|
FuncName: "Save",
|
|
|
|
Params: codegen.StatementList{
|
|
|
|
codegen.Identifier("user"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
expectedCode := `
|
|
|
|
func Save(user User) (User, error) {
|
|
|
|
return collection.Save(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)
|
|
|
|
}
|
|
|
|
}
|