-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Extract examples.Generate function
- Loading branch information
1 parent
cdcb0c6
commit f349c08
Showing
3 changed files
with
109 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package examples | ||
|
||
import ( | ||
"math/rand" | ||
"strings" | ||
|
||
"github.com/s0rg/fantasyname" | ||
) | ||
|
||
const emptyNames = "" | ||
|
||
// Generate as many examples as numberOfExamples from the given pattern as a comma-separated string | ||
func Generate(pattern string, numberOfExamples uint) (string, error) { | ||
if pattern == "" || numberOfExamples == 0 { | ||
return emptyNames, nil | ||
} | ||
examples := make([]string, numberOfExamples) | ||
for i := 0; i < int(numberOfExamples); i++ { | ||
gen, err := fantasyname.Compile(pattern, fantasyname.Collapse(true), fantasyname.RandFn(rand.Intn)) | ||
if err != nil { | ||
return emptyNames, err | ||
} | ||
examples[i] = gen.String() | ||
} | ||
return commaSeparated(examples), nil | ||
} | ||
|
||
func commaSeparated(examples []string) string { | ||
if len(examples) == 0 { | ||
return "" | ||
} | ||
|
||
joinedString := new(strings.Builder) | ||
for i, example := range examples { | ||
joinedString.WriteString(example) | ||
if i < len(examples)-1 { | ||
joinedString.WriteString(", ") | ||
} | ||
} | ||
|
||
return joinedString.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package examples_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/carloscasalar/fantasyname-pattern-extractor/internal/examples" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateExamples_(t *testing.T) { | ||
tests := map[string]struct { | ||
pattern string | ||
numberOfExamples uint | ||
expected string | ||
}{ | ||
"Empty pattern should return no examples": { | ||
pattern: "", | ||
numberOfExamples: 100, | ||
expected: "", | ||
}, | ||
"Zero examples should return no examples": { | ||
pattern: "vcv", | ||
numberOfExamples: 0, | ||
expected: "", | ||
}, | ||
"Valid pattern and non-zero number should generate the expected number of examples": { | ||
pattern: "(some pattern)", | ||
numberOfExamples: 3, | ||
expected: "some pattern, some pattern, some pattern", | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
got, err := examples.Generate(tt.pattern, tt.numberOfExamples) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestGenerateExamples_ErrorCases(t *testing.T) { | ||
tests := map[string]struct { | ||
pattern string | ||
numberOfExamples uint | ||
}{ | ||
"Invalid pattern should result in error": { | ||
pattern: "a>", | ||
numberOfExamples: 1, | ||
}, | ||
} | ||
|
||
for name, tt := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
got, err := examples.Generate(tt.pattern, tt.numberOfExamples) | ||
assert.Error(t, err) | ||
assert.Equal(t, "", got) | ||
}) | ||
} | ||
} |