summaryrefslogtreecommitdiff
path: root/regexp/regexp_test.go
blob: 5cfd9b5f69b5b405ef68113ab9a1f32d04ac64d0 (plain)
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
128
package regexp

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestFind(t *testing.T) {
	f, err := Find(`[a-z]+`, `foo bar baz`)
	require.NoError(t, err)
	assert.Equal(t, "foo", f)

	_, err = Find(`[a-`, "")
	require.Error(t, err)
}

func TestFindAll(t *testing.T) {
	_, err := FindAll(`[a-`, 42, "")
	require.Error(t, err)

	testdata := []struct {
		re       string
		in       string
		expected []string
		n        int
	}{
		{`[a-z]+`, `foo bar baz`, []string{"foo", "bar", "baz"}, -1},
		{`[a-z]+`, `foo bar baz`, nil, 0},
		{`[a-z]+`, `foo bar baz`, []string{"foo", "bar"}, 2},
		{`[a-z]+`, `foo bar baz`, []string{"foo", "bar", "baz"}, 14},
	}

	for _, d := range testdata {
		f, err := FindAll(d.re, d.n, d.in)
		require.NoError(t, err)
		assert.Equal(t, d.expected, f)
	}
}

func TestMatch(t *testing.T) {
	actual, err := Match(`^[a-z]+\[[0-9]+\]$`, "adam[23]")
	require.NoError(t, err)
	assert.True(t, actual)

	actual, err = Match(`^[a-z]+\[[0-9]+\]$`, "eve[7]")
	require.NoError(t, err)
	assert.True(t, actual)

	actual, err = Match(`^[a-z]+\[[0-9]+\]$`, "Job[48]")
	require.NoError(t, err)
	assert.False(t, actual)

	actual, err = Match(`^[a-z]+\[[0-9]+\]$`, "snakey")
	require.NoError(t, err)
	assert.False(t, actual)
}

func TestReplace(t *testing.T) {
	testdata := []struct {
		expected    string
		expression  string
		replacement string
		input       string
	}{
		{"-T-T-", "a(x*)b", "T", "-ab-axxb-"},
		{"--xx-", "a(x*)b", "$1", "-ab-axxb-"},
		{"---", "a(x*)b", "$1W", "-ab-axxb-"},
		{"-W-xxW-", "a(x*)b", "${1}W", "-ab-axxb-"},
		{"Turing, Alan", "(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)", "${last}, ${first}", "Alan Turing"},
	}
	for _, d := range testdata {
		actual, err := Replace(d.expression, d.replacement, d.input)
		require.NoError(t, err)
		assert.Equal(t, d.expected, actual)
	}
}

func TestReplaceLiteral(t *testing.T) {
	_, err := ReplaceLiteral(`[a-`, "", "")
	require.Error(t, err)

	testdata := []struct {
		expected    string
		expression  string
		replacement string
		input       string
	}{
		{"-T-T-", "a(x*)b", "T", "-ab-axxb-"},
		{"-$1-$1-", "a(x*)b", "$1", "-ab-axxb-"},
		{"-$1W-$1W-", "a(x*)b", "$1W", "-ab-axxb-"},
		{"-${1}W-${1}W-", "a(x*)b", "${1}W", "-ab-axxb-"},
		{"${last}, ${first}", "(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)", "${last}, ${first}", "Alan Turing"},
	}
	for _, d := range testdata {
		r, err := ReplaceLiteral(d.expression, d.replacement, d.input)
		require.NoError(t, err)
		assert.Equal(t, d.expected, r)
	}
}

func TestSplit(t *testing.T) {
	_, err := Split(`[a-`, 42, "")
	require.Error(t, err)

	testdata := []struct {
		re       string
		in       string
		expected []string
		n        int
	}{
		{`\s+`, "foo  bar baz\tqux", []string{"foo", "bar", "baz", "qux"}, -1},
		{`,`, `foo bar baz`, nil, 0},
		{` `, `foo bar baz`, []string{"foo", "bar baz"}, 2},
		{`[\s,.]`, `foo bar.baz,qux`, []string{"foo", "bar", "baz", "qux"}, 14},
	}

	for _, d := range testdata {
		f, err := Split(d.re, d.n, d.in)
		require.NoError(t, err)
		assert.Equal(t, d.expected, f)
	}
}

func TestQuoteMeta(t *testing.T) {
	assert.Equal(t, `foo\{\(\\`, QuoteMeta(`foo{(\`))
}