test: split tests between valid and invalid

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31491/head
Benoit Tigeot 10 months ago
parent 9b242dd9ed
commit b296cbef6c
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -22,38 +22,38 @@ import (
) )
func TestValidPluginName(t *testing.T) { func TestValidPluginName(t *testing.T) {
tests := []struct { validNames := map[string]string{
name string "lowercase": "myplugin",
pluginName string "uppercase": "MYPLUGIN",
shouldPass bool "mixed case": "MyPlugin",
}{ "with digits": "plugin123",
// Valid names "with hyphen": "my-plugin",
{"lowercase", "myplugin", true}, "with underscore": "my_plugin",
{"uppercase", "MYPLUGIN", true}, "mixed chars": "my-awesome_plugin_123",
{"mixed case", "MyPlugin", true},
{"with digits", "plugin123", true},
{"with hyphen", "my-plugin", true},
{"with underscore", "my_plugin", true},
{"mixed chars", "my-awesome_plugin_123", true},
// Invalid names
{"empty", "", false},
{"space", "my plugin", false},
{"colon", "plugin:", false},
{"period", "my.plugin", false},
{"slash", "my/plugin", false},
{"dollar", "$plugin", false},
{"unicode", "plügîn", false},
} }
for _, tt := range tests { for name, pluginName := range validNames {
t.Run(tt.name, func(t *testing.T) { t.Run("valid/"+name, func(t *testing.T) {
matches := validPluginName.MatchString(tt.pluginName) if !validPluginName.MatchString(pluginName) {
if tt.shouldPass && !matches { t.Errorf("expected %q to match validPluginName regex", pluginName)
t.Errorf("expected %q to match validPluginName regex", tt.pluginName)
} }
if !tt.shouldPass && matches { })
t.Errorf("expected %q to not match validPluginName regex", tt.pluginName) }
invalidNames := map[string]string{
"empty": "",
"space": "my plugin",
"colon": "plugin:",
"period": "my.plugin",
"slash": "my/plugin",
"dollar": "$plugin",
"unicode": "plügîn",
}
for name, pluginName := range invalidNames {
t.Run("invalid/"+name, func(t *testing.T) {
if validPluginName.MatchString(pluginName) {
t.Errorf("expected %q to not match validPluginName regex", pluginName)
} }
}) })
} }

Loading…
Cancel
Save