Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cpp): add cpp filter for test value #176

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/gen/filters/filtercpp/cpp_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func ToDefaultString(prefix string, schema *model.Schema) (string, error) {
} else {
if xe.NameSpace != "" {
prefix = fmt.Sprintf("%s::", xe.NameSpace)
} else {
prefix = "" // Externs should not be prefixed with any other prefix than given in extern info.
}
text = fmt.Sprintf("%s%s()", prefix, xe.Name)
}
case model.TypeEnum:
e := schema.LookupEnum(schema.Import, schema.Type)
NameSpace := ""
NameSpace := prefix
if schema.Import != "" {
NameSpace = fmt.Sprintf("%s::%s::", common.CamelTitleCase(schema.System().Name), common.CamelTitleCase(schema.Import))
}
Expand All @@ -46,7 +48,7 @@ func ToDefaultString(prefix string, schema *model.Schema) (string, error) {
}
case model.TypeStruct:
s := schema.LookupStruct(schema.Import, schema.Type)
NameSpace := ""
NameSpace := prefix
if schema.Import != "" {
NameSpace = fmt.Sprintf("%s::%s::", common.CamelTitleCase(schema.System().Name), common.CamelTitleCase(schema.Import))
}
Expand Down
96 changes: 96 additions & 0 deletions pkg/gen/filters/filtercpp/cpp_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,99 @@ func TestDefaultSymbolsFromIdl(t *testing.T) {
}
}
}

