Skip to content

Commit

Permalink
(template): Add & Update Tests; Rewrite Helper funcs; Updates #45
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGimbel committed Jun 1, 2017
1 parent 4f9d978 commit 89cab85
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
11 changes: 6 additions & 5 deletions integration/help.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Usage: fakedata [option ...] field...

-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
-v, --version shows version information
-f, --format string generators rows in f format. Available formats: csv|tab|sql
-g, --generators lists available generators
-l, --limit int limits rows up to n (default 10)
-t, --table string table name of the sql format (default "TABLE")
--template string Use template as input
-v, --version shows version information
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var limitFlag = flag.IntP("limit", "l", 10, "limits rows up to n")
var formatFlag = flag.StringP("format", "f", "", "generators rows in f format. Available formats: csv|tab|sql")
var versionFlag = flag.BoolP("version", "v", false, "shows version information")
var tableFlag = flag.StringP("table", "t", "TABLE", "table name of the sql format")
var templateFlag = flag.StringP("template", "", "", "Use template")
var templateFlag = flag.StringP("template", "", "", "Use template as input")

func getFormatter(format string) (f fakedata.Formatter) {
switch format {
Expand Down
10 changes: 9 additions & 1 deletion pkg/fakedata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ func createConstraints(params []string) string {
}

func getTemplateNameFromPath(name string) string {
ts := strings.Split(name, "/")
ts := strings.FieldsFunc(name, splitPathName)
tn := ts[len(ts)-1]
return tn
}

// this custom split function is used with strings.FieldsFunc to split the path
// by `/` (Unix, MacOS) or `\` (Windows) for absolute and relative paths to template files
func splitPathName(r rune) bool {
return r == '/' || r == '\\'
}

// ParseTemplate takes a path to a template file as argument. It parses the template file and executes it on
// os.Stdout.
func ParseTemplate(path string) {
tn := getTemplateNameFromPath(path)
tmp, err := template.New(tn).Funcs(generatorFunctions).ParseFiles(path)
Expand Down
48 changes: 48 additions & 0 deletions pkg/fakedata/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fakedata

import (
"testing"
)

func TestCreateConstraints(t *testing.T) {
tests := []struct {
name string
input []string
want string
}{
{"two integers", []string{"1", "100"}, "1..100"},
{"three word enum constraints", []string{"feat", "issue", "docs"}, "feat..issue..docs"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := createConstraints(tt.input); got != tt.want {
t.Errorf("template.createConstraints = %v, want %v", got, tt.want)
}
})
}
}

func TestGetTemplateNameFromPath(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{"one word template name", "template.tmpl", "template.tmpl"},
{"absolute template path (unix)", "/var/www/home.tmpl", "home.tmpl"},
{"absolute template path (Windows)", "C:\\Data\\Templates\\test.tmpl", "test.tmpl"},
{"relative template path (Unix)", "./views/user/account.tmpl", "account.tmpl"},
{"relative template path (Windows)", "views\\user\\account.tmpl", "account.tmpl"},
{"spaced template path (Unix)", "./views/user account/account.tmpl", "account.tmpl"},
{"spaced template path (Windows)", "views\\user account\\account.tmpl", "account.tmpl"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getTemplateNameFromPath(tt.input); got != tt.want {
t.Errorf("template.getTemplateNameFromPath = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 89cab85

Please sign in to comment.