Debian Codegraph @

internal/config/types/slice_test.go

raw
1 package types_test
2
3 import (
4 "encoding/json"
5 "regexp"
6 "strings"
7 "testing"
8
9 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.yaml.in/yaml/v3"
13 )
14
15 func TestSliceUnmarshalText(t *testing.T) {
16 t.Parallel()
17
18 slice := types.StringSlice{}
19
20 require.NoError(t, slice.UnmarshalText([]byte("a,b,c,d")))
21
22 assert.Equal(t, types.StringSlice{"a", "b", "c", "d"}, slice)
23 }
24
25 func TestSliceMarshalText(t *testing.T) {
26 t.Parallel()
27
28 slice, err := types.StringSlice{"a", "b", "c", "d"}.MarshalText()
29
30 require.NoError(t, err)
31
32 assert.Equal(t, []byte("a,b,c,d"), slice)
33 }
34
35 func TestSliceUnmarshalJSON(t *testing.T) {
36 t.Parallel()
37
38 slice := types.StringSlice{}
39
40 require.NoError(t, json.NewDecoder(strings.NewReader(`["a","b","c","d"]`)).Decode(&slice))
41
42 assert.Equal(t, types.StringSlice{"a", "b", "c", "d"}, slice)
43 }
44
45 func TestSliceUnmarshalYAML(t *testing.T) {
46 t.Parallel()
47
48 slice := types.StringSlice{}
49
50 require.NoError(t, yaml.NewDecoder(strings.NewReader("- a\n- b\n- c\n- d\n")).Decode(&slice))
51
52 assert.Equal(t, types.StringSlice{"a", "b", "c", "d"}, slice)
53 }
54
55 func TestRegexpSliceUnmarshalText(t *testing.T) {
56 t.Parallel()
57
58 slice := types.RegexpSlice{}
59
60 require.NoError(t, slice.UnmarshalText([]byte("a,b,c,d")))
61
62 //goland:noinspection RegExpUnnecessaryNonCapturingGroup
63 assert.Equal(t, types.RegexpSlice{regexp.MustCompile("^(?:a)$"), regexp.MustCompile("^(?:b)$"), regexp.MustCompile("^(?:c)$"), regexp.MustCompile("^(?:d)$")}, slice)
64 }
65
66 func TestRegexpSliceUnmarshalTextError(t *testing.T) {
67 t.Parallel()
68
69 slice := types.RegexpSlice{}
70
71 require.EqualError(t, slice.UnmarshalText([]byte("^(a,b,c,d")), "error parsing regexp: missing closing ): `^(?:^(a)$`")
72 }
73
74 func TestRegexpSliceMarshalText(t *testing.T) {
75 t.Parallel()
76
77 slice, err := types.RegexpSlice{regexp.MustCompile("a"), regexp.MustCompile("b"), regexp.MustCompile("c"), regexp.MustCompile("d")}.MarshalText()
78
79 require.NoError(t, err)
80
81 assert.Equal(t, []byte("a,b,c,d"), slice)
82 }
83
84 func TestRegexpSliceUnmarshalJSON(t *testing.T) {
85 t.Parallel()
86
87 slice := types.RegexpSlice{}
88
89 require.EqualError(t, json.NewDecoder(strings.NewReader(`["^(a","b","c","d"]`)).Decode(&slice), "error parsing regexp: missing closing ): `^(?:^(a)$`")
90 }
91
92 func TestRegexpSliceUnmarshalYAML(t *testing.T) {
93 t.Parallel()
94
95 slice := types.RegexpSlice{}
96
97 require.EqualError(t, yaml.NewDecoder(strings.NewReader("- \"^(a\"\n- b\n- c\n- d\n")).Decode(&slice), "error parsing regexp: missing closing ): `^(?:^(a)$`")
98 }