From b7a87fb15b50d2f5365f3c084616b2d4e2f1fa34 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 28 Oct 2024 21:30:50 +0100 Subject: [PATCH] Add tests for MsgWriter_writeString function Implemented tests to validate the MsgWriter writeString method for successful writes, failure scenarios, and handling pre-existing errors in the writer. This improves coverage and ensures robustness of the MsgWriter component. --- msgwriter_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/msgwriter_test.go b/msgwriter_test.go index 6b9a9d7..10650e5 100644 --- a/msgwriter_test.go +++ b/msgwriter_test.go @@ -468,3 +468,34 @@ func TestMsgWriter_writePart(t *testing.T) { } }) } + +func TestMsgWriter_writeString(t *testing.T) { + msgwriter := &msgWriter{ + charset: CharsetUTF8, + encoder: getEncoder(EncodingQP), + } + t.Run("writeString succeeds", func(t *testing.T) { + buffer := bytes.NewBuffer(nil) + msgwriter.writer = buffer + msgwriter.writeString("thisisatest") + if !strings.EqualFold(buffer.String(), "thisisatest") { + t.Errorf("writeString failed, expected: thisisatest got: %s", buffer.String()) + } + }) + t.Run("writeString fails", func(t *testing.T) { + msgwriter.writer = failReadWriteSeekCloser{} + msgwriter.writeString("thisisatest") + if msgwriter.err == nil { + t.Errorf("writeString succeeded, expected error") + } + }) + t.Run("writeString on errored writer should return", func(t *testing.T) { + buffer := bytes.NewBuffer(nil) + msgwriter.writer = buffer + msgwriter.err = errors.New("intentional error") + msgwriter.writeString("thisisatest") + if !strings.EqualFold(buffer.String(), "") { + t.Errorf("writeString succeeded, expected: empty string, got: %s", buffer.String()) + } + }) +}