Skip to content
Snippets Groups Projects
Commit 3c19a4d4 authored by Tamer Tas's avatar Tamer Tas
Browse files

Extend the tests for regexp pattern package

parent cb09cecb
No related branches found
No related tags found
No related merge requests found
......@@ -3,14 +3,14 @@ package pattern
import "regexp"
const (
secureURL = `^((https):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`
url = `^(https?:\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`
email = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\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= "^[-+]?[0-9]+$"
)
......@@ -34,5 +34,5 @@ var (
UnixPath = regexp.MustCompile(unixPath)
// URL regular expression for URL strings.
URL = regexp.MustCompile(secureURL)
URL = regexp.MustCompile(url)
)
......@@ -26,3 +26,67 @@ func TestUnixPathPattern(t *testing.T) {
}
}
}
func TestAlphanumericPattern(t *testing.T) {
tests := []struct {
String string
Valid bool
}{
{" ", false},
{"/", false},
{"root", true},
{"tmp-dir", false},
{"TMPDIR", true},
{"L33T", true},
{"L@@T", false},
}
for _, test := range tests {
if ok := pattern.Alphanumeric.MatchString(test.String); ok != test.Valid {
t.Errorf("pattern.Alphanumeric.MatchString(%q) expected to be %v", test.String, test.Valid)
}
}
}
func TestIntegerPattern(t *testing.T) {
tests := []struct {
String string
Valid bool
}{
{"", false},
{" ", false},
{"/", false},
{"root", false},
{"L33T", false},
{"", false},
}
for _, test := range tests {
if ok := pattern.Numeric.MatchString(test.String); ok != test.Valid {
t.Errorf("pattern.Numeric.MatchString(%q) expected to be %v", test.String, test.Valid)
}
}
}
func TestURLPattern(t *testing.T) {
tests := []struct {
String string
Valid bool
}{
{"", false},
{" ", false},
{"/", false},
{"http://", false},
{"http://github.com/tmrts/boilr", true},
{"https://github.com/tmrts/boilr", true},
{"github.com/tmrts/boilr", true},
{"rawcontent.github.com/tmrts/boilr", true},
{"github.com:80/tmrts/boilr", true},
}
for _, test := range tests {
if ok := pattern.URL.MatchString(test.String); ok != test.Valid {
t.Errorf("pattern.URL.MatchString(%q) expected to be %v", test.String, test.Valid)
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment