kopia lustrzana https://github.com/reiver/greatape
feat(app): 🦺 add new validators
rodzic
d28e95bccd
commit
a107dcd600
|
@ -0,0 +1,11 @@
|
|||
package validators
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
. "github.com/reiver/greatape/components/constants"
|
||||
)
|
||||
|
||||
func EmailIsValid(input string) bool {
|
||||
return regexp.MustCompile(EMAIL).MatchString(input)
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
package validators_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/reiver/greatape/app/validators"
|
||||
)
|
||||
|
||||
func TestEmailValidator(test *testing.T) {
|
||||
type arguments struct {
|
||||
email string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectation bool
|
||||
arguments arguments
|
||||
}{
|
||||
{
|
||||
"Case1",
|
||||
false,
|
||||
arguments{
|
||||
email: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case2",
|
||||
false,
|
||||
arguments{
|
||||
email: "user",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case3",
|
||||
true,
|
||||
arguments{
|
||||
email: "user@domain.com",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case4",
|
||||
true,
|
||||
arguments{
|
||||
email: "user+plus@gmail.com",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case5",
|
||||
true,
|
||||
arguments{
|
||||
email: "susan235@gmail.com",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case6",
|
||||
true,
|
||||
arguments{
|
||||
email: "new_user@icloud.com",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case7",
|
||||
true,
|
||||
arguments{
|
||||
email: "someone@somewhere.co.uk",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case8",
|
||||
true,
|
||||
arguments{
|
||||
email: "north.star@space.social",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
test.Run(testCase.name, func(test *testing.T) {
|
||||
if result := validators.EmailIsValid(testCase.arguments.email); result != testCase.expectation {
|
||||
test.Errorf("EmailIsValid() = %v, expected %v", result, testCase.expectation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package validators
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
. "github.com/reiver/greatape/components/constants"
|
||||
)
|
||||
|
||||
func WebfingerIsValid(webfinger string) bool {
|
||||
return regexp.MustCompile(WEBFINGER).MatchString(webfinger)
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package validators_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/reiver/greatape/app/validators"
|
||||
)
|
||||
|
||||
func TestWebfingerValidator(test *testing.T) {
|
||||
type arguments struct {
|
||||
webfinger string
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectation bool
|
||||
arguments arguments
|
||||
}{
|
||||
{
|
||||
"Case1",
|
||||
false,
|
||||
arguments{
|
||||
webfinger: "somebody@somewhere.xyz",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case2",
|
||||
true,
|
||||
arguments{
|
||||
webfinger: "acct:somebody@somewhere.xyz",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case3",
|
||||
true,
|
||||
arguments{
|
||||
webfinger: "acct:user@sub.domain.com",
|
||||
},
|
||||
},
|
||||
{
|
||||
"Case4",
|
||||
false,
|
||||
arguments{
|
||||
webfinger: "acct:user",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
test.Run(testCase.name, func(test *testing.T) {
|
||||
if result := validators.WebfingerIsValid(testCase.arguments.webfinger); result != testCase.expectation {
|
||||
test.Errorf("WebfingerIsValid() = %v, expected %v", result, testCase.expectation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Ładowanie…
Reference in New Issue