File: bytes_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 (105 lines) | stat: -rw-r--r-- 1,929 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
package stdlib

import (
	"reflect"
	"testing"

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

func TestBytesLen(t *testing.T) {
	tests := []struct {
		Input cty.Value
		Want  cty.Value
	}{
		{
			BytesVal([]byte{}),
			cty.NumberIntVal(0),
		},
		{
			BytesVal([]byte{'a'}),
			cty.NumberIntVal(1),
		},
		{
			BytesVal([]byte{'a', 'b', 'c'}),
			cty.NumberIntVal(3),
		},
	}

	for _, test := range tests {
		t.Run(test.Input.GoString(), func(t *testing.T) {
			got, err := BytesLen(test.Input)

			if err != nil {
				t.Fatal(err)
			}

			if !got.RawEquals(test.Want) {
				t.Errorf(
					"wrong result\ninput: %#v\ngot:   %#v\nwant:  %#v",
					test.Input, got, test.Want,
				)
			}
		})
	}
}

func TestBytesSlice(t *testing.T) {
	tests := []struct {
		Input  cty.Value
		Offset cty.Value
		Length cty.Value
		Want   cty.Value
	}{
		{
			BytesVal([]byte{}),
			cty.NumberIntVal(0),
			cty.NumberIntVal(0),
			BytesVal([]byte{}),
		},
		{
			BytesVal([]byte{'a'}),
			cty.NumberIntVal(0),
			cty.NumberIntVal(1),
			BytesVal([]byte{'a'}),
		},
		{
			BytesVal([]byte{'a', 'b', 'c'}),
			cty.NumberIntVal(0),
			cty.NumberIntVal(2),
			BytesVal([]byte{'a', 'b'}),
		},
		{
			BytesVal([]byte{'a', 'b', 'c'}),
			cty.NumberIntVal(1),
			cty.NumberIntVal(2),
			BytesVal([]byte{'b', 'c'}),
		},
		{
			BytesVal([]byte{'a', 'b', 'c'}),
			cty.NumberIntVal(0),
			cty.NumberIntVal(3),
			BytesVal([]byte{'a', 'b', 'c'}),
		},
	}

	for _, test := range tests {
		t.Run(test.Input.GoString(), func(t *testing.T) {
			got, err := BytesSlice(test.Input, test.Offset, test.Length)

			if err != nil {
				t.Fatal(err)
			}

			gotBytes := *(got.EncapsulatedValue().(*[]byte))
			wantBytes := *(test.Want.EncapsulatedValue().(*[]byte))

			if !reflect.DeepEqual(gotBytes, wantBytes) {
				t.Errorf(
					"wrong result\ninput: %#v, %#v,  %#v\ngot:   %#v\nwant:  %#v",
					test.Input, test.Offset, test.Length, got, test.Want,
				)
			}
		})
	}
}