repogen/internal/code/models_test.go

174 lines
4 KiB
Go
Raw Normal View History

2021-01-22 02:56:30 +00:00
package code_test
import (
"reflect"
"testing"
2023-05-24 11:01:50 +00:00
"git.kmsign.com/royalcat/repogen/internal/code"
2021-01-22 02:56:30 +00:00
)
func TestStructFieldsByName(t *testing.T) {
idField := code.StructField{Name: "ID", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}}
usernameField := code.StructField{Name: "Username", Type: code.TypeString}
2021-01-22 02:56:30 +00:00
fields := code.StructFields{idField, usernameField}
t.Run("struct field found", func(t *testing.T) {
field, ok := fields.ByName("Username")
if !ok {
t.Fail()
}
if !reflect.DeepEqual(field, usernameField) {
2021-03-31 11:44:31 +00:00
t.Errorf("Expected = %+v\nReceived = %+v", usernameField, field)
2021-01-22 02:56:30 +00:00
}
})
t.Run("struct field not found", func(t *testing.T) {
_, ok := fields.ByName("Password")
if ok {
t.Fail()
}
})
}
2021-02-01 14:39:20 +00:00
type TypeCodeTestCase struct {
Name string
Type code.Type
ExpectedCode string
}
2021-05-03 07:12:58 +00:00
func TestTypeCode(t *testing.T) {
2021-02-01 14:39:20 +00:00
testTable := []TypeCodeTestCase{
{
Name: "simple type",
Type: code.SimpleType("UserModel"),
ExpectedCode: "UserModel",
},
{
Name: "external type",
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
ExpectedCode: "context.Context",
},
{
Name: "pointer type",
Type: code.PointerType{ContainedType: code.SimpleType("UserModel")},
ExpectedCode: "*UserModel",
},
{
Name: "array type",
Type: code.ArrayType{ContainedType: code.SimpleType("UserModel")},
ExpectedCode: "[]UserModel",
},
{
Name: "map type",
Type: code.MapType{
KeyType: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
ValueType: code.PointerType{ContainedType: code.SimpleType("UserModel")},
},
ExpectedCode: "map[primitive.ObjectID]*UserModel",
},
2021-02-01 14:39:20 +00:00
}
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
code := testCase.Type.Code()
if code != testCase.ExpectedCode {
2021-03-31 11:44:31 +00:00
t.Errorf("Expected = %+v\nReceived = %+v", testCase.ExpectedCode, code)
2021-02-01 14:39:20 +00:00
}
})
}
}
2021-05-03 07:12:58 +00:00
type TypeIsNumberTestCase struct {
Name string
Type code.Type
IsNumber bool
}
func TestTypeIsNumber(t *testing.T) {
testTable := []TypeIsNumberTestCase{
{
Name: "simple type: int",
Type: code.TypeInt,
2021-05-03 07:12:58 +00:00
IsNumber: true,
},
{
Name: "simple type: other integer variants",
Type: code.SimpleType("int64"),
IsNumber: true,
},
{
Name: "simple type: uint",
Type: code.SimpleType("uint"),
IsNumber: true,
},
{
Name: "simple type: other unsigned integer variants",
Type: code.SimpleType("uint64"),
IsNumber: true,
},
{
Name: "simple type: float32",
Type: code.SimpleType("float32"),
IsNumber: true,
},
{
Name: "simple type: other float variants",
Type: code.TypeFloat64,
2021-05-03 07:12:58 +00:00
IsNumber: true,
},
{
Name: "simple type: non-number primitive type",
Type: code.TypeString,
2021-05-03 07:12:58 +00:00
IsNumber: false,
},
{
Name: "simple type: non-number custom type",
Type: code.SimpleType("UserModel"),
IsNumber: false,
},
{
Name: "external type",
Type: code.ExternalType{PackageAlias: "context", Name: "Context"},
IsNumber: false,
},
{
Name: "pointer type: number",
Type: code.PointerType{ContainedType: code.TypeInt},
2021-05-03 07:12:58 +00:00
IsNumber: true,
},
{
Name: "pointer type: non-number",
Type: code.PointerType{ContainedType: code.TypeString},
2021-05-03 07:12:58 +00:00
IsNumber: false,
},
{
Name: "array type",
Type: code.ArrayType{ContainedType: code.TypeInt},
2021-05-03 07:12:58 +00:00
IsNumber: false,
},
{
Name: "map type",
Type: code.MapType{KeyType: code.TypeInt, ValueType: code.TypeFloat64},
IsNumber: false,
},
2021-05-03 07:12:58 +00:00
{
Name: "interface type",
Type: code.InterfaceType{},
IsNumber: false,
},
}
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
isNumber := testCase.Type.IsNumber()
if isNumber != testCase.IsNumber {
t.Errorf("Expected = %+v\nReceived = %+v", testCase.IsNumber, isNumber)
}
})
}
}