Skip to content

Commit

Permalink
Expose fields in Options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Mar 14, 2021
1 parent 0f4770b commit 55e33f8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
42 changes: 21 additions & 21 deletions uniqe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ import (

// Options is the input argument for Generate function
type Options struct {
dictionaries [][]string
length int
separator *string
seed int64
style string
Dictionaries [][]string
Length int
Separator *string
Seed int64
Style string
}

var defaultSeparator = "_"

func (o *Options) fillDefaults() *Options {
if o.dictionaries == nil {
o.dictionaries = [][]string{Adjectives, Colors, Animals}
if o.Dictionaries == nil {
o.Dictionaries = [][]string{Adjectives, Colors, Animals}
}

if o.length == 0 {
if o.Length == 0 {
defaultLength := 3

if dictLength := len(o.dictionaries); dictLength < defaultLength {
o.length = dictLength
if dictLength := len(o.Dictionaries); dictLength < defaultLength {
o.Length = dictLength
} else {
o.length = 3
o.Length = 3
}
}

if o.seed == 0 {
o.seed = time.Now().Unix()
if o.Seed == 0 {
o.Seed = time.Now().Unix()
}

if o.separator == nil {
o.separator = &defaultSeparator
if o.Separator == nil {
o.Separator = &defaultSeparator
}

if o.style == "" {
o.style = "lowercase"
if o.Style == "" {
o.Style = "lowercase"
}

return o
Expand All @@ -53,14 +53,14 @@ func New(o Options) string {

config := o.fillDefaults()

for _, dict := range config.dictionaries[0:config.length] {
s := rand.NewSource(config.seed)
for _, dict := range config.Dictionaries[0:config.Length] {
s := rand.NewSource(config.Seed)

r := rand.New(s)
n := r.Intn(len(dict))
word := dict[n]

switch config.style {
switch config.Style {
case "uppercase":
word = strings.ToUpper(word)
case "lowercase":
Expand All @@ -72,5 +72,5 @@ func New(o Options) string {
uniqueNames = append(uniqueNames, word)
}

return strings.Join(uniqueNames, *config.separator)
return strings.Join(uniqueNames, *config.Separator)
}
28 changes: 14 additions & 14 deletions unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func TestGenerate(t *testing.T) {
}

// should generate random names given only two dictionaries
if got := New(Options{dictionaries: [][]string{{"a"}, {"b"}}}); got != "a_b" {
if got := New(Options{Dictionaries: [][]string{{"a"}, {"b"}}}); got != "a_b" {
t.Errorf(`Expected to generate "a-b" but got: "%s"`, got)
}

// should generate random names given only one dictonary
if got := New(Options{dictionaries: [][]string{{"a"}}}); got != "a" {
if got := New(Options{Dictionaries: [][]string{{"a"}}}); got != "a" {
t.Errorf(`Expected to generate "a" but got: "%s"`, got)
}

Expand All @@ -33,13 +33,13 @@ func TestGenerate(t *testing.T) {
}

// should generate string with given length
if got := New(Options{length: 2}); len(strings.Split(got, "_")) != 2 {
if got := New(Options{Length: 2}); len(strings.Split(got, "_")) != 2 {
t.Errorf("Generated string is expected to be of length 2")
}

// should generate string with given separator
hypen := "-"
if got := New(Options{separator: &hypen}); got != "" {
if got := New(Options{Separator: &hypen}); got != "" {
match, _ := regexp.MatchString(`^\w+-\w+-\w+$`, got)

if match == false {
Expand All @@ -49,12 +49,12 @@ func TestGenerate(t *testing.T) {

// should be able to concat strings without a seperator
emptySeparator := ""
if got := New(Options{dictionaries: [][]string{{"a"}, {"b"}}, separator: &emptySeparator}); got != "ab" {
if got := New(Options{Dictionaries: [][]string{{"a"}, {"b"}}, Separator: &emptySeparator}); got != "ab" {
t.Errorf(`Expected ab but got %s`, got)
}

seededUnique1 := New(Options{seed: 10})
seededUnique2 := New(Options{seed: 10})
seededUnique1 := New(Options{Seed: 10})
seededUnique2 := New(Options{Seed: 10})

// should generate random string with given separator `underscrore`
if seededUnique1 != seededUnique2 {
Expand All @@ -64,8 +64,8 @@ func TestGenerate(t *testing.T) {
// should format the string to uppercase
if got := New(
Options{
dictionaries: [][]string{{"test"}, {"uppercase"}, {"style"}},
style: "uppercase",
Dictionaries: [][]string{{"test"}, {"uppercase"}, {"style"}},
Style: "uppercase",
},
); got != "TEST_UPPERCASE_STYLE" {
t.Errorf(`Expect to format in uppercase style but got "%s"`, got)
Expand All @@ -74,18 +74,18 @@ func TestGenerate(t *testing.T) {
// should format the string to lowercase
if got := New(
Options{
dictionaries: [][]string{{"TEST"}, {"LOWERCASE"}, {"STYLE"}},
style: "lowercase",
Dictionaries: [][]string{{"TEST"}, {"LOWERCASE"}, {"STYLE"}},
Style: "lowercase",
},
); got != "test_lowercase_style" {
t.Errorf(`Expect to format in lowercase style but got "%s"`, got)
}

if got := New(
Options{
dictionaries: [][]string{{"test"}, {"titlecase"}, {"style"}},
separator: &emptySeparator,
style: "titlecase",
Dictionaries: [][]string{{"test"}, {"titlecase"}, {"style"}},
Separator: &emptySeparator,
Style: "titlecase",
},
); got != "TestTitlecaseStyle" {
t.Errorf(`Expect to format in titlecase style but got "%s"`, got)
Expand Down

0 comments on commit 55e33f8

Please sign in to comment.