summaryrefslogtreecommitdiff
path: root/regexp/regexp_test.go
blob: 89f6cb8651d9f2f7d074d47c3a79d35a041cd87f (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
package regexp

import (
	"testing"

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

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

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

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

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

	for _, d := range testdata {
		f, err := FindAll(d.re, d.n, d.in)
		assert.NoError(t, err)
		assert.EqualValues(t, d.expected, f)
	}
}
func TestMatch(t *testing.T) {
	assert.True(t, Match(`^[a-z]+\[[0-9]+\]$`, "adam[23]"))
	assert.True(t, Match(`^[a-z]+\[[0-9]+\]$`, "eve[7]"))
	assert.False(t, Match(`^[a-z]+\[[0-9]+\]$`, "Job[48]"))
	assert.False(t, Match(`^[a-z]+\[[0-9]+\]$`, "snakey"))
}

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 {
		assert.Equal(t, d.expected, Replace(d.expression, d.replacement, d.input))
	}
}

func TestReplaceLiteral(t *testing.T) {
	_, err := ReplaceLiteral(`[a-`, "", "")
	assert.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)
		assert.NoError(t, err)
		assert.Equal(t, d.expected, r)
	}
}

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

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

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