Skip to content

Commit

Permalink
Refine error message, add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Mar 7, 2023
1 parent 15048fb commit 5204192
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions modulegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,31 @@ 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",
example: Example{
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)"),
},
}

Expand Down

0 comments on commit 5204192

Please sign in to comment.