From 4db66696a6ed004a42c3c78623750d445def7b1f Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 27 Oct 2024 14:56:34 +0100 Subject: [PATCH] Add tests for SetBodyTextTemplate method in msg_test.go Implemented thorough unit tests to validate the SetBodyTextTemplate method in msg_test.go. These tests cover default behavior, handling of nil templates, and invalid templates, ensuring robustness and reliability. --- msg_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/msg_test.go b/msg_test.go index e9488bc..cee2f95 100644 --- a/msg_test.go +++ b/msg_test.go @@ -15,6 +15,7 @@ import ( "reflect" "strings" "testing" + ttpl "text/template" "time" ) @@ -3977,6 +3978,75 @@ func TestMsg_SetBodyHTMLTemplate(t *testing.T) { }) } +func TestMsg_SetBodyTextTemplate(t *testing.T) { + tplString := `Teststring: {{.teststring}}` + invalidTplString := `Teststring: {{call $.invalid .teststring}}` + data := map[string]interface{}{"teststring": "this is a test"} + textTpl, err := ttpl.New("texttpl").Parse(tplString) + if err != nil { + t.Fatalf("failed to parse Text template: %s", err) + } + invalidTpl, err := ttpl.New("texttpl").Parse(invalidTplString) + if err != nil { + t.Fatalf("failed to parse invalid Text template: %s", err) + } + t.Run("SetBodyTextTemplate default", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + if err = message.SetBodyTextTemplate(textTpl, data); err != nil { + t.Fatalf("failed to set body text template: %s", err) + } + parts := message.GetParts() + if len(parts) != 1 { + t.Fatalf("expected 1 part, got: %d", len(parts)) + } + if parts[0] == nil { + t.Fatal("expected part to be not nil") + } + if parts[0].contentType != TypeTextPlain { + t.Errorf("expected contentType to be %s, got: %s", TypeTextPlain, parts[0].contentType) + } + messageBuf := bytes.NewBuffer(nil) + _, err = parts[0].writeFunc(messageBuf) + if err != nil { + t.Errorf("writeFunc failed: %s", err) + } + if !strings.EqualFold(messageBuf.String(), "Teststring: this is a test") { + t.Errorf("expected message body to be %s, got: %s", "Teststring: this is a test", messageBuf.String()) + } + }) + t.Run("SetBodyTextTemplate with nil tpl", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + err = message.SetBodyTextTemplate(nil, nil) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.EqualFold(err.Error(), errTplPointerNil) { + t.Errorf("expected error to be %s, got: %s", errTplPointerNil, err.Error()) + } + }) + t.Run("SetBodyTextTemplate with invalid tpl", func(t *testing.T) { + message := NewMsg() + if message == nil { + t.Fatal("message is nil") + } + err = message.SetBodyTextTemplate(invalidTpl, data) + if err == nil { + t.Fatal("expected error, got nil") + } + expectErr := `failed to execute template: template: texttpl:1:14: executing "texttpl" at : error calling call: call of nil` + if !strings.EqualFold(err.Error(), expectErr) { + t.Errorf("expected error to be %s, got: %s", expectErr, err.Error()) + } + }) +} + /* // TestNewMsgWithMiddleware tests WithMiddleware