From 5204192574fa16befb09956a2bedfa61a7225098 Mon Sep 17 00:00:00 2001 From: Florent Biville Date: Tue, 7 Mar 2023 19:04:44 +0100 Subject: [PATCH] Refine error message, add test case --- modulegen/main.go | 4 ++-- modulegen/main_test.go | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/modulegen/main.go b/modulegen/main.go index 91c312c11d2..d5e6b8b4edb 100644 --- a/modulegen/main.go +++ b/modulegen/main.go @@ -97,11 +97,11 @@ func (e *Example) Type() string { func (e *Example) Validate() error { if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.Name) { - return fmt.Errorf("invalid name: %s. Only alphanumerical characters are allowed", e.Name) + return fmt.Errorf("invalid name: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.Name) } if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.TitleName) { - return fmt.Errorf("invalid title: %s. Only alphanumerical characters are allowed", e.TitleName) + return fmt.Errorf("invalid title: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.TitleName) } return nil diff --git a/modulegen/main_test.go b/modulegen/main_test.go index 2e985dfccd2..c29463062f3 100644 --- a/modulegen/main_test.go +++ b/modulegen/main_test.go @@ -113,7 +113,7 @@ func TestExample_Validate(outer *testing.T) { Name: "Amazing DB 4 The Win", TitleName: "AmazingDB", }, - expectedErr: errors.New("invalid name: Amazing DB 4 The Win. Only alphanumerical characters are allowed"), + expectedErr: errors.New("invalid name: Amazing DB 4 The Win. Only alphanumerical characters are allowed (leading character must be a letter)"), }, { name: "non-alphanumerical characters in title", @@ -121,7 +121,23 @@ func TestExample_Validate(outer *testing.T) { Name: "AmazingDB", TitleName: "Amazing DB 4 The Win", }, - expectedErr: errors.New("invalid title: Amazing DB 4 The Win. Only alphanumerical characters are allowed"), + expectedErr: errors.New("invalid title: Amazing DB 4 The Win. Only alphanumerical characters are allowed (leading character must be a letter)"), + }, + { + name: "leading numerical character in name", + example: Example{ + Name: "1AmazingDB", + TitleName: "AmazingDB", + }, + expectedErr: errors.New("invalid name: 1AmazingDB. Only alphanumerical characters are allowed (leading character must be a letter)"), + }, + { + name: "leading numerical character in title", + example: Example{ + Name: "AmazingDB", + TitleName: "1AmazingDB", + }, + expectedErr: errors.New("invalid title: 1AmazingDB. Only alphanumerical characters are allowed (leading character must be a letter)"), }, }