forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_test.go
112 lines (96 loc) · 3.64 KB
/
strings_test.go
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
// Copyright 2019 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import "testing"
func TestBuiltinTrim(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims '!¡' from string", []string{`p[x] { x := trim("¡¡¡foo, bar!!!", "!¡") }`}, `["foo, bar"]`},
{"trims nothing from string", []string{`p[x] { x := trim("¡¡¡foo, bar!!!", "i") }`}, `["¡¡¡foo, bar!!!"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestBuiltinTrimLeft(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims leading '!¡' from string", []string{`p[x] { x := trim_left("¡¡¡foo, bar!!!", "!¡") }`}, `["foo, bar!!!"]`},
{"trims nothing from string", []string{`p[x] { x := trim_left("!!!foo, bar¡¡¡", "¡") }`}, `["!!!foo, bar¡¡¡"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestBuiltinTrimPrefix(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims prefix '!¡' from string", []string{`p[x] { x := trim_prefix("¡¡¡foo, bar!!!", "¡¡¡foo") }`}, `[", bar!!!"]`},
{"trims nothing from string", []string{`p[x] { x := trim_prefix("¡¡¡foo, bar!!!", "¡¡¡bar") }`}, `["¡¡¡foo, bar!!!"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestBuiltinTrimRight(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims trailing '!¡' from string", []string{`p[x] { x := trim_right("¡¡¡foo, bar!!!", "!¡") }`}, `["¡¡¡foo, bar"]`},
{"trims nothing from string", []string{`p[x] { x := trim_right("!!!foo, bar¡¡¡", "!") }`}, `["!!!foo, bar¡¡¡"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestBuiltinTrimSuffix(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims suffix '!¡' from string", []string{`p[x] { x := trim_suffix("¡¡¡foo, bar!!!", ", bar!!!") }`}, `["¡¡¡foo"]`},
{"trims nothing from string", []string{`p[x] { x := trim_suffix("¡¡¡foo, bar!!!", ", foo!!!") }`}, `["¡¡¡foo, bar!!!"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestBuiltinTrimSpace(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"trims all leading and trailing white space from string", []string{`p[x] { x := trim_space(" \t\n foo, bar \n\t\r\n") }`}, `["foo, bar"]`},
{"trims nothing from string", []string{`p[x] { x := trim_space("foo, bar") }`}, `["foo, bar"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}
func TestReplaceN(t *testing.T) {
tests := []struct {
note string
rules []string
expected interface{}
}{
{"replace multiple patterns", []string{`p[x] { x = strings.replace_n({"<": "<", ">": ">"}, "This is <b>HTML</b>!") }`}, `["This is <b>HTML</b>!"]`},
{"find no patterns", []string{`p[x] { x = strings.replace_n({"old1": "new1", "old2": "new2"}, "Everything is new1, new2") }`}, `["Everything is new1, new2"]`},
}
for _, tc := range tests {
runTopDownTestCase(t, map[string]interface{}{}, tc.note, tc.rules, tc.expected)
}
}