Skip to content

Commit

Permalink
Merge branch 'randimize-enums-with-single-value' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondl committed Jun 15, 2017
2 parents 553cc9f + 64206cf commit 1e97530
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions randomize/randomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ func randEnumValue(enum string) (string, error) {
vals := strmangle.ParseEnumVals(enum)
if vals == nil || len(vals) == 0 {
return "", fmt.Errorf("unable to parse enum string: %s", enum)
} else if len(vals) == 1 {
return vals[0], nil
}

return vals[rand.Intn(len(vals)-1)], nil
Expand Down
14 changes: 12 additions & 2 deletions randomize/randomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ func TestRandEnumValue(t *testing.T) {

enum1 := "enum.workday('monday','tuesday')"
enum2 := "enum('monday','tuesday')"
enum3 := "enum('monday')"

r1, err := randEnumValue(enum1)
if err != nil {
t.Error(err)
}

if r1 != "monday" && r1 != "tuesday" {
t.Errorf("Expected monday or tueday, got: %q", r1)
t.Errorf("Expected monday or tuesday, got: %q", r1)
}

r2, err := randEnumValue(enum2)
Expand All @@ -166,6 +167,15 @@ func TestRandEnumValue(t *testing.T) {
}

if r2 != "monday" && r2 != "tuesday" {
t.Errorf("Expected monday or tueday, got: %q", r2)
t.Errorf("Expected monday or tuesday, got: %q", r2)
}

r3, err := randEnumValue(enum3)
if err != nil {
t.Error(err)
}

if r3 != "monday" {
t.Errorf("Expected monday got: %q", r3)
}
}

0 comments on commit 1e97530

Please sign in to comment.