diff --git a/sample.tmpl b/sample.tmpl index 54ebaef..0b40e71 100644 --- a/sample.tmpl +++ b/sample.tmpl @@ -1 +1 @@ -¡Mañana es el cumpleaños de {{.Name}}! Puedes felicitarle o bien escribiendo a {{.Email}} o bien llamando al número {{.Phone}} \ No newline at end of file +¡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}} \ No newline at end of file diff --git a/server/helpers_test.go b/server/helpers_test.go index 1cdc576..e73e2c7 100644 --- a/server/helpers_test.go +++ b/server/helpers_test.go @@ -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() diff --git a/server/server.go b/server/server.go index 4d1caad..1c0e715 100644 --- a/server/server.go +++ b/server/server.go @@ -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 diff --git a/server/server_test.go b/server/server_test.go index 6d8d5d9..3da18b9 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -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()) + }) +}