File: conversion_capsule_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 (128 lines) | stat: -rw-r--r-- 2,811 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
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
package convert

import (
	"fmt"
	"reflect"
	"testing"

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

func TestConvertCapsuleType(t *testing.T) {
	capTy := cty.CapsuleWithOps("test thingy", reflect.TypeOf(""), &cty.CapsuleOps{
		GoString: func(rawV interface{}) string {
			vPtr := rawV.(*string)
			return fmt.Sprintf("capTy(%q)", *vPtr)
		},
		TypeGoString: func(ty reflect.Type) string {
			return "capTy"
		},
		RawEquals: func(a, b interface{}) bool {
			aPtr := a.(*string)
			bPtr := b.(*string)
			return *aPtr == *bPtr
		},
		ConversionFrom: func(srcTy cty.Type) func(interface{}, cty.Path) (cty.Value, error) {
			if !srcTy.Equals(cty.String) {
				return nil
			}
			return func(rawV interface{}, path cty.Path) (cty.Value, error) {
				vPtr := rawV.(*string)
				return cty.StringVal(*vPtr), nil
			}
		},
		ConversionTo: func(dstTy cty.Type) func(cty.Value, cty.Path) (interface{}, error) {
			if !dstTy.Equals(cty.String) {
				return nil
			}
			return func(from cty.Value, path cty.Path) (interface{}, error) {
				s := from.AsString()
				return &s, nil
			}
		},
	})

	capVal := func(s string) cty.Value {
		return cty.CapsuleVal(capTy, &s)
	}

	tests := []struct {
		From    cty.Value
		To      cty.Type
		Want    cty.Value
		WantErr string
	}{
		{
			From: capVal("hello"),
			To:   cty.String,
			Want: cty.StringVal("hello"),
		},
		{
			From: cty.StringVal("hello"),
			To:   capTy,
			Want: capVal("hello"),
		},
		{
			From:    cty.True,
			To:      capTy,
			WantErr: `test thingy required`,
		},
		{
			From:    capVal("hello"),
			To:      cty.Bool,
			WantErr: `bool required`,
		},
		{
			From: cty.UnknownVal(capTy),
			To:   cty.String,
			Want: cty.UnknownVal(cty.String),
		},
		{
			From: cty.NullVal(capTy),
			To:   cty.String,
			Want: cty.NullVal(cty.String),
		},
		{
			From:    cty.UnknownVal(cty.Bool),
			To:      capTy,
			WantErr: `test thingy required`,
		},
		{
			From:    cty.NullVal(cty.Bool),
			To:      capTy,
			WantErr: `test thingy required`,
		},
		{
			From:    cty.UnknownVal(capTy),
			To:      cty.Bool,
			WantErr: `bool required`,
		},
		{
			From:    cty.NullVal(capTy),
			To:      cty.Bool,
			WantErr: `bool required`,
		},
	}

	for _, test := range tests {
		t.Run(fmt.Sprintf("%#v to %#v", test.From, test.To), func(t *testing.T) {
			got, err := Convert(test.From, test.To)

			if test.WantErr == "" {
				if err != nil {
					t.Fatalf("wrong error\nwant: <no error>\ngot:  %s", err)
				}
				if !test.Want.RawEquals(got) {
					t.Errorf("wrong result\nwant: %#v\ngot:  %#v", got, test.Want)
				}
			} else {
				if err == nil {
					t.Fatalf("wrong error\nwant: %s\ngot:  <no error>", test.WantErr)
				}
				if got, want := err.Error(), test.WantErr; got != want {
					t.Errorf("wrong error\nwant: %s\ngot:  %s", got, want)
				}
			}
		})
	}
}