Skip to content

Commit

Permalink
refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iagapie committed Jul 6, 2023
1 parent a0f7ae1 commit e619447
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ jobs:
- name: Init Go modules Cache # Docs: <https://git.io/JfAKn#go---modules>
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-

Expand Down
70 changes: 57 additions & 13 deletions template_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package et_test
import (
"bytes"
"context"
"errors"
"fmt"
et "github.com/gowool/extends-template"
"github.com/stretchr/testify/assert"
Expand All @@ -12,6 +13,7 @@ import (
)

const (
htmlGlobal = `{{define "title_h1"}}<h1>{{.}}</h1>{{end}}`
htmlLayout = `<body>{{block "content" .}}{{end}}</body>`
htmlTitle = `<h1>Title Test</h1>`
htmlSubtitle = `<h2>Subtitle Test</h2>`
Expand All @@ -20,6 +22,7 @@ const (
)

var htmlViews = map[string][]byte{
"@main/global.html": []byte(htmlGlobal),
"@main/layout.html": []byte(htmlLayout),
"@main/title.html": []byte(htmlTitle),
"@main/subtitle.html": []byte(htmlSubtitle),
Expand Down Expand Up @@ -49,6 +52,7 @@ func (l wrapLoader) Exists(_ context.Context, name string) (bool, error) {

func TestTemplateWrapper_IsFresh(t *testing.T) {
scenarios := []struct {
handlers []et.Handler
t int64
expected bool
}{
Expand All @@ -60,14 +64,23 @@ func TestTemplateWrapper_IsFresh(t *testing.T) {
t: time.Now().Add(-24 * time.Hour).Unix(),
expected: true,
},
{
t: time.Now().Add(-24 * time.Hour).Unix(),
expected: false,
handlers: []et.Handler{
func(_ context.Context, _ *et.Node, _ string) error {
return errors.New("handler error")
},
},
},
}

for _, s := range scenarios {
name := "@main/view.html"
wrapper := et.NewTemplateWrapper(
template.New(name),
wrapLoader{t: s.t},
nil,
s.handlers,
et.ReExtends("{{", "}}"),
et.ReTemplate("{{", "}}"))

Expand All @@ -79,18 +92,49 @@ func TestTemplateWrapper_IsFresh(t *testing.T) {

func TestTemplateWrapper_Parse(t *testing.T) {
name := "@main/view.html"
wrapper := et.NewTemplateWrapper(
template.New(name),
wrapLoader{},
nil,
et.ReExtends("{{", "}}"),
et.ReTemplate("{{", "}}"))

for range []struct{}{{}, {}} {
if err := wrapper.Parse(context.TODO()); assert.NoError(t, err) && assert.NotNil(t, wrapper.HTML) {
var out bytes.Buffer
if err = wrapper.HTML.ExecuteTemplate(&out, name, nil); assert.NoError(t, err) {
assert.Equal(t, htmlResult, out.String())

scenarios := []struct {
handlers []et.Handler
isError bool
}{
{
handlers: []et.Handler{
func(_ context.Context, _ *et.Node, _ string) error {
return nil
},
},
isError: false,
},
{
handlers: []et.Handler{
func(_ context.Context, _ *et.Node, _ string) error {
return errors.New("handler error")
},
},
isError: true,
},
}

for _, s := range scenarios {
wrapper := et.NewTemplateWrapper(
template.New(name),
wrapLoader{},
s.handlers,
et.ReExtends("{{", "}}"),
et.ReTemplate("{{", "}}"),
"@main/global.html",
)

for range []struct{}{{}, {}} {
err := wrapper.Parse(context.TODO())

if s.isError {
assert.Error(t, err)
} else if assert.NoError(t, err) && assert.NotNil(t, wrapper.HTML) {
var out bytes.Buffer
if err = wrapper.HTML.ExecuteTemplate(&out, name, nil); assert.NoError(t, err) {
assert.Equal(t, htmlResult, out.String())
}
}
}
}
Expand Down

0 comments on commit e619447

Please sign in to comment.