forked from obeattie/ohmyglob
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_test.go
89 lines (76 loc) · 2.42 KB
/
utils_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
package ohmyglob
import (
"bytes"
"fmt"
"math/rand"
"regexp"
"testing"
"time"
"unicode"
"github.com/stretchr/testify/assert"
)
func TestRegexComponentEscaping(t *testing.T) {
rand.Seed(time.Now().UnixNano())
charsTested := 0
sampled := make([]rune, 10)
for _, table := range unicode.PrintRanges {
for _, charRange := range table.R16 {
for c := charRange.Lo; c <= charRange.Hi; c += charRange.Stride {
charsTested++
if rand.Float32() <= 0.05 {
sampled = append(sampled, rune(c))
}
component := escapeRegexComponent(string(c))
re := regexp.MustCompile(fmt.Sprintf("^%s$", component))
if !assert.True(t, re.MatchString(string(c))) {
return
}
}
}
}
t.Logf("Tested %d runes individually", charsTested)
// Test permutations of the sampled runes
t.Logf("Sampled %d runes", len(sampled))
permsTested := 0
for i := 0; i <= len(sampled)/2; i++ {
permsTested++
seq := rand.Perm(rand.Intn(len(sampled)))
buf := new(bytes.Buffer)
for _, idx := range seq {
buf.WriteRune(sampled[idx])
}
charSeq := buf.String()
re := regexp.MustCompile(fmt.Sprintf("^%s$", escapeRegexComponent(charSeq)))
if !assert.True(t, re.MatchString(charSeq)) {
return
}
}
t.Logf("Tested %d permutations", permsTested)
}
func TestEscapeGlobComponent(t *testing.T) {
expectations := map[string]string{
`foobar`: `foobar`,
`foo/bar`: `foo\/bar`,
`foobarbaz*/foobar`: `foobarbaz\*\/foobar`,
`.*////.**/foobar///baz`: `.\*\/\/\/\/.\*\*\/foobar\/\/\/baz`,
`\foobar`: `\\foobar`,
`/∆≈¨´∂#ª˙ƒ¨∞˙**®´∆¢#º....///..∂˚ø´∂˚®≥...`: `\/∆≈¨´∂#ª˙ƒ¨∞˙\*\*®´∆¢#º....\/\/\/..∂˚ø´∂˚®≥...`,
`!ADMIN`: `\!ADMIN`,
}
for src, result := range expectations {
assert.Equal(t, result, EscapeGlobComponent(src, DefaultOptions))
}
}
func TestEscapeGlobString(t *testing.T) {
expectations := map[string]string{
`foobar`: `foobar`,
`foo/bar`: `foo/bar`,
`foobarbaz*/foobar`: `foobarbaz\*/foobar`,
`.*////.**/foobar///baz`: `.\*////.\*\*/foobar///baz`,
`\foobar`: `\\foobar`,
`/∆≈¨´∂#ª˙ƒ¨∞˙**®´∆¢#º....///..∂˚ø´∂˚®≥...`: `/∆≈¨´∂#ª˙ƒ¨∞˙\*\*®´∆¢#º....///..∂˚ø´∂˚®≥...`,
}
for src, result := range expectations {
assert.Equal(t, result, EscapeGlobString(src, DefaultOptions))
}
}