diff options
author | n-peugnet <n.peugnet@free.fr> | 2021-09-22 21:41:48 +0200 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2021-09-22 21:41:48 +0200 |
commit | bded863524b701332bbc6e20ae72468301015c36 (patch) | |
tree | 9f31069e22aea7fdb6015506491dcdbc403774d0 /utils | |
parent | 3ffc6cb0218ecd1868efdb76a4930925097da190 (diff) | |
download | dna-backup-bded863524b701332bbc6e20ae72468301015c36.tar.gz dna-backup-bded863524b701332bbc6e20ae72468301015c36.zip |
add tests for io utils
Diffstat (limited to 'utils')
-rw-r--r-- | utils/io_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/utils/io_test.go b/utils/io_test.go new file mode 100644 index 0000000..5f9c8c7 --- /dev/null +++ b/utils/io_test.go @@ -0,0 +1,71 @@ +package utils_test + +import ( + "bytes" + "io" + "testing" + + "github.com/n-peugnet/dna-backup/utils" +) + +func TestNopCloser(t *testing.T) { + var buff bytes.Buffer + w := utils.NopCloser(&buff) + w.Close() + n, err := buff.WriteString("test") + if err != nil { + t.Error(err) + } + if n != 4 { + t.Error("expected: 4, actual:", n) + } +} + +type wrapper struct { + n string + r utils.ReadWrapper + w utils.WriteWrapper +} + +var wrappers = []wrapper{ + {"Zlib", utils.ZlibReader, utils.ZlibWriter}, + {"Nop", utils.NopReadWrapper, utils.NopWriteWrapper}, +} + +func TestWrappers(t *testing.T) { + 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) + } + } +} |