-
Notifications
You must be signed in to change notification settings - Fork 5
/
env_test.go
86 lines (76 loc) · 1.83 KB
/
env_test.go
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
package main
import (
"testing"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/resource"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/protobuf/testing/protocmp"
)
func TestFromValueRef(t *testing.T) {
type args struct {
req *fnv1beta1.RunFunctionRequest
path string
}
type want struct {
result string
err error
}
cases := map[string]struct {
reason string
args args
want want
}{
"FromCompositeValid": {
reason: "If composite path is valid, it should be returned.",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Observed: &fnv1beta1.State{
Composite: &fnv1beta1.Resource{
Resource: resource.MustStructJSON(`{
"apiVersion": "",
"kind": "",
"spec": {
"foo": "bar"
}
}`),
},
},
},
path: "spec.foo",
},
want: want{
result: "bar",
err: nil,
},
},
"FromContextValid": {
reason: "If composite path is valid, it should be returned.",
args: args{
req: &fnv1beta1.RunFunctionRequest{
Context: resource.MustStructJSON(`{
"apiextensions.crossplane.io/foo": {
"bar": "baz"
}
}`),
},
path: "context[apiextensions.crossplane.io/foo].bar",
},
want: want{
result: "baz",
err: nil,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
result, err := fromValueRef(tc.args.req, tc.args.path)
if diff := cmp.Diff(tc.want.result, result, protocmp.Transform()); diff != "" {
t.Errorf("%s\nf.RunFunction(...): -want rsp, +got rsp:\n%s", tc.reason, diff)
}
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {
t.Errorf("%s\nf.RunFunction(...): -want err, +got err:\n%s", tc.reason, diff)
}
})
}
}