Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,26 @@ func New(dirPath string, sharedPath string, fileSuffix string) (TemplateEngine,

func (te *TemplateEngine) Parse(data TemplateData) ([]TemplateData, error) {
result := []TemplateData{}

for name, tmpl := range te.templates {

newData, err := parse(name, tmpl, data, te.funcs, te.sharedTemplates)
if err != nil {
return result, err
}

fileEx := filepath.Ext(newData.To)
if !strings.Contains(fileEx, "go") {
result = append(result, newData)
continue
}

formattedOut, err := format.Source(newData.Output)
if err != nil {
log.Printf(chalk.Red("error formatting: %s"), err)
return result, err
}

newData.Output = formattedOut
result = append(result, newData)
}
Expand Down
72 changes: 58 additions & 14 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/code-gorilla-au/odize"
"github.com/code-gorilla-au/pyrotic/internal/testfixtures"
)

func Test_withTemplates(t *testing.T) {
Expand Down Expand Up @@ -52,25 +53,68 @@ func Test_withTemplates(t *testing.T) {
}
}

func TestTmplEngine_Parse_should_render(t *testing.T) {
strTmp := `---
to: elo
---
blah
`
func TestTmplEngine_Parse(t *testing.T) {
group := odize.NewGroup(t, nil)

expected := TemplateData{
Name: "hello",
}
te := &TemplateEngine{
templates: map[string]string{"tmp": strTmp},
funcs: defaultFuncs,
}
data, err := te.Parse(expected)

var te *TemplateEngine

group.BeforeEach(func() {
strGoFile := `---
to: elo.go
---
blah
`

strNonGoFile := `---
to: {{ "elo" | caseSnake }}
---
blah
`

te = &TemplateEngine{
templates: map[string]string{
"tmp": strGoFile,
"tmp2": strNonGoFile,
},
funcs: defaultFuncs,
}
})

err := group.
Test("should parse go file and format", func(t *testing.T) {
data, err := te.Parse(expected)
odize.AssertNoError(t, err)

result, found := testfixtures.Find(data, func(d TemplateData) bool {
return d.To == "elo.go"
})
odize.AssertTrue(t, found)

odize.AssertEqual(t, expected.Name, result.Name)
odize.AssertEqual(t, "elo.go", result.To)
odize.AssertEqual(t, "blah", strings.TrimSpace(string(result.Output)))
}).
Test("should parse non go file", func(t *testing.T) {
data, err := te.Parse(expected)

result, found := testfixtures.Find(data, func(d TemplateData) bool {
return d.To == "elo"
})
odize.AssertTrue(t, found)

odize.AssertNoError(t, err)
odize.AssertEqual(t, expected.Name, result.Name)
odize.AssertEqual(t, "elo", result.To)
odize.AssertEqual(t, "blah", strings.TrimSpace(string(result.Output)))
}).
Run()
odize.AssertNoError(t, err)
odize.AssertEqual(t, expected.Name, data[0].Name)
odize.AssertEqual(t, "elo", data[0].To)
odize.AssertEqual(t, "blah", strings.TrimSpace(string(data[0].Output)))
}

func TestTmplEngine_Parse_missing_funcs_should_fail_on_meta_parse(t *testing.T) {
strTmp := `---
to: {{ "elo" | caseSnake }}
Expand Down
11 changes: 11 additions & 0 deletions internal/testfixtures/testfixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package testfixtures

func Find[T any](data []T, fn func(T) bool) (T, bool) {
for _, item := range data {
if fn(item) {
return item, true
}
}
var zero T
return zero, false
}
3 changes: 3 additions & 0 deletions scripts/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test: test-unit test-integration ## Run all tests
test-unit: ## Run unit tests
go test -coverprofile $(COVER_OUTPUT_RAW) --short -cover -failfast ./...

test-unit-watch: ## Run unit tests in watch mode
gow test -coverprofile $(COVER_OUTPUT_RAW) --short -cover -failfast ./...

test-integration: build ## Run integration test
ENV=DEV ./pyrotic -p example/_templates generate fakr --meta foo=bar,bin=baz,enum_list=a-long-list

Expand Down