65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package codegen_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
|
"git.kmsign.com/royalcat/repogen/internal/codegen"
|
|
"git.kmsign.com/royalcat/repogen/internal/testutils"
|
|
)
|
|
|
|
const expectedStructBuilderCode = `
|
|
type User struct {
|
|
ID primitive.ObjectID ` + "`bson:\"id,omitempty\" json:\"id,omitempty\"`" + `
|
|
Username string ` + "`bson:\"username\" json:\"username\"`" + `
|
|
Age int ` + "`bson:\"age\"`" + `
|
|
orderCount *int
|
|
}
|
|
`
|
|
|
|
func TestStructBuilderBuild(t *testing.T) {
|
|
sb := codegen.StructBuilder{
|
|
Name: "User",
|
|
Fields: []code.StructField{
|
|
{
|
|
Name: "ID",
|
|
Type: code.ExternalType{
|
|
PackageAlias: "primitive",
|
|
Name: "ObjectID",
|
|
},
|
|
Tag: `bson:"id,omitempty" json:"id,omitempty"`,
|
|
},
|
|
{
|
|
Name: "Username",
|
|
Type: code.TypeString,
|
|
Tag: `bson:"username" json:"username"`,
|
|
},
|
|
{
|
|
Name: "Age",
|
|
Type: code.TypeInt,
|
|
Tag: `bson:"age"`,
|
|
},
|
|
{
|
|
Name: "orderCount",
|
|
Type: code.PointerType{
|
|
ContainedType: code.TypeInt,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
buffer := new(bytes.Buffer)
|
|
|
|
err := sb.Impl(buffer)
|
|
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actual := buffer.String()
|
|
if err := testutils.ExpectMultiLineString(
|
|
expectedStructBuilderCode,
|
|
actual,
|
|
); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|