Skip to content

Commit

Permalink
fix regex for checking field names and add unit tests for helper.go f…
Browse files Browse the repository at this point in the history
…unctions
  • Loading branch information
cmdoret committed Oct 7, 2020
1 parent 2fbe73a commit ecc8308
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dnaglider/pkg/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"
)

var fieldRegex = regexp.MustCompile("(GC|GCSKEW|ATSKEW|ENTRO|[0-9K]MER|)")
var fieldRegex = regexp.MustCompile("^(GC|GCSKEW|ATSKEW|ENTRO|[0-9]+MER)$")

func checkError(err error) {
if err != nil {
Expand All @@ -21,6 +21,7 @@ func checkFields(fields []string) error {
for _, field := range fields {
if !fieldRegex.Match([]byte(field)) {
err = errors.New("Invalid field name")
return err
}
}
return err
Expand Down
16 changes: 16 additions & 0 deletions dnaglider/pkg/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pkg

import (
"testing"
)

func TestCheckFields(t *testing.T) {
validFields := []string{"GC", "GCSKEW", "ATSKEW", "ENTRO", "3MER", "14MER"}
invalidFields := []string{"John Smith", "paper"}
if err := checkFields(validFields); err != nil {
t.Errorf("Valid fields raised an error")
}
if err := checkFields(invalidFields); err == nil {
t.Errorf("Invalid fields did not raise an error")
}
}

0 comments on commit ecc8308

Please sign in to comment.