Properly parses the template functions, and adds some tests

This commit is contained in:
Miguel de la Cruz 2023-07-11 10:10:56 +02:00
parent 7dae3def51
commit 1918740563
4 changed files with 73 additions and 12 deletions

View File

@ -1 +1 @@
¡Mañana es el cumpleaños de {{.Name}}! Puedes felicitarle o bien escribiendo a {{.Email}} o bien llamando al número {{.Phone}}
¡Mañana es el cumpleaños de {{.Name}}! Cumple {{getYearsOld .YearOfBirth}} años, puedes felicitarle o bien escribiendo a {{.Email}} o bien llamando al número {{.Phone}}

View File

@ -31,7 +31,7 @@ func testConfig(t *testing.T) *model.Config {
return &model.Config{Birthdays: &model.BirthdaysConfig{File: f.Name()}}
}
func SetupTestHelper(t *testing.T) *TestHelper {
func SetupTestHelper(t *testing.T, opts ...Option) *TestHelper {
th := &TestHelper{t: t}
th.ctrl = gomock.NewController(t)
@ -54,14 +54,16 @@ func SetupTestHelper(t *testing.T) *TestHelper {
},
}
var err error
th.srv, err = New(
serverOpts := append([]Option{
WithConfig(testConfig(t)),
WithLogger(log.New(os.Stderr)),
WithBirthdays(birthdays),
WithNotificationServices(notificationServices),
WithWorkers(workers),
)
}, opts...)
var err error
th.srv, err = New(serverOpts...)
require.NoError(t, err)
th.srv.Start()

View File

@ -3,6 +3,7 @@ package server
import (
"errors"
"fmt"
"path"
"text/template"
"time"
@ -90,17 +91,20 @@ func New(options ...Option) (*Server, error) {
if srv.Config.Birthdays.Template != "" {
srv.Logger.Debug("parsing birthday template", "file", srv.Config.Birthdays.Template)
funcs := template.FuncMap{
"getYearsOld": func(yearOfBirth int) int {
return time.Now().Year() - yearOfBirth
},
}
var err error
srv.tmpl, err = template.ParseFiles(srv.Config.Birthdays.Template)
srv.tmpl, err = template.
New(path.Base(srv.Config.Birthdays.Template)).
Funcs(funcs).
ParseFiles(srv.Config.Birthdays.Template)
if err != nil {
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.Config.Birthdays.Template, err)
}
srv.tmpl.Funcs(template.FuncMap{
"GetYearsOld": func(yearOfBirth int) int {
return time.Now().Year() - yearOfBirth
},
})
}
return srv, nil

View File

@ -1,9 +1,14 @@
package server
import (
"bytes"
"errors"
"fmt"
"os"
"testing"
"time"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"github.com/stretchr/testify/require"
)
@ -35,3 +40,53 @@ func TestNotify(t *testing.T) {
require.ErrorIs(t, err, mockErr)
})
}
func TestTemplate(t *testing.T) {
t.Run("template should work with birthday data", func(t *testing.T) {
// create a template file and populate it
f, err := os.CreateTemp("", "birthdaybot-config-")
require.NoError(t, err)
_, werr := fmt.Fprint(f, "My name is {{.Name}}")
require.NoError(t, werr)
require.NoError(t, f.Close())
// create a test config and set the template
config := testConfig(t)
config.Birthdays.Template = f.Name()
// create the test helper with the custom config
th := SetupTestHelper(t, WithConfig(config))
defer th.TearDown()
birthday := &model.Birthday{Name: "Jane Doe"}
expectedString := "My name is Jane Doe"
var stringBuffer bytes.Buffer
require.NoError(t, th.srv.tmpl.Execute(&stringBuffer, birthday))
require.Equal(t, expectedString, stringBuffer.String())
})
t.Run("template should work with custom functions", func(t *testing.T) {
// create a template file and populate it
f, err := os.CreateTemp("", "birthdaybot-config-")
require.NoError(t, err)
_, werr := fmt.Fprint(f, "I'm getting {{getYearsOld .YearOfBirth}} years old")
require.NoError(t, werr)
require.NoError(t, f.Close())
// create a test config and set the template
config := testConfig(t)
config.Birthdays.Template = f.Name()
// create the test helper with the custom config
th := SetupTestHelper(t, WithConfig(config))
defer th.TearDown()
birthday := &model.Birthday{YearOfBirth: 1980}
expectedString := fmt.Sprintf("I'm getting %d years old", time.Now().Year()-birthday.YearOfBirth)
var stringBuffer bytes.Buffer
require.NoError(t, th.srv.tmpl.Execute(&stringBuffer, birthday))
require.Equal(t, expectedString, stringBuffer.String())
})
}