diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-09-23 01:20:25 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-09-23 01:20:25 +0200 |
commit | b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06 (patch) | |
tree | 65f792759394d1bc08e18d6987e4da1887990376 /utils | |
parent | cac8795d33cd52d362e02590b4df4ccd9b96ff71 (diff) | |
download | dna-backup-b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06.tar.gz dna-backup-b3b6e4bf6c9a14514bcfb8406d9cc86d9d18bd06.zip |
better tests (util/io subtests + check log output)
Diffstat (limited to 'utils')
-rw-r--r-- | utils/io_test.go | 81 |
1 files changed, 43 insertions, 38 deletions
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) } } |