2019-05-28 22:16:20 +02:00
|
|
|
package bpdoc
|
|
|
|
|
|
|
|
import (
|
2020-07-28 18:30:46 +02:00
|
|
|
"fmt"
|
2019-05-28 22:16:20 +02:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2020-11-05 02:20:11 +01:00
|
|
|
type propInfo struct {
|
|
|
|
name string
|
|
|
|
typ string
|
|
|
|
}
|
|
|
|
|
2019-05-28 22:16:20 +02:00
|
|
|
type parentProps struct {
|
|
|
|
A string
|
|
|
|
|
|
|
|
Child *childProps
|
|
|
|
|
|
|
|
Mutated *mutatedProps `blueprint:"mutated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type childProps struct {
|
|
|
|
B int
|
|
|
|
|
|
|
|
Child *grandchildProps
|
|
|
|
}
|
|
|
|
|
|
|
|
type grandchildProps struct {
|
|
|
|
C bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutatedProps struct {
|
|
|
|
D int
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNestedPropertyStructs(t *testing.T) {
|
|
|
|
parent := parentProps{Child: &childProps{Child: &grandchildProps{}}, Mutated: &mutatedProps{}}
|
|
|
|
|
|
|
|
allStructs := nestedPropertyStructs(reflect.ValueOf(parent))
|
|
|
|
|
|
|
|
// mutated shouldn't be found because it's a mutated property.
|
|
|
|
expected := []string{"child", "child.child"}
|
|
|
|
if len(allStructs) != len(expected) {
|
2020-09-25 21:49:21 +02:00
|
|
|
t.Fatalf("expected %d structs, got %d, all entries: %v",
|
2019-05-28 22:16:20 +02:00
|
|
|
len(expected), len(allStructs), allStructs)
|
|
|
|
}
|
2020-09-25 21:49:21 +02:00
|
|
|
got := []string{}
|
|
|
|
for _, s := range allStructs {
|
|
|
|
got = append(got, s.nestPoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, expected) {
|
|
|
|
t.Errorf("Expected nested properties:\n\t %q,\n but got\n\t %q", expected, got)
|
2019-05-28 22:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 18:30:46 +02:00
|
|
|
|
|
|
|
func TestAllPackages(t *testing.T) {
|
|
|
|
packages, err := AllPackages(pkgFiles, moduleTypeNameFactories, moduleTypeNamePropertyStructs)
|
|
|
|
if err != nil {
|
2020-09-25 21:49:21 +02:00
|
|
|
t.Fatalf("expected nil error for AllPackages(%v, %v, %v), got %s", pkgFiles, moduleTypeNameFactories, moduleTypeNamePropertyStructs, err)
|
2020-07-28 18:30:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if numPackages := len(packages); numPackages != 1 {
|
|
|
|
t.Errorf("Expected %d package, got %d packages %v instead", len(pkgFiles), numPackages, packages)
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg := packages[0]
|
|
|
|
|
2020-11-05 02:20:11 +01:00
|
|
|
expectedProps := map[string][]propInfo{
|
|
|
|
"bar": []propInfo{
|
|
|
|
propInfo{
|
|
|
|
name: "a",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "nested",
|
|
|
|
typ: "",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "nested.c",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "nested_struct",
|
|
|
|
typ: "structToNest",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "nested_struct.e",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "struct_has_embed",
|
|
|
|
typ: "StructWithEmbedded",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "struct_has_embed.nested_in_embedded",
|
|
|
|
typ: "structToNest",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "struct_has_embed.nested_in_embedded.e",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "struct_has_embed.f",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "list_of_ints",
|
|
|
|
typ: "list of int",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "list_of_nested",
|
|
|
|
typ: "list of structToNest",
|
|
|
|
},
|
2024-05-07 00:14:17 +02:00
|
|
|
propInfo{
|
|
|
|
name: "configurable_bool",
|
|
|
|
typ: "configurable bool",
|
|
|
|
},
|
2020-11-05 02:20:11 +01:00
|
|
|
propInfo{
|
|
|
|
name: "nested_in_other_embedded",
|
|
|
|
typ: "otherStructToNest",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "nested_in_other_embedded.g",
|
|
|
|
typ: "string",
|
|
|
|
},
|
|
|
|
propInfo{
|
|
|
|
name: "h",
|
|
|
|
typ: "string",
|
|
|
|
},
|
2020-09-25 21:49:21 +02:00
|
|
|
},
|
2020-11-05 02:20:11 +01:00
|
|
|
"foo": []propInfo{
|
|
|
|
propInfo{
|
|
|
|
name: "a",
|
|
|
|
typ: "string",
|
|
|
|
},
|
2020-09-25 21:49:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-28 18:30:46 +02:00
|
|
|
for _, m := range pkg.ModuleTypes {
|
2020-11-05 02:20:11 +01:00
|
|
|
foundProps := []propInfo{}
|
|
|
|
|
2020-07-28 18:30:46 +02:00
|
|
|
for _, p := range m.PropertyStructs {
|
2020-09-25 21:49:21 +02:00
|
|
|
nestedProps, errs := findAllProperties("", p.Properties)
|
|
|
|
foundProps = append(foundProps, nestedProps...)
|
|
|
|
for _, err := range errs {
|
2020-07-28 18:30:46 +02:00
|
|
|
t.Errorf("%s", err)
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 21:49:21 +02:00
|
|
|
if wanted, ok := expectedProps[m.Name]; ok {
|
|
|
|
if !reflect.DeepEqual(foundProps, wanted) {
|
|
|
|
t.Errorf("For %s, expected\n\t %q,\nbut got\n\t %q", m.Name, wanted, foundProps)
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 18:30:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 02:20:11 +01:00
|
|
|
func findAllProperties(prefix string, properties []Property) ([]propInfo, []error) {
|
|
|
|
foundProps := []propInfo{}
|
2020-07-28 18:30:46 +02:00
|
|
|
errs := []error{}
|
|
|
|
for _, p := range properties {
|
2020-11-05 02:20:11 +01:00
|
|
|
prop := propInfo{
|
|
|
|
name: prefix + p.Name,
|
|
|
|
typ: p.Type,
|
|
|
|
}
|
|
|
|
foundProps = append(foundProps, prop)
|
2020-07-28 18:30:46 +02:00
|
|
|
if hasTag(p.Tag, "blueprint", "mutated") {
|
2020-09-25 21:49:21 +02:00
|
|
|
err := fmt.Errorf("Property %s has `blueprint:\"mutated\" tag but should have been excluded.", p.Name)
|
2020-07-28 18:30:46 +02:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:49:21 +02:00
|
|
|
nestedProps, nestedErrs := findAllProperties(prefix+p.Name+".", p.Properties)
|
|
|
|
foundProps = append(foundProps, nestedProps...)
|
|
|
|
errs = append(errs, nestedErrs...)
|
2020-07-28 18:30:46 +02:00
|
|
|
}
|
2020-09-25 21:49:21 +02:00
|
|
|
return foundProps, errs
|
2020-07-28 18:30:46 +02:00
|
|
|
}
|