File: unpredictable_test.go

package info (click to toggle)
golang-github-zclconf-go-cty 1.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,464 kB
  • sloc: makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,668 bytes parent folder | download | duplicates (2)
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
package function

import (
	"testing"

	"github.com/zclconf/go-cty/cty"
)

func TestUnpredictable(t *testing.T) {
	f := New(&Spec{
		Params: []Parameter{
			{
				Name: "fixed",
				Type: cty.Bool,
			},
		},
		VarParam: &Parameter{
			Name: "variadic",
			Type: cty.String,
		},
		Type: func(args []cty.Value) (cty.Type, error) {
			if len(args) == 1 {
				return cty.Bool, nil
			} else {
				return cty.String, nil
			}
		},
		Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
			return cty.NullVal(retType), nil
		},
	})

	uf := Unpredictable(f)

	{
		predVal, err := f.Call([]cty.Value{cty.True})
		if err != nil {
			t.Fatal(err)
		}
		if !predVal.RawEquals(cty.NullVal(cty.Bool)) {
			t.Fatal("wrong predictable result")
		}
	}

	t.Run("argument type error", func(t *testing.T) {
		_, err := uf.Call([]cty.Value{cty.StringVal("hello")})
		if err == nil {
			t.Fatal("call successful; want error")
		}
	})

	t.Run("type check 1", func(t *testing.T) {
		ty, err := uf.ReturnTypeForValues([]cty.Value{cty.True})
		if err != nil {
			t.Fatal(err)
		}
		if !ty.Equals(cty.Bool) {
			t.Errorf("wrong type %#v; want %#v", ty, cty.Bool)
		}
	})

	t.Run("type check 2", func(t *testing.T) {
		ty, err := uf.ReturnTypeForValues([]cty.Value{cty.True, cty.StringVal("hello")})
		if err != nil {
			t.Fatal(err)
		}
		if !ty.Equals(cty.String) {
			t.Errorf("wrong type %#v; want %#v", ty, cty.String)
		}
	})

	t.Run("call", func(t *testing.T) {
		v, err := uf.Call([]cty.Value{cty.True})
		if err != nil {
			t.Fatal(err)
		}
		if !v.RawEquals(cty.UnknownVal(cty.Bool)) {
			t.Errorf("wrong result %#v; want %#v", v, cty.UnknownVal(cty.Bool))
		}
	})

}