utils/validators_test.go
2024-06-05 21:32:49 +02:00

137 lines
4.0 KiB
Go

package utils
import (
"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) {
slice1 := []string{"apple", "banana", "cherry", "date"}
target1 := "banana"
result1 := IsStringInSliceOfStrings(slice1, target1)
expectedResult1 := true
if result1 != expectedResult1 {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q\nin: %+v", result1, expectedResult1, target1, slice1)
}
slice2 := []string{"apple", "banana", "cherry", "date"}
target2 := "grape"
result2 := IsStringInSliceOfStrings(slice2, target2)
expectedResult2 := false
if result2 != expectedResult2 {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q\nin: %+v", result2, expectedResult2, target2, slice2)
}
slice3 := []string{}
target3 := "apple"
result3 := IsStringInSliceOfStrings(slice3, target3)
expectedResult3 := false
if result3 != expectedResult3 {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q\nin: %+v", result3, expectedResult3, target3, slice3)
}
slice4 := []string{"pear"}
target4 := "pear"
result4 := IsStringInSliceOfStrings(slice4, target4)
expectedResult4 := true
if result4 != expectedResult4 {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q\nin: %+v", result4, expectedResult4, target4, slice4)
}
}
func TestIsValidURL(t *testing.T) {
data := map[string]bool{
"": false,
"0x0001f346": false,
"git.0x0001f346.de": false,
"http://git.0x0001f346.de": true,
"https://git.0x0001f346.de": true,
"https://git.0x0001f346.de/andreas/": true,
}
for d, expectedResult := range data {
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 {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}