Skip to content

Commit

Permalink
Allow numerical characters in example names/titles
Browse files Browse the repository at this point in the history
  • Loading branch information
fbiville committed Mar 7, 2023
1 parent e67432e commit 15048fb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
8 changes: 4 additions & 4 deletions modulegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (e *Example) Type() string {
}

func (e *Example) Validate() error {
if !regexp.MustCompile(`^[A-Za-z]+$`).MatchString(e.Name) {
return fmt.Errorf("invalid name: %s. Only alphabetical characters are allowed", e.Name)
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)
}

if !regexp.MustCompile(`^[A-Za-z]+$`).MatchString(e.TitleName) {
return fmt.Errorf("invalid title: %s. Only alphabetical characters are allowed", e.TitleName)
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 nil
Expand Down
59 changes: 57 additions & 2 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -77,6 +78,60 @@ func TestExample(t *testing.T) {
}
}

func TestExample_Validate(outer *testing.T) {
outer.Parallel()

tests := []struct {
name string
example Example
expectedErr error
}{
{
name: "only alphabetical characters in name/title",
example: Example{
Name: "AmazingDB",
TitleName: "AmazingDB",
},
},
{
name: "alphanumerical characters in name",
example: Example{
Name: "AmazingDB4tw",
TitleName: "AmazingDB",
},
},
{
name: "alphanumerical characters in title",
example: Example{
Name: "AmazingDB",
TitleName: "AmazingDB4tw",
},
},
{
name: "non-alphanumerical characters in name",
example: Example{
Name: "Amazing DB 4 The Win",
TitleName: "AmazingDB",
},
expectedErr: errors.New("invalid name: Amazing DB 4 The Win. Only alphanumerical characters are allowed"),
},
{
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"),
},
}

for _, test := range tests {
outer.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedErr, test.example.Validate())
})
}
}

func TestGenerateWrongExampleName(t *testing.T) {
rootTmp := t.TempDir()
examplesTmp := filepath.Join(rootTmp, "examples")
Expand Down Expand Up @@ -190,8 +245,8 @@ func TestGenerate(t *testing.T) {
assert.Nil(t, err)

example := Example{
Name: "foodb",
TitleName: "FooDB",
Name: "foodb4tw",
TitleName: "FooDB4TheWin",
IsModule: false,
Image: "docker.io/example/foodb:latest",
TCVersion: "v0.0.0-test",
Expand Down

0 comments on commit 15048fb

Please sign in to comment.