Skip to content

Commit

Permalink
(template): Add test for ExecuteTemplate; use variable for template i…
Browse files Browse the repository at this point in the history
…n main.go
  • Loading branch information
KevinGimbel committed Jun 18, 2017
1 parent 1a00f08 commit c105af8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"io/ioutil"
"math/rand"
"os"
"text/template"
"time"
)

var version = "master"
var tmp *template.Template

func getFormatter(format, table string) (f fakedata.Formatter, err error) {
switch format {
Expand Down Expand Up @@ -83,29 +85,35 @@ func main() {
rand.Seed(time.Now().UnixNano())

if *templateFlag != "" {
template, err := fakedata.ParseTemplate(*templateFlag)
t, err := fakedata.ParseTemplate(*templateFlag)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fakedata.ExecuteTemplate(template, *limitFlag)
os.Exit(0)
tmp = t
}

if isPipe() {
t, err := ioutil.ReadAll(os.Stdin)
tp, err := ioutil.ReadAll(os.Stdin)

if err != nil {
fmt.Printf("Unable to read input: %s", err)
os.Exit(1)
}

template, err := fakedata.ParseTemplateFromPipe(string(t))
t, err := fakedata.ParseTemplateFromPipe(string(tp))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fakedata.ExecuteTemplate(template, *limitFlag)
tmp = t
}
// Execute a template if there is one
if tmp != nil {
if err := fakedata.ExecuteTemplate(tmp, *limitFlag); err != nil {
fmt.Println(err)
}

os.Exit(0)
}

Expand Down
31 changes: 30 additions & 1 deletion pkg/fakedata/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fakedata

import (
"testing"
"text/template"
)

func TestCreateConstraints(t *testing.T) {
Expand Down Expand Up @@ -64,9 +65,37 @@ func TestParseTemplateFromPipe(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ParseTemplateFromPipe(tt.input, 1); err != nil && (tt.wantErr != true) {
if _, err := ParseTemplateFromPipe(tt.input); err != nil && (tt.wantErr != true) {
t.Errorf("template.ParseTemplateFromPipe = %v, want %v", err, tt.wantErr)
}
})
}
}

func TestExecuteTemplate(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"simple template", "{{ Name }}", false},
{"two generators", "{{ Name }} {{ Date }}", false},
{"undefined function", "{{ GetDataFromUrl http://fake.link/ }}", true},
{"printf in template", "{{ printf \"%s %s\" NameLast NameFirst }}", false},
{"unclosed range", "{{range Loop 5}} {{Name}}", true},
{"template variables", "{{$a := Name }} {{ $a }}", false},
{"function with parameters", "{{ Enum \"Lorem\" \"Ipsum\"}}", false},
{"invalid template", "{{ Name, {{ Int 15 20 }}, }}", true},
{"text template", "Hello, World!", false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := template.New(tt.name).Funcs(generatorFunctions).Parse(tt.input)
if err != nil && !tt.wantErr {
t.Errorf("template.ExecuteTemplate error = %v, want %v", err, tt.wantErr)
}

})
}
}

0 comments on commit c105af8

Please sign in to comment.