utils/validators_test.go

94 lines
2.8 KiB
Go
Raw Normal View History

2023-10-02 23:42:16 +02:00
package utils
2023-10-13 20:38:08 +02:00
import (
"testing"
)
2023-10-02 23:42:16 +02:00
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)
}
}
}
2023-10-13 20:38:08 +02:00
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)
}
}
}