File: slice_test.go

package info (click to toggle)
golang-github-containers-common 0.62.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,436 kB
  • sloc: makefile: 131; sh: 102
file content (127 lines) | stat: -rw-r--r-- 4,817 bytes parent folder | download | duplicates (3)
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
package attributedstring

import (
	"bytes"
	"testing"

	"github.com/BurntSushi/toml"
	"github.com/stretchr/testify/require"
)

type testConfig struct {
	Array Slice `toml:"array,omitempty"`
}

const (
	configDefault     = `array=["1", "2", "3"]`
	configAppendFront = `array=[{append=true},"4", "5", "6"]`
	configAppendMid   = `array=["7", {append=true}, "8"]`
	configAppendBack  = `array=["9", {append=true}]`
	configAppendFalse = `array=["10", {append=false}]`
)

var (
	bTrue  = true
	bFalse = false
)

func loadConfigs(configs []string) (*testConfig, error) {
	var config testConfig
	for _, c := range configs {
		if _, err := toml.Decode(c, &config); err != nil {
			return nil, err
		}
	}
	return &config, nil
}

func TestSliceLoading(t *testing.T) {
	for _, test := range []struct {
		configs                []string
		expectedValues         []string
		expectedAppend         *bool
		expectedErrorSubstring string
	}{
		// Load single configs
		{[]string{configDefault}, []string{"1", "2", "3"}, nil, ""},
		{[]string{configAppendFront}, []string{"4", "5", "6"}, &bTrue, ""},
		{[]string{configAppendMid}, []string{"7", "8"}, &bTrue, ""},
		{[]string{configAppendBack}, []string{"9"}, &bTrue, ""},
		{[]string{configAppendFalse}, []string{"10"}, &bFalse, ""},
		// Append=true
		{[]string{configDefault, configAppendFront}, []string{"1", "2", "3", "4", "5", "6"}, &bTrue, ""},
		{[]string{configAppendFront, configDefault}, []string{"4", "5", "6", "1", "2", "3"}, &bTrue, ""}, // The attribute is sticky unless explicitly being turned off in a later config
		{[]string{configAppendFront, configDefault, configAppendBack}, []string{"4", "5", "6", "1", "2", "3", "9"}, &bTrue, ""},
		// Append=false
		{[]string{configDefault, configAppendFalse}, []string{"10"}, &bFalse, ""},
		{[]string{configDefault, configAppendMid, configAppendFalse}, []string{"10"}, &bFalse, ""},
		{[]string{configDefault, configAppendFalse, configAppendMid}, []string{"10", "7", "8"}, &bTrue, ""}, // Append can be re-enabled by a later config

		// Error checks
		{[]string{`array=["1", false]`}, nil, nil, `unsupported item in attributed string slice: false`},
		{[]string{`array=["1", 42]`}, nil, nil, `unsupported item in attributed string slice: 42`}, // Stop a `int` such that it passes on 32bit as well
		{[]string{`array=["1", {foo=true}]`}, nil, nil, `unsupported key "foo" in map: `},
		{[]string{`array=["1", {append="false"}]`}, nil, nil, `unable to cast append to bool: `},
	} {
		result, err := loadConfigs(test.configs)
		if test.expectedErrorSubstring != "" {
			require.Error(t, err, "test is expected to fail: %v", test)
			require.ErrorContains(t, err, test.expectedErrorSubstring, "error does not match: %v", test)
			continue
		}
		require.NoError(t, err, "test is expected to succeed: %v", test)
		require.NotNil(t, result, "loaded config must not be nil: %v", test)
		require.Equal(t, result.Array.Values, test.expectedValues, "slices do not match: %v", test)
		require.Equal(t, result.Array.Attributes.Append, test.expectedAppend, "append field does not match: %v", test)
	}
}

func TestSliceEncoding(t *testing.T) {
	for _, test := range []struct {
		configs        []string
		marshalledData string
		expectedValues []string
		expectedAppend *bool
	}{
		{
			[]string{configDefault},
			"array = [\"1\", \"2\", \"3\"]\n",
			[]string{"1", "2", "3"},
			nil,
		},
		{
			[]string{configAppendFront},
			"array = [\"4\", \"5\", \"6\", {append = true}]\n",
			[]string{"4", "5", "6"},
			&bTrue,
		},
		{
			[]string{configAppendFront, configAppendFalse},
			"array = [\"10\", {append = false}]\n",
			[]string{"10"},
			&bFalse,
		},
	} {
		// 1) Load the configs
		result, err := loadConfigs(test.configs)
		require.NoError(t, err, "loading config must succeed")
		require.NotNil(t, result, "loaded config must not be nil")
		require.Equal(t, result.Array.Values, test.expectedValues, "slices do not match: %v", test)
		require.Equal(t, result.Array.Attributes.Append, test.expectedAppend, "append field does not match: %v", test)

		// 2) Marshal the config to emulate writing it to disk
		buf := new(bytes.Buffer)
		enc := toml.NewEncoder(buf)
		encErr := enc.Encode(result)
		require.NoError(t, encErr, "encoding config must work")
		require.Equal(t, buf.String(), test.marshalledData)

		// 3) Reload the marshaled config to make sure that data is preserved
		var reloadedConfig testConfig
		_, decErr := toml.Decode(buf.String(), &reloadedConfig)
		require.NoError(t, decErr, "loading config must succeed")
		require.NotNil(t, reloadedConfig, "re-loaded config must not be nil")
		require.Equal(t, reloadedConfig.Array.Values, test.expectedValues, "slices do not match: %v", test)
		require.Equal(t, reloadedConfig.Array.Attributes.Append, test.expectedAppend, "append field does not match: %v", test)
	}
}