Compare commits
No commits in common. "main" and "v1.1" have entirely different histories.
@ -1,39 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
// GetSharedElementsOfStringSlices finds the elements that are in both slices
|
|
||||||
func GetSharedElementsOfStringSlices(a, b []string) []string {
|
|
||||||
// Create a map to store the elements of "b"
|
|
||||||
mapOfElementsOfSliceB := make(map[string]bool)
|
|
||||||
for _, e := range b {
|
|
||||||
mapOfElementsOfSliceB[e] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate through "a" and find the elements that are not in "b"
|
|
||||||
var sharedElementsOfBothSlices []string
|
|
||||||
for _, e := range a {
|
|
||||||
if mapOfElementsOfSliceB[e] {
|
|
||||||
sharedElementsOfBothSlices = append(sharedElementsOfBothSlices, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sharedElementsOfBothSlices
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUniqueElementsOfStringSlices finds the elements that are in the first slice but not in the second slice
|
|
||||||
func GetUniqueElementsOfStringSlices(a, b []string) []string {
|
|
||||||
// Create a map to store the elements of "b"
|
|
||||||
mapOfElementsOfSliceB := make(map[string]bool)
|
|
||||||
for _, e := range b {
|
|
||||||
mapOfElementsOfSliceB[e] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate through "a" and find the elements that are not in "b"
|
|
||||||
var uniqueElementsOfSliceA []string
|
|
||||||
for _, e := range a {
|
|
||||||
if !mapOfElementsOfSliceB[e] {
|
|
||||||
uniqueElementsOfSliceA = append(uniqueElementsOfSliceA, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return uniqueElementsOfSliceA
|
|
||||||
}
|
|
@ -1,93 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetSharedElementsOfStringSlices(t *testing.T) {
|
|
||||||
data := []map[string][]string{
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{"banana", "cherry", "date", "fig"},
|
|
||||||
"expectedResult": []string{"banana", "cherry"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"banana", "cherry", "date", "fig"},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{"banana", "cherry"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{"apple", "banana", "cherry"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{},
|
|
||||||
"expectedResult": []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{},
|
|
||||||
"b": []string{},
|
|
||||||
"expectedResult": []string{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range data {
|
|
||||||
result := GetSharedElementsOfStringSlices(test["a"], test["b"])
|
|
||||||
if !reflect.DeepEqual(result, test["expectedResult"]) {
|
|
||||||
if len(result) == 0 && len(test["expectedResult"]) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, test["expectedResult"], test)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetUniqueElementsOfStringSlices(t *testing.T) {
|
|
||||||
data := []map[string][]string{
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{"banana", "cherry", "date", "fig"},
|
|
||||||
"expectedResult": []string{"apple"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"banana", "cherry", "date", "fig"},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{"date", "fig"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{},
|
|
||||||
"b": []string{"apple", "banana", "cherry"},
|
|
||||||
"expectedResult": []string{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"a": []string{"apple", "banana", "cherry"},
|
|
||||||
"b": []string{},
|
|
||||||
"expectedResult": []string{"apple", "banana", "cherry"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range data {
|
|
||||||
result := GetUniqueElementsOfStringSlices(test["a"], test["b"])
|
|
||||||
if !reflect.DeepEqual(result, test["expectedResult"]) {
|
|
||||||
if len(result) == 0 && len(test["expectedResult"]) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, test["expectedResult"], test)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
108
filesystem.go
108
filesystem.go
@ -1,108 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AppendLineToFile appends the line l to file
|
|
||||||
func AppendLineToFile(file string, l string) error {
|
|
||||||
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
if _, err = f.WriteString(l + "\n"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateFolder creates a the folder f
|
|
||||||
func CreateFolder(f string) error {
|
|
||||||
err := os.Mkdir(f, 0700)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAllFilesInFolderByExtension returns all files by extension of a given folder recursively
|
|
||||||
func GetAllFilesInFolderByExtension(pathToFolder string, extension string) []string {
|
|
||||||
files := []string{}
|
|
||||||
|
|
||||||
filepath.Walk(pathToFolder,
|
|
||||||
func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if filepath.Ext(path) != extension {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
files = append(files, path)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return files
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPathToParrentFolderOfThisExecutable returns the path to the folder where this executable is located
|
|
||||||
func GetPathToParrentFolderOfThisExecutable() string {
|
|
||||||
return filepath.Dir(GetPathToThisExecutable())
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPathToThisExecutable returns the path where this executable is located
|
|
||||||
func GetPathToThisExecutable() string {
|
|
||||||
e, err := os.Executable()
|
|
||||||
if err != nil {
|
|
||||||
return "."
|
|
||||||
}
|
|
||||||
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetSizeOfFileInBytes returns the size of a file in bytes
|
|
||||||
func GetSizeOfFileInBytes(p string) (int64, error) {
|
|
||||||
if !DoesFileExist(p) {
|
|
||||||
return 0, errors.New("not a file or does not exist")
|
|
||||||
}
|
|
||||||
|
|
||||||
fileInfo, err := os.Stat(p)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileInfo.Size(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadStringFromFile writes a string to a file
|
|
||||||
func LoadStringFromFile(pathOfFile string) (string, error) {
|
|
||||||
if !DoesFileExist(pathOfFile) {
|
|
||||||
return "", errors.New("file " + pathOfFile + "does not exist")
|
|
||||||
}
|
|
||||||
|
|
||||||
fileContent, err := os.ReadFile(pathOfFile)
|
|
||||||
if err != nil {
|
|
||||||
return "", errors.New("could not read file " + pathOfFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(fileContent), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteStringToFile writes a string to a file
|
|
||||||
func WriteStringToFile(file string, s string) error {
|
|
||||||
err := os.WriteFile(file, []byte(s+"\n"), 0660)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetSizeOfFileInBytes(t *testing.T) {
|
|
||||||
data := map[string]int64{
|
|
||||||
"/usr/bin/go": 11466992, // ls -lH /usr/bin/go | awk -F ' ' '{print $5}'
|
|
||||||
"/etc/fstab": 1049, // ls -lH /etc/fstab | awk -F ' ' '{print $5}'
|
|
||||||
"/etc/os-release": 371, // ls -lH /etc/os-release | awk -F ' ' '{print $5}'
|
|
||||||
"/etc": 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result, _ := GetSizeOfFileInBytes(d)
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
16
strings.go
16
strings.go
@ -52,22 +52,6 @@ func RemoveDoubledStrings(s string, target string) string {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveLeadingStrings removes all leading occurrences of target in s
|
|
||||||
func RemoveLeadingStrings(s string, target string) string {
|
|
||||||
lenS := len(s)
|
|
||||||
lenTarget := len(target)
|
|
||||||
|
|
||||||
if lenS < lenTarget {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
if s[:lenTarget] != target {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
return RemoveLeadingStrings(s[lenTarget:], target)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SanitizeStringWithWhitelist cleans s by all substrings which do not occur in whitelist
|
// SanitizeStringWithWhitelist cleans s by all substrings which do not occur in whitelist
|
||||||
func SanitizeStringWithWhitelist(s string, whitelist map[string]string) string {
|
func SanitizeStringWithWhitelist(s string, whitelist map[string]string) string {
|
||||||
sanitizedString := ""
|
sanitizedString := ""
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFirstCharacterToUppercase(t *testing.T) {
|
func TestFirstCharacterToUppercase(t *testing.T) {
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
@ -58,36 +56,6 @@ func TestRemoveDoubledStrings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveLeadingStrings(t *testing.T) {
|
|
||||||
data := map[string]string{
|
|
||||||
"": "",
|
|
||||||
".": "",
|
|
||||||
"...Jahr 2022... die überleben wollen...": "Jahr 2022... die überleben wollen...",
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := RemoveLeadingStrings(d, ".")
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data = map[string]string{
|
|
||||||
"": "",
|
|
||||||
".": ".",
|
|
||||||
"..": "..",
|
|
||||||
"...": "",
|
|
||||||
"....": ".",
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := RemoveLeadingStrings(d, "...")
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSanitizeStringWithWhitelist(t *testing.T) {
|
func TestSanitizeStringWithWhitelist(t *testing.T) {
|
||||||
whitelistedChars := []string{
|
whitelistedChars := []string{
|
||||||
".", "-",
|
".", "-",
|
||||||
|
@ -1,52 +1,6 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import "net/url"
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DoesFileExist returns true, if a file exists (and actually is a file)
|
|
||||||
func DoesFileExist(p string) bool {
|
|
||||||
s, err := os.Stat(p)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.IsDir() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoesFolderExist returns true, if a folder exists (and actually is a folder)
|
|
||||||
func DoesFolderExist(p string) bool {
|
|
||||||
s, err := os.Stat(p)
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !s.IsDir() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// DoesStringContainsNonWhitelistedSubstrings returns false if s contains substrings which are not in whitelist
|
|
||||||
func DoesStringContainsNonWhitelistedSubstrings(s string, whitelist map[string]string) bool {
|
|
||||||
for _, char := range s {
|
|
||||||
_, charIsWhitelisted := whitelist[string(char)]
|
|
||||||
if !charIsWhitelisted {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsStringInSliceOfStrings returns true, if slice contains target
|
// IsStringInSliceOfStrings returns true, if slice contains target
|
||||||
func IsStringInSliceOfStrings(slice []string, target string) bool {
|
func IsStringInSliceOfStrings(slice []string, target string) bool {
|
||||||
@ -59,17 +13,8 @@ func IsStringInSliceOfStrings(slice []string, target string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValidEmail returns true, if s is a email address.
|
// IsValidUrl returns true, if s is a valid URL
|
||||||
func IsValidEmail(s string) bool {
|
func IsValidUrl(s string) bool {
|
||||||
// Used Regex instead of mail.ParseAddress(), as the latter incorrectly recognizes samples such as “bad@domain” as a valid address
|
|
||||||
// Shoutout to emailregex.com for the almighty regular expression
|
|
||||||
return regexp.MustCompile(
|
|
||||||
`(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`,
|
|
||||||
).MatchString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsValidURL returns true, if s is a valid URL
|
|
||||||
func IsValidURL(s string) bool {
|
|
||||||
u, err := url.Parse(s)
|
u, err := url.Parse(s)
|
||||||
if err != nil || u.Scheme == "" || u.Host == "" {
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
||||||
return false
|
return false
|
||||||
@ -77,9 +22,3 @@ func IsValidURL(s string) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValidUUID returns true, if s is a valid UUID
|
|
||||||
func IsValidUUID(s string) bool {
|
|
||||||
_, err := uuid.Parse(s)
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
@ -1,44 +1,6 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestDoesFileExist(t *testing.T) {
|
|
||||||
data := map[string]bool{
|
|
||||||
"/usr/bin/go": true,
|
|
||||||
"/etc/fstab": true,
|
|
||||||
"/etc/os-release": true,
|
|
||||||
"/usr/bin": false,
|
|
||||||
"/home/derschmierigetypvomaldi": false,
|
|
||||||
"/tmp/plainTextPasswords.txt": false,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := DoesFileExist(d)
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDoesFolderExist(t *testing.T) {
|
|
||||||
data := map[string]bool{
|
|
||||||
"/home": true,
|
|
||||||
"/tmp": true,
|
|
||||||
"/usr/bin": true,
|
|
||||||
"/home/derschmierigetypvomaldi": false,
|
|
||||||
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
|
|
||||||
"/usr/bin/go": false,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := DoesFolderExist(d)
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsStringInSliceOfStrings(t *testing.T) {
|
func TestIsStringInSliceOfStrings(t *testing.T) {
|
||||||
slice1 := []string{"apple", "banana", "cherry", "date"}
|
slice1 := []string{"apple", "banana", "cherry", "date"}
|
||||||
@ -74,7 +36,7 @@ func TestIsStringInSliceOfStrings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValidURL(t *testing.T) {
|
func TestIsValidUrl(t *testing.T) {
|
||||||
data := map[string]bool{
|
data := map[string]bool{
|
||||||
"": false,
|
"": false,
|
||||||
"0x0001f346": false,
|
"0x0001f346": false,
|
||||||
@ -85,50 +47,7 @@ func TestIsValidURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
for d, expectedResult := range data {
|
||||||
result := IsValidURL(d)
|
result := IsValidUrl(d)
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsValidUUID(t *testing.T) {
|
|
||||||
data := map[string]bool{
|
|
||||||
"": false,
|
|
||||||
"0x0001f346": false,
|
|
||||||
"0179eb94-7a81-11ee-b962-0242ac120002": true,
|
|
||||||
"c3bcd438-7a80-11ee-b962-0242ac120002": true,
|
|
||||||
"0a245e0a-7a81-11ee-b962-0242ac120002": true,
|
|
||||||
"9d90b107-fc59-45f8-a622-f55b0a1396d6": true,
|
|
||||||
"f327c2b7-15a5-43c4-aefb-c66c6f34a32e": true,
|
|
||||||
"02cb18ec-5d6a-428d-8159-643bde3f43bb": true,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := IsValidUUID(d)
|
|
||||||
if result != expectedResult {
|
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsValidEmail(t *testing.T) {
|
|
||||||
data := map[string]bool{
|
|
||||||
"test@example.com": true,
|
|
||||||
"another.test@domain.co.uk": true,
|
|
||||||
"username@domain.c": true,
|
|
||||||
"": false,
|
|
||||||
"invalid-email": false,
|
|
||||||
"bad@domain": false,
|
|
||||||
"@missingusername.com": false,
|
|
||||||
"plainaddress": false,
|
|
||||||
"@missingdomain": false,
|
|
||||||
"missingatsign.com": false,
|
|
||||||
"username@.com": false,
|
|
||||||
}
|
|
||||||
|
|
||||||
for d, expectedResult := range data {
|
|
||||||
result := IsValidEmail(d)
|
|
||||||
if result != expectedResult {
|
if result != expectedResult {
|
||||||
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user