-
Notifications
You must be signed in to change notification settings - Fork 4
/
expression_validator_test.go
58 lines (48 loc) · 1.57 KB
/
expression_validator_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
package unis
import (
"strings"
"testing"
)
// Tests:
// If
// NewMatcher
// IsMail
// NewExclusivePrepender
func TestIsMail(t *testing.T) {
mailToPreffixer := NewExclusivePrepender("mailto:")
mailTo := If(IsMail, mailToPreffixer, ClearProcessor)
// it checks if a string is an e-mail,if so then it runs the "mailToPreffixer"
// otherwise it runs the ClearProcessor which clears the string and returns an empty string.
tests := []originalAgainstResult{
{"mail1@hotmail.com", "mailto:mail1@hotmail.com"},
{"mail1hotmail.com", ""},
{"@google.com", ""},
{"mail@google", ""},
{"mail2@google.com.dot", "mailto:mail2@google.com.dot"},
{"mail2@google.com.dot.2", ""},
}
testOriginalAgainstResult(mailTo, tests, t)
}
func TestMatcherPanics(t *testing.T) {
Logger = func(string) {} // disable the print of the error to the console.
m := NewMatcher("\xf8\xa1\xa1\xa1\xa1")
expected := false
expectedErrMessagePrefix := "error parsing regexp: invalid UTF-8:"
got, err := m.Valid("this_is_an_invalid_regexp_@expression")
if err == nil {
t.Fatalf("error should be filled with the correct message because the regexp is invalid")
} else if err != nil && !strings.HasPrefix(err.Error(), expectedErrMessagePrefix) {
t.Fatalf("expected error to starts with '%s' but got '%s'", expectedErrMessagePrefix, err.Error())
}
if expected != got {
t.Fatalf("[0] expected false but got true")
}
got, _ = m.Valid("")
if expected != got {
t.Fatalf("[1] expected false but got true")
}
got, _ = m.Valid("string")
if expected != got {
t.Fatalf("[2] expected false but got true")
}
}