...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package common
16
17 import (
18 "encoding/json"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 "gopkg.in/yaml.v2"
24 )
25
26 func TestRegexpUnmarshal(t *testing.T) {
27 t.Run("json", func(t *testing.T) {
28 var r Regexp
29 require.NoError(t, json.Unmarshal([]byte(`"test/.*"`), &r), "failed to unmarshal json")
30
31 assert.True(t, r.Matches("test/path/to/file.txt"))
32 assert.False(t, r.Matches("something/else/path/to/file.txt"))
33 })
34
35 t.Run("jsonEmpty", func(t *testing.T) {
36 var r Regexp
37 require.NoError(t, json.Unmarshal([]byte(`""`), &r), "failed to unmarshal json")
38
39 assert.True(t, r.Matches("any string"))
40 assert.True(t, r.Matches(""))
41 })
42
43 t.Run("jsonError", func(t *testing.T) {
44 var r Regexp
45 require.Error(t, json.Unmarshal([]byte(`"this(is an unclosed [group"`), &r), "invalid regexp unmarshalled without error")
46 })
47
48 t.Run("yaml", func(t *testing.T) {
49 var r Regexp
50 require.NoError(t, yaml.Unmarshal([]byte(`"test/.*"`), &r), "failed to unmarshal yaml")
51
52 assert.True(t, r.Matches("test/path/to/file.txt"))
53 assert.False(t, r.Matches("something/else/path/to/file.txt"))
54 })
55
56 t.Run("yamlEmpty", func(t *testing.T) {
57 var r Regexp
58 require.NoError(t, yaml.Unmarshal([]byte(`""`), &r), "failed to unmarshal yaml")
59
60 assert.True(t, r.Matches("any string"))
61 assert.True(t, r.Matches(""))
62 })
63
64 t.Run("yamlError", func(t *testing.T) {
65 var r Regexp
66 require.Error(t, yaml.Unmarshal([]byte(`"this(is an unclosed [group"`), &r), "invalid regexp unmarshalled without error")
67 })
68 }
69
View as plain text