Properly parses the template functions, and adds some tests
This commit is contained in:
parent
7dae3def51
commit
1918740563
4 changed files with 73 additions and 12 deletions
|
@ -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}}
|
|
@ -31,7 +31,7 @@ func testConfig(t *testing.T) *model.Config {
|
||||||
return &model.Config{Birthdays: &model.BirthdaysConfig{File: f.Name()}}
|
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 := &TestHelper{t: t}
|
||||||
th.ctrl = gomock.NewController(t)
|
th.ctrl = gomock.NewController(t)
|
||||||
|
|
||||||
|
@ -54,14 +54,16 @@ func SetupTestHelper(t *testing.T) *TestHelper {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
serverOpts := append([]Option{
|
||||||
th.srv, err = New(
|
|
||||||
WithConfig(testConfig(t)),
|
WithConfig(testConfig(t)),
|
||||||
WithLogger(log.New(os.Stderr)),
|
WithLogger(log.New(os.Stderr)),
|
||||||
WithBirthdays(birthdays),
|
WithBirthdays(birthdays),
|
||||||
WithNotificationServices(notificationServices),
|
WithNotificationServices(notificationServices),
|
||||||
WithWorkers(workers),
|
WithWorkers(workers),
|
||||||
)
|
}, opts...)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
th.srv, err = New(serverOpts...)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
th.srv.Start()
|
th.srv.Start()
|
||||||
|
|
|
@ -3,6 +3,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -90,17 +91,20 @@ func New(options ...Option) (*Server, error) {
|
||||||
if srv.Config.Birthdays.Template != "" {
|
if srv.Config.Birthdays.Template != "" {
|
||||||
srv.Logger.Debug("parsing birthday template", "file", 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
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot parse template file %q: %w", srv.Config.Birthdays.Template, err)
|
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
|
return srv, nil
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,3 +40,53 @@ func TestNotify(t *testing.T) {
|
||||||
require.ErrorIs(t, err, mockErr)
|
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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue