-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathfunc_when_test.go
More file actions
151 lines (137 loc) · 4.68 KB
/
Copy pathfunc_when_test.go
File metadata and controls
151 lines (137 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottlfuncs
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)
func Test_when(t *testing.T) {
asLiteralGetter := func(v any) ottl.Getter[any] {
getter, err := ottl.NewTestingLiteralGetter[any](true, ottl.StandardGetSetter[any]{
Getter: func(context.Context, any) (any, error) {
return v, nil
},
})
require.NoError(t, err)
return getter
}
tests := []struct {
name string
condition *ottl.LambdaExpression[any]
trueValue ottl.Getter[any]
falseValue ottl.Getter[any]
want any
}{
{
name: "condition true with literal values",
condition: ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return true, nil
}),
trueValue: asLiteralGetter("true"),
falseValue: asLiteralGetter("false"),
want: "true",
},
{
name: "condition false with literal values",
condition: ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return false, nil
}),
trueValue: asLiteralGetter("true"),
falseValue: asLiteralGetter("false"),
want: "false",
},
{
name: "condition true with dynamic values",
condition: ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return true, nil
}),
trueValue: &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return int64(1), nil
}},
falseValue: &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return int64(0), nil
}},
want: int64(1),
},
{
name: "condition false with dynamic values",
condition: ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return false, nil
}),
trueValue: &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return int64(1), nil
}},
falseValue: &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return int64(0), nil
}},
want: int64(0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := whenFunction(tt.condition, tt.trueValue, tt.falseValue)
got, err := exprFunc(t.Context(), nil)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func Test_when_unused_branch(t *testing.T) {
condition := ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return true, nil
})
trueValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return "true", nil
}}
falseValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return nil, errors.New("should not be reached")
}}
exprFunc := whenFunction(condition, trueValue, falseValue)
got, err := exprFunc(t.Context(), nil)
require.NoError(t, err)
assert.Equal(t, "true", got)
}
func Test_when_lambda_type_error(t *testing.T) {
condition := ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return "not a bool", nil
})
trueValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return "true", nil
}}
falseValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return "false", nil
}}
exprFunc := whenFunction(condition, trueValue, falseValue)
_, err := exprFunc(t.Context(), nil)
require.Error(t, err)
assert.ErrorContains(t, err, "lambda expression must return a value of type bool")
}
func Test_createWhenFunction(t *testing.T) {
fCtx := ottl.FunctionContext{}
condition := ottl.NewTestingLambdaExpression[any]([]string{}, func(_ context.Context, _ any, _ func(string) any) (any, error) {
return true, nil
})
trueValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return "true", nil
}}
falseValue := &ottl.StandardGetSetter[any]{Getter: func(context.Context, any) (any, error) {
return "false", nil
}}
t.Run("valid args", func(t *testing.T) {
fn, err := createWhenFunction[any](fCtx, &WhenArguments[any]{
Condition: *condition,
TrueValue: trueValue,
FalseValue: falseValue,
})
require.NoError(t, err)
require.NotNil(t, fn)
})
t.Run("invalid args type", func(t *testing.T) {
_, err := createWhenFunction[any](fCtx, &struct{}{})
assert.EqualError(t, err, "WhenFactory args must be of type *WhenArguments[K]")
})
}