Skip to content

Commit

Permalink
refactor: remove instances of typecasting in config/generate.go
Browse files Browse the repository at this point in the history
refactor: service/transfomer.go so all the transformation functions belong to DefaultTransformerService
test: update various test function names
  • Loading branch information
eljamo committed Jan 10, 2024
1 parent a6d8826 commit a0a4bc0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
# Output of the go coverage tool
*.out

# Dependency directories (remove the comment below to include it)
Expand All @@ -25,4 +25,4 @@ go.work
.DS_Store

# GoLand
.idea/
.idea/
12 changes: 6 additions & 6 deletions config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
"github.com/eljamo/libpass/v5/internal/merger"
)

func mapToJSONString(m map[string]any) (string, error) {
func mapToJSONString(m map[string]any) ([]byte, error) {
mj, err := json.Marshal(m)
if err != nil {
return "", err
return nil, err
}

return string(mj), nil
return mj, nil
}

func mergeMaps(ms ...map[string]any) (string, error) {
func mergeMaps(ms ...map[string]any) ([]byte, error) {
mm := merger.Map(ms...)

return mapToJSONString(mm)
}

func jsonToSettings(js string) (*Settings, error) {
func jsonToSettings(js []byte) (*Settings, error) {
var cfg Settings
if err := json.Unmarshal([]byte(js), &cfg); err != nil {
if err := json.Unmarshal(js, &cfg); err != nil {
return nil, err
}

Expand Down
8 changes: 3 additions & 5 deletions service/password_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (m *mockWordListErrService) GetWords() ([]string, error) {
return nil, errors.New("word list error")
}

func TestNewPasswordGeneratorService(t *testing.T) {
func TestNewCustomPasswordGeneratorService(t *testing.T) {
t.Parallel()

t.Run("Valid Configuration", func(t *testing.T) {
Expand All @@ -88,7 +88,7 @@ func TestNewPasswordGeneratorService(t *testing.T) {
})
}

func TestGenerate(t *testing.T) {
func TestPasswordGenerate(t *testing.T) {
t.Parallel()

setupService := func(
Expand All @@ -107,14 +107,12 @@ func TestGenerate(t *testing.T) {
}
}

validService := setupService(&mockTransformerService{}, &mockSeparatorService{}, &mockPaddingService{}, &mockWordListService{})

tests := []struct {
name string
service *DefaultPasswordGeneratorService
wantErr bool
}{
{"Valid Service", validService, false},
{"Valid Service", setupService(&mockTransformerService{}, &mockSeparatorService{}, &mockPaddingService{}, &mockWordListService{}), false},
{"Word List Error", setupService(&mockTransformerService{}, &mockSeparatorService{}, &mockPaddingService{}, &mockWordListErrService{}), true},
{"Transformer Error", setupService(&mockTransformerErrService{}, &mockSeparatorService{}, &mockPaddingService{}, &mockWordListService{}), true},
{"Separator Error", setupService(&mockTransformerService{}, &mockSeparatorErrService{}, &mockPaddingService{}, &mockWordListService{}), true},
Expand Down
2 changes: 1 addition & 1 deletion service/separator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestNewSeparatorService(t *testing.T) {
}
}

func TestSeparatorService_Separate(t *testing.T) {
func TestSeparatorServiceSeparate(t *testing.T) {
t.Parallel()

rngs := &MockRNGService{}
Expand Down
8 changes: 4 additions & 4 deletions service/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *DefaultTransformerService) Transform(slice []string) ([]string, error)
case option.Alternate:
return s.alternate(slice), nil
case option.AlternateLettercase:
return alternateLettercase(slice)
return s.alternateLettercase(slice)
case option.Capitalise:
return s.capitalise(slice), nil
case option.CapitaliseInvert:
Expand All @@ -76,7 +76,7 @@ func (s *DefaultTransformerService) Transform(slice []string) ([]string, error)
case option.Lower:
return s.lower(slice), nil
case option.LowerVowelUpperConsonant:
return lowerVowelUpperConsonant(slice)
return s.lowerVowelUpperConsonant(slice)
case option.Random:
return s.random(slice)
case option.Sentence:
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *DefaultTransformerService) alternate(slice []string) []string {
// or an error if an issue occurs during string building.
//
// Example Output: string[]{"hElLo", "WoRlD"}, nil
func alternateLettercase(slice []string) ([]string, error) {
func (s *DefaultTransformerService) alternateLettercase(slice []string) ([]string, error) {
var result []string
for _, str := range slice {
var sb strings.Builder
Expand Down Expand Up @@ -198,7 +198,7 @@ func isVowel(r rune) bool {
// occurs during the string building process.
//
// Example Output: string[]{"hEllO", "wOrld"}, nil
func lowerVowelUpperConsonant(slice []string) ([]string, error) {
func (s *DefaultTransformerService) lowerVowelUpperConsonant(slice []string) ([]string, error) {
var result []string
for _, str := range slice {
var sb strings.Builder
Expand Down
4 changes: 2 additions & 2 deletions service/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestNewTransformerService(t *testing.T) {
}
}

func TestDefaultTransformerService_Transform(t *testing.T) {
func TestDefaultTransformerServiceTransform(t *testing.T) {
t.Parallel()

rngs := &MockRNGService{}
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestDefaultTransformerService_Transform(t *testing.T) {
}
}

func TestDefaultTransformerService_Validate(t *testing.T) {
func TestDefaultTransformerServiceValidate(t *testing.T) {
t.Parallel()

validCaseTransforms := []string{
Expand Down
2 changes: 1 addition & 1 deletion service/word_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestNewWordListService(t *testing.T) {
}
}

func TestDefaultWordListService_GetWords(t *testing.T) {
func TestDefaultWordListServiceGetWords(t *testing.T) {
t.Parallel()

cfg := &config.Settings{NumWords: 5, WordList: "EN_SMALL", WordLengthMin: 2, WordLengthMax: 10}
Expand Down

0 comments on commit a0a4bc0

Please sign in to comment.