xref: /aosp_15_r20/build/blueprint/proptools/configurable_test.go (revision 1fa6dee971e1612fa5cc0aa5ca2d35a22e2c34a3)
1package proptools
2
3import (
4	"fmt"
5	"reflect"
6	"testing"
7)
8
9func TestPostProcessor(t *testing.T) {
10	// Same as the ascii art example in Configurable.evaluate()
11	prop := NewConfigurable[[]string](nil, nil)
12	prop.AppendSimpleValue([]string{"a"})
13	prop.AppendSimpleValue([]string{"b"})
14	prop.AddPostProcessor(addToElements("1"))
15
16	prop2 := NewConfigurable[[]string](nil, nil)
17	prop2.AppendSimpleValue([]string{"c"})
18
19	prop3 := NewConfigurable[[]string](nil, nil)
20	prop3.AppendSimpleValue([]string{"d"})
21	prop3.AppendSimpleValue([]string{"e"})
22	prop3.AddPostProcessor(addToElements("2"))
23
24	prop4 := NewConfigurable[[]string](nil, nil)
25	prop4.AppendSimpleValue([]string{"f"})
26
27	prop5 := NewConfigurable[[]string](nil, nil)
28	prop5.AppendSimpleValue([]string{"g"})
29	prop5.AddPostProcessor(addToElements("3"))
30
31	prop2.Append(prop3)
32	prop2.AddPostProcessor(addToElements("z"))
33
34	prop.Append(prop2)
35	prop.AddPostProcessor(addToElements("y"))
36	prop.Append(prop4)
37	prop.Append(prop5)
38
39	expected := []string{"a1y", "b1y", "czy", "d2zy", "e2zy", "f", "g3"}
40	x := prop.Get(&configurableEvalutorForTesting{})
41	if !reflect.DeepEqual(x.Get(), expected) {
42		t.Fatalf("Expected %v, got %v", expected, x.Get())
43	}
44}
45
46func TestPostProcessorWhenPassedToHelperFunction(t *testing.T) {
47	prop := NewConfigurable[[]string](nil, nil)
48	prop.AppendSimpleValue([]string{"a"})
49	prop.AppendSimpleValue([]string{"b"})
50
51	helper := func(p Configurable[[]string]) {
52		p.AddPostProcessor(addToElements("1"))
53	}
54
55	helper(prop)
56
57	expected := []string{"a1", "b1"}
58	x := prop.Get(&configurableEvalutorForTesting{})
59	if !reflect.DeepEqual(x.Get(), expected) {
60		t.Fatalf("Expected %v, got %v", expected, x.Get())
61	}
62}
63
64func addToElements(s string) func([]string) []string {
65	return func(arr []string) []string {
66		for i := range arr {
67			arr[i] = arr[i] + s
68		}
69		return arr
70	}
71}
72
73type configurableEvalutorForTesting struct {
74	vars map[string]string
75}
76
77func (e *configurableEvalutorForTesting) EvaluateConfiguration(condition ConfigurableCondition, property string) ConfigurableValue {
78	if condition.functionName != "f" {
79		panic("Expected functionName to be f")
80	}
81	if len(condition.args) != 1 {
82		panic("Expected exactly 1 arg")
83	}
84	val, ok := e.vars[condition.args[0]]
85	if ok {
86		return ConfigurableValueString(val)
87	}
88	return ConfigurableValueUndefined()
89}
90
91func (e *configurableEvalutorForTesting) PropertyErrorf(property, fmtString string, args ...interface{}) {
92	panic(fmt.Sprintf(fmtString, args...))
93}
94
95var _ ConfigurableEvaluator = (*configurableEvalutorForTesting)(nil)
96