platform_build_blueprint/proptools/configurable_test.go
Cole Faust c472e38ec1
Add PostProcessors to configurable properties
Some module types currently evaluate configurable properties in load
hooks, modify the results, and pass them onto properties of other
modules. Evaluating configurable properties in load hooks is
problematic, it happens so early that we can't decide the configuration
beforehand.

Add a "post processors" mechanism to configurable properties where
the result of evaluating the property will be passed through a post
processing function before being returned from Get(). This essentially
allows you to modify the property without evaluating it.

Bug: 362579941
Test: m nothing --no-skip-soong-tests
Change-Id: Ibddb3f14b3433364ba474b964c701e8915d4dc85
2024-10-24 19:18:21 +02:00

77 lines
2 KiB
Go

package proptools
import (
"fmt"
"reflect"
"testing"
)
func TestPostProcessor(t *testing.T) {
// Same as the ascii art example in Configurable.evaluate()
prop := NewConfigurable[[]string](nil, nil)
prop.AppendSimpleValue([]string{"a"})
prop.AppendSimpleValue([]string{"b"})
prop.AddPostProcessor(addToElements("1"))
prop2 := NewConfigurable[[]string](nil, nil)
prop2.AppendSimpleValue([]string{"c"})
prop3 := NewConfigurable[[]string](nil, nil)
prop3.AppendSimpleValue([]string{"d"})
prop3.AppendSimpleValue([]string{"e"})
prop3.AddPostProcessor(addToElements("2"))
prop4 := NewConfigurable[[]string](nil, nil)
prop4.AppendSimpleValue([]string{"f"})
prop5 := NewConfigurable[[]string](nil, nil)
prop5.AppendSimpleValue([]string{"g"})
prop5.AddPostProcessor(addToElements("3"))
prop2.Append(prop3)
prop2.AddPostProcessor(addToElements("z"))
prop.Append(prop2)
prop.AddPostProcessor(addToElements("y"))
prop.Append(prop4)
prop.Append(prop5)
expected := []string{"a1y", "b1y", "czy", "d2zy", "e2zy", "f", "g3"}
x := prop.Get(&configurableEvalutorForTesting{})
if !reflect.DeepEqual(x.Get(), expected) {
t.Fatalf("Expected %v, got %v", expected, x.Get())
}
}
func addToElements(s string) func([]string) []string {
return func(arr []string) []string {
for i := range arr {
arr[i] = arr[i] + s
}
return arr
}
}
type configurableEvalutorForTesting struct {
vars map[string]string
}
func (e *configurableEvalutorForTesting) EvaluateConfiguration(condition ConfigurableCondition, property string) ConfigurableValue {
if condition.functionName != "f" {
panic("Expected functionName to be f")
}
if len(condition.args) != 1 {
panic("Expected exactly 1 arg")
}
val, ok := e.vars[condition.args[0]]
if ok {
return ConfigurableValueString(val)
}
return ConfigurableValueUndefined()
}
func (e *configurableEvalutorForTesting) PropertyErrorf(property, fmtString string, args ...interface{}) {
panic(fmt.Sprintf(fmtString, args...))
}
var _ ConfigurableEvaluator = (*configurableEvalutorForTesting)(nil)