From b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Thu, 23 Sep 2021 01:20:25 +0200 Subject: better tests (util/io subtests + check log output) --- utils/io_test.go | 81 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 38 deletions(-) (limited to 'utils') diff --git a/utils/io_test.go b/utils/io_test.go index 5f9c8c7..ad1a576 100644 --- a/utils/io_test.go +++ b/utils/io_test.go @@ -27,45 +27,50 @@ type wrapper struct { w utils.WriteWrapper } -var wrappers = []wrapper{ - {"Zlib", utils.ZlibReader, utils.ZlibWriter}, - {"Nop", utils.NopReadWrapper, utils.NopWriteWrapper}, -} - func TestWrappers(t *testing.T) { + wrappers := []wrapper{ + {"Zlib", utils.ZlibReader, utils.ZlibWriter}, + {"Nop", utils.NopReadWrapper, utils.NopWriteWrapper}, + } for _, wrapper := range wrappers { - var buff bytes.Buffer - var err error - w := wrapper.w(&buff) - n, err := w.Write([]byte("test")) - if err != nil { - t.Error(wrapper.n, err) - } - if n != 4 { - t.Error(wrapper.n, "expected: 4, actual:", n) - } - err = w.Close() - if err != nil { - t.Error(wrapper.n, err) - } - r, err := wrapper.r(&buff) - if err != nil { - t.Error(wrapper.n, err) - } - b := make([]byte, 4) - n, err = r.Read(b) - if n != 4 { - t.Error(wrapper.n, "expected: 4, actual:", n) - } - if err != nil && err != io.EOF { - t.Error(wrapper.n, err) - } - if !bytes.Equal(b, []byte("test")) { - t.Errorf("%s, expected: %q, actual: %q", "test", wrapper.n, b) - } - n, err = r.Read(b) - if err != io.EOF { - t.Error(wrapper.n, err) - } + t.Run(wrapper.n, func(t *testing.T) { + testWrapper(t, wrapper) + }) + } +} + +func testWrapper(t *testing.T, wrapper wrapper) { + var buff bytes.Buffer + var err error + w := wrapper.w(&buff) + n, err := w.Write([]byte("test")) + if err != nil { + t.Error(wrapper.n, err) + } + if n != 4 { + t.Error(wrapper.n, "expected: 4, actual:", n) + } + err = w.Close() + if err != nil { + t.Error(wrapper.n, err) + } + r, err := wrapper.r(&buff) + if err != nil { + t.Error(wrapper.n, err) + } + b := make([]byte, 4) + n, err = r.Read(b) + if n != 4 { + t.Error(wrapper.n, "expected: 4, actual:", n) + } + if err != nil && err != io.EOF { + t.Error(wrapper.n, err) + } + if !bytes.Equal(b, []byte("test")) { + t.Errorf("%s, expected: %q, actual: %q", "test", wrapper.n, b) + } + n, err = r.Read(b) + if err != io.EOF { + t.Error(wrapper.n, err) } } -- cgit v1.2.3