summaryrefslogtreecommitdiffstats
path: root/pkg/utils/regexp_test.go
blob: 03afe70653333d06bccdb4760f89720e97557364 (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
package utils

import (
	"reflect"
	"regexp"
	"testing"
)

func TestFindNamedMatches(t *testing.T) {
	scenarios := []struct {
		regex    *regexp.Regexp
		input    string
		expected map[string]string
	}{
		{
			regexp.MustCompile(`^(?P<name>\w+)`),
			"hello world",
			map[string]string{
				"name": "hello",
			},
		},
		{
			regexp.MustCompile(`^https?://.*/(?P<owner>.*)/(?P<repo>.*?)(\.git)?$`),
			"https://my_username@bitbucket.org/johndoe/social_network.git",
			map[string]string{
				"owner": "johndoe",
				"repo":  "social_network",
				"":      ".git", // unnamed capture group
			},
		},
		{
			regexp.MustCompile(`(?P<owner>hello) world`),
			"yo world",
			nil,
		},
	}

	for _, scenario := range scenarios {
		actual := FindNamedMatches(scenario.regex, scenario.input)
		if !reflect.DeepEqual(actual, scenario.expected) {
			t.Errorf("FindNamedMatches(%s, %s) == %s, expected %s", scenario.regex, scenario.input, actual, scenario.expected)
		}
	}
}