func TestDefaultFromIdlWithPrefix(t *testing.T) {
t.Parallel()
syss := loadTestSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"test", "Test1", "propVoid", "void"},
{"test", "Test1", "propBool", "false"},
{"test", "Test1", "propInt", "0"},
{"test", "Test1", "propInt32", "0"},
{"test", "Test1", "propInt64", "0LL"},
{"test", "Test1", "propFloat", "0.0f"},
{"test", "Test1", "propFloat32", "0.0f"},
{"test", "Test1", "propFloat64", "0.0"},
{"test", "Test1", "propString", "std::string()"},
{"test", "Test1", "propBoolArray", "std::list<bool>()"},
{"test", "Test1", "propIntArray", "std::list<int>()"},
{"test", "Test1", "propInt32Array", "std::list<int32_t>()"},
{"test", "Test1", "propInt64Array", "std::list<int64_t>()"},
{"test", "Test1", "propFloatArray", "std::list<float>()"},
{"test", "Test1", "propFloat32Array", "std::list<float>()"},
{"test", "Test1", "propFloat64Array", "std::list<double>()"},
{"test", "Test1", "propStringArray", "std::list<std::string>()"},
{"test", "Test2", "propEnum", "MyPrefix::Enum1Enum::Default"},
{"test", "Test2", "propStruct", "MyPrefix::Struct1()"},
{"test", "Test2", "propInterface", "nullptr"},
{"test", "Test2", "propEnumArray", "std::list<MyPrefix::Enum1Enum>()"},
{"test", "Test2", "propStructArray", "std::list<MyPrefix::Struct1>()"},
{"test", "Test2", "propInterfaceArray", "std::list<MyPrefix::Interface1*>()"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppDefault("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}

func TestImportedDefaultParam(t *testing.T) {
syss := loadExternSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"demo", "Iface2", "prop1", "XType1()"},
{"demo", "Iface2", "prop2", "demo::x::XType2()"},
{"demo", "Iface2", "prop3", "demo::x::XtypeFactory::create()"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppDefault("", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}

func TestImportedExternDefaultPrefix(t *testing.T) {
syss := loadExternSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"demo", "Iface2", "prop1", "XType1()"},
{"demo", "Iface2", "prop2", "demo::x::XType2()"},
{"demo", "Iface2", "prop3", "demo::x::XtypeFactory::create()"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppDefault("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}
8 changes: 5 additions & 3 deletions pkg/gen/filters/filtercpp/cpp_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func ToParamString(prefix string, schema *model.Schema, name string) (string, er
xe := parseCppExtern(schema)
if xe.NameSpace != "" {
prefix = fmt.Sprintf("%s::", xe.NameSpace)
} else {
prefix = "" // Externs should not be prefixed with any other prefix than given in extern info.
}
return fmt.Sprintf("const %s%s& %s", prefix, xe.Name, name), nil
case model.TypeEnum:
e := schema.LookupEnum(schema.Import, schema.Type)
NameSpace := ""
NameSpace := prefix
if schema.Import != "" {
NameSpace = fmt.Sprintf("%s::%s::", common.CamelTitleCase(schema.System().Name), common.CamelTitleCase(schema.Import))
}
Expand All @@ -50,7 +52,7 @@ func ToParamString(prefix string, schema *model.Schema, name string) (string, er
}
case model.TypeStruct:
s := schema.LookupStruct(schema.Import, schema.Type)
NameSpace := ""
NameSpace := prefix
if schema.Import != "" {
NameSpace = fmt.Sprintf("%s::%s::", common.CamelTitleCase(schema.System().Name), common.CamelTitleCase(schema.Import))
}
Expand All @@ -59,7 +61,7 @@ func ToParamString(prefix string, schema *model.Schema, name string) (string, er
}
case model.TypeInterface:
i := schema.LookupInterface(schema.Import, schema.Type)
NameSpace := ""
NameSpace := prefix
if schema.Import != "" {
NameSpace = fmt.Sprintf("%s::%s::", common.CamelTitleCase(schema.System().Name), common.CamelTitleCase(schema.Import))
}
Expand Down
94 changes: 94 additions & 0 deletions pkg/gen/filters/filtercpp/cpp_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,97 @@ func TestParamSymbols(t *testing.T) {
}
}
}

func TestParamPrefix(t *testing.T) {
t.Parallel()
table := []struct {
mn string
in string
pn string
rt string
}{
{"test", "Test1", "propBool", "bool propBool"},
{"test", "Test1", "propInt", "int propInt"},
{"test", "Test1", "propInt32", "int32_t propInt32"},
{"test", "Test1", "propInt64", "int64_t propInt64"},
{"test", "Test1", "propFloat", "float propFloat"},
{"test", "Test1", "propFloat32", "float propFloat32"},
{"test", "Test1", "propFloat64", "double propFloat64"},
{"test", "Test1", "propString", "const std::string& propString"},
{"test", "Test1", "propBoolArray", "const std::list<bool>& propBoolArray"},
{"test", "Test1", "propIntArray", "const std::list<int>& propIntArray"},
{"test", "Test1", "propFloatArray", "const std::list<float>& propFloatArray"},
{"test", "Test1", "propStringArray", "const std::list<std::string>& propStringArray"},
{"test", "Test1", "prop_Bool", "bool prop_Bool"},
{"test", "Test1", "prop_bool", "bool prop_bool"},
{"test", "Test1", "prop_1", "bool prop_1"},
{"test", "Test2", "propEnum", "MyPrefix::Enum1Enum propEnum"},
{"test", "Test2", "propStruct", "const MyPrefix::Struct1& propStruct"},
{"test", "Test2", "propInterface", "MyPrefix::Interface1* propInterface"},
{"test", "Test2", "propEnumArray", "const std::list<MyPrefix::Enum1Enum>& propEnumArray"},
{"test", "Test2", "propStructArray", "const std::list<MyPrefix::Struct1>& propStructArray"},
{"test", "Test2", "propInterfaceArray", "const std::list<MyPrefix::Interface1*>& propInterfaceArray"},
}
syss := loadTestSystems(t)
for _, sys := range syss {
for _, tt := range table {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppParam("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}

func TestImportedExternParam(t *testing.T) {
syss := loadExternSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"demo", "Iface2", "prop1", "const XType1& prop1"},
{"demo", "Iface2", "prop2", "const demo::x::XType2& prop2"},
{"demo", "Iface2", "prop3", "const demo::x::XType3A& prop3"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppParam("", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}

func TestImportedExternParamPrefix(t *testing.T) {
syss := loadExternSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"demo", "Iface2", "prop1", "const XType1& prop1"},
{"demo", "Iface2", "prop2", "const demo::x::XType2& prop2"},
{"demo", "Iface2", "prop3", "const demo::x::XType3A& prop3"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppParam("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}
2 changes: 2 additions & 0 deletions pkg/gen/filters/filtercpp/cpp_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func ToReturnString(prefix string, schema *model.Schema) (string, error) {
xe := parseCppExtern(schema)
if xe.NameSpace != "" {
prefix = fmt.Sprintf("%s::", xe.NameSpace)
} else {
prefix = "" // Externs should not be prefixed with any other prefix than given in extern info.
}
text = fmt.Sprintf("%s%s", prefix, xe.Name)
case model.TypeEnum:
Expand Down
72 changes: 72 additions & 0 deletions pkg/gen/filters/filtercpp/cpp_return_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,75 @@ func TestImportedExternReturn(t *testing.T) {
}
}
}

func TestPrefixedReturn(t *testing.T) {
t.Parallel()
syss := loadTestSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{
{"test", "Test1", "propVoid", "void"},
{"test", "Test1", "propBool", "bool"},
{"test", "Test1", "propInt", "int"},
{"test", "Test1", "propInt32", "int32_t"},
{"test", "Test1", "propInt64", "int64_t"},
{"test", "Test1", "propFloat", "float"},
{"test", "Test1", "propFloat32", "float"},
{"test", "Test1", "propFloat64", "double"},
{"test", "Test1", "propString", "std::string"},
{"test", "Test1", "propBoolArray", "std::list<bool>"},
{"test", "Test1", "propIntArray", "std::list<int>"},
{"test", "Test1", "propInt32Array", "std::list<int32_t>"},
{"test", "Test1", "propInt64Array", "std::list<int64_t>"},
{"test", "Test1", "propFloatArray", "std::list<float>"},
{"test", "Test1", "propFloat32Array", "std::list<float>"},
{"test", "Test1", "propFloat64Array", "std::list<double>"},
{"test", "Test1", "propStringArray", "std::list<std::string>"},
{"test", "Test2", "propEnum", "MyPrefix::Enum1Enum"},
{"test", "Test2", "propStruct", "MyPrefix::Struct1"},
{"test", "Test2", "propInterface", "MyPrefix::Interface1*"},
{"test", "Test2", "propEnumArray", "std::list<MyPrefix::Enum1Enum>"},
{"test", "Test2", "propStructArray", "std::list<MyPrefix::Struct1>"},
{"test", "Test2", "propInterfaceArray", "std::list<MyPrefix::Interface1*>"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppReturn("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}

func TestPrefixedExternReturn(t *testing.T) {
syss := loadExternSystems(t)
var propTests = []struct {
mn string
in string
pn string
rt string
}{

{"demo", "Iface2", "prop1", "XType1"},
{"demo", "Iface2", "prop2", "demo::x::XType2"},
{"demo", "Iface2", "prop3", "demo::x::XType3A"},
}
for _, sys := range syss {
for _, tt := range propTests {
t.Run(tt.pn, func(t *testing.T) {
prop := sys.LookupProperty(tt.mn, tt.in, tt.pn)
assert.NotNil(t, prop)
r, err := cppReturn("MyPrefix::", prop)
assert.NoError(t, err)
assert.Equal(t, tt.rt, r)
})
}
}
}
Loading
Loading