utils/strings_test.go
2023-10-02 23:42:16 +02:00

87 lines
3.3 KiB
Go

package utils
import "testing"
func TestFirstCharacterToUppercase(t *testing.T) {
data := map[string]string{
"": "",
" ": " ",
"houston": "Houston",
"san antonio": "San antonio",
"Austin": "Austin",
}
for d, expectedResult := range data {
result := FirstCharacterToUppercase(d)
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestGetSHA256SumOfString(t *testing.T) {
data := map[string]string{
"": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"Hello, World!": "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f",
"Veni, vidi, vici": "b1610284c94bbf9aa78333e57ddce234a5e845d61e09ce91a7e19fa24737f466",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"123456": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"123456789": "15e2b0d3c33891ebb0f1ef609ec419420c20e320ce94c65fbc8c3312448eb225",
"qwertz": "d482ba4b7d3218f3e841038c407ed1f94e9846a4dd68e56bab7718903962aa98",
"12345": "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5",
"abc123": "6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090",
"0000": "9af15b336e6a9619928537df30b2e6a2376569fcf9d7e773eccede65606529a0",
}
for d, expectedResult := range data {
result := GetSHA256SumOfString(d)
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestRemoveDoubledStrings(t *testing.T) {
data := map[string]string{
"................": ".",
"..SENDNUDES..": ".SENDNUDES.",
"....SEND........NUDES....": ".SEND.NUDES.",
}
for d, expectedResult := range data {
result := RemoveDoubledStrings(d, ".")
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestSanitizeStringWithWhitelist(t *testing.T) {
whitelistedChars := []string{
".", "-",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
whitelist := map[string]string{}
for _, c := range whitelistedChars {
whitelist[c] = c
}
data := map[string]string{
"! \" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \\ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~": "-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
"Während Adam lacht, jagen zwölf Boxkämpfer Eva quer über den großen Sylter Deich - für satte 12.345.667,89 €uro": "WhrendAdamlachtjagenzwlfBoxkmpferEvaquerberdengroenSylterDeich-frsatte12.345.66789uro",
}
for d, expectedResult := range data {
result := SanitizeStringWithWhitelist(d, whitelist)
if result != expectedResult {
t.Errorf("\ngot: %s\nwanted: %s\nfor: %q", result, expectedResult, d)
}
}
}