Skip to content

Commit

Permalink
Support auto detection (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw authored Jun 25, 2022
1 parent b64e1a9 commit 2e6d375
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Run(arguments []string, out io.Writer, version string) (err error) {

outputFormat := flag.String(
"format",
string(model.FormatGo),
string(model.FormatAutoDetect),
"output format: "+strings.Join(model.Formats(), ", "),
)
templateFile := flag.String(
Expand Down
18 changes: 18 additions & 0 deletions internal/app/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app
import (
"fmt"
"io"
"path"
"strings"

"github.com/hedhyw/gherkingen/v2/internal/docplugin/goplugin"
"github.com/hedhyw/gherkingen/v2/internal/docplugin/multiplugin"
Expand All @@ -22,6 +24,10 @@ func runGenerator(
return err
}

if outputFormat == model.FormatAutoDetect {
outputFormat = detectFormat(templateFile)
}

inputSource, err := readInput(inputFile)
if err != nil {
return err
Expand All @@ -42,3 +48,15 @@ func runGenerator(

return nil
}

func detectFormat(templateFile string) model.Format {
templateFile = strings.ToLower(templateFile)
switch path.Ext(templateFile) {
case ".go":
return model.FormatGo
case ".tmpl":
return detectFormat(strings.TrimSuffix(templateFile, ".tmpl"))
default:
return model.FormatRaw
}
}
41 changes: 41 additions & 0 deletions internal/app/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app

import (
"testing"

"github.com/hedhyw/gherkingen/v2/internal/model"

"github.com/stretchr/testify/assert"
)

func TestDetectFormat(t *testing.T) {
t.Parallel()

testCases := [...]struct {
Filename string
Expected model.Format
}{{
Filename: "example.go",
Expected: model.FormatGo,
}, {
Filename: "example.rb",
Expected: model.FormatRaw,
}, {
Filename: "example.json",
Expected: model.FormatRaw,
}, {
Filename: "example.GO",
Expected: model.FormatGo,
}, {
Filename: "example.go.tmpl",
Expected: model.FormatGo,
}, {
Filename: "example.rb.tmpl",
Expected: model.FormatRaw,
}}

for _, tc := range testCases {
actual := detectFormat(tc.Filename)
assert.Equal(t, tc.Expected, actual, tc.Filename)
}
}
8 changes: 5 additions & 3 deletions internal/model/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ type Format string

// Possible formats.
const (
FormatJSON Format = "json"
FormatGo Format = "go"
FormatRaw Format = "raw"
FormatAutoDetect Format = "autodetect"
FormatJSON Format = "json"
FormatGo Format = "go"
FormatRaw Format = "raw"
)

// Formats returns supported output formats.
func Formats() []string {
return []string{
string(FormatAutoDetect),
string(FormatJSON),
string(FormatGo),
string(FormatRaw),
Expand Down
1 change: 1 addition & 0 deletions internal/model/enums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestFormats(t *testing.T) {
actualFormats := model.Formats()

expFormats := [...]string{
string(model.FormatAutoDetect),
string(model.FormatJSON),
string(model.FormatGo),
string(model.FormatRaw),
Expand Down

0 comments on commit 2e6d375

Please sign in to comment.