1package stdlib
2
3import (
4	"reflect"
5	"testing"
6
7	"github.com/zclconf/go-cty/cty"
8)
9
10func TestBytesLen(t *testing.T) {
11	tests := []struct {
12		Input cty.Value
13		Want  cty.Value
14	}{
15		{
16			BytesVal([]byte{}),
17			cty.NumberIntVal(0),
18		},
19		{
20			BytesVal([]byte{'a'}),
21			cty.NumberIntVal(1),
22		},
23		{
24			BytesVal([]byte{'a', 'b', 'c'}),
25			cty.NumberIntVal(3),
26		},
27	}
28
29	for _, test := range tests {
30		t.Run(test.Input.GoString(), func(t *testing.T) {
31			got, err := BytesLen(test.Input)
32
33			if err != nil {
34				t.Fatal(err)
35			}
36
37			if !got.RawEquals(test.Want) {
38				t.Errorf(
39					"wrong result\ninput: %#v\ngot:   %#v\nwant:  %#v",
40					test.Input, got, test.Want,
41				)
42			}
43		})
44	}
45}
46
47func TestBytesSlice(t *testing.T) {
48	tests := []struct {
49		Input  cty.Value
50		Offset cty.Value
51		Length cty.Value
52		Want   cty.Value
53	}{
54		{
55			BytesVal([]byte{}),
56			cty.NumberIntVal(0),
57			cty.NumberIntVal(0),
58			BytesVal([]byte{}),
59		},
60		{
61			BytesVal([]byte{'a'}),
62			cty.NumberIntVal(0),
63			cty.NumberIntVal(1),
64			BytesVal([]byte{'a'}),
65		},
66		{
67			BytesVal([]byte{'a', 'b', 'c'}),
68			cty.NumberIntVal(0),
69			cty.NumberIntVal(2),
70			BytesVal([]byte{'a', 'b'}),
71		},
72		{
73			BytesVal([]byte{'a', 'b', 'c'}),
74			cty.NumberIntVal(1),
75			cty.NumberIntVal(2),
76			BytesVal([]byte{'b', 'c'}),
77		},
78		{
79			BytesVal([]byte{'a', 'b', 'c'}),
80			cty.NumberIntVal(0),
81			cty.NumberIntVal(3),
82			BytesVal([]byte{'a', 'b', 'c'}),
83		},
84	}
85
86	for _, test := range tests {
87		t.Run(test.Input.GoString(), func(t *testing.T) {
88			got, err := BytesSlice(test.Input, test.Offset, test.Length)
89
90			if err != nil {
91				t.Fatal(err)
92			}
93
94			gotBytes := *(got.EncapsulatedValue().(*[]byte))
95			wantBytes := *(test.Want.EncapsulatedValue().(*[]byte))
96
97			if !reflect.DeepEqual(gotBytes, wantBytes) {
98				t.Errorf(
99					"wrong result\ninput: %#v, %#v,  %#v\ngot:   %#v\nwant:  %#v",
100					test.Input, test.Offset, test.Length, got, test.Want,
101				)
102			}
103		})
104	}
105}
106