Skip to content

Commit

Permalink
Add badge, change one test to example (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
karelbilek authored Feb 15, 2024
1 parent 1c15f29 commit fa7c7e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# template-parse-recursive

[![Go Reference](https://pkg.go.dev/badge/github.com/karelbilek/template-parse-recursive.svg)](https://pkg.go.dev/github.com/karelbilek/template-parse-recursive)

Tool for parsing go templates recursively.

By default, go's template.ParseGlob does not traverse folders recursively, and uses only filename without folder name as a template name.
Expand Down
41 changes: 27 additions & 14 deletions simple_test.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,71 @@
package recurparse

import (
"embed"
"html/template"
"os"
"strings"
"testing"
"testing/fstest"
)

//go:embed testdata/simple/*.txt
var simple embed.FS
func ExampleHTMLParseFS() {
fsTest := fstest.MapFS{
"data.txt": &fstest.MapFile{
Data: []byte("super simple {{.Foo}}"),
},
"some/deep/file.txt": &fstest.MapFile{
Data: []byte("other simple {{.Foo}}"),
},
}

func TestSimpleNil(t *testing.T) {
tmpl, err := HTMLParseFS(
nil,
simple,
fsTest,
"*.txt",
)
if err != nil {
panic(err)
}

b := &strings.Builder{}
err = tmpl.ExecuteTemplate(b, "testdata/simple/simple.txt", struct{ Foo string }{Foo: "bar"})
err = tmpl.ExecuteTemplate(os.Stdout, "some/deep/file.txt", struct{ Foo string }{Foo: "bar"})
if err != nil {
panic(err)
}
if b.String() != "super simple bar" {
t.Error("not equal super simple bar")
}

// Output: other simple bar
}

func TestSimpleExisting(t *testing.T) {
fsTest := fstest.MapFS{
"data.txt": &fstest.MapFile{
Data: []byte("super simple {{.Foo}}"),
},
"some/deep/file.txt": &fstest.MapFile{
Data: []byte("other simple {{.Foo}}"),
},
}

existing, err := template.New("existing").Parse("existing {{.Foo}}")
if err != nil {
panic(err)
}

tmpl, err := HTMLParseFS(
existing,
simple,
fsTest,
"*.txt",
)
if err != nil {
panic(err)
}

b := &strings.Builder{}
err = tmpl.ExecuteTemplate(b, "testdata/simple/simple.txt", struct{ Foo string }{Foo: "bar"})
err = tmpl.ExecuteTemplate(b, "some/deep/file.txt", struct{ Foo string }{Foo: "bar"})
if err != nil {
panic(err)
}
if b.String() != "super simple bar" {
t.Error("not equal super simple bar")
if b.String() != "other simple bar" {
t.Error("not equal other simple bar")
}

b = &strings.Builder{}
Expand Down
1 change: 0 additions & 1 deletion testdata/simple/simple.txt

This file was deleted.

0 comments on commit fa7c7e3

Please sign in to comment.