From 4334f37fc339ae2e6a7f1181568a3f2e0b282bd6 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 7 Jul 2023 17:59:59 +0800 Subject: [PATCH] :necktie: up: test,map - update some map util func and test utils --- maputil/smap.go | 12 +++++++++ maputil/smap_test.go | 8 ++++++ stdio/iface.go | 7 ++++- testutil/fakeobj/io.go | 21 ++++++++++++--- testutil/fsmock.go | 13 --------- testutil/writer.go | 60 ++++++------------------------------------ 6 files changed, 51 insertions(+), 70 deletions(-) delete mode 100644 testutil/fsmock.go diff --git a/maputil/smap.go b/maputil/smap.go index 47c9ad991..bcc74cc5e 100644 --- a/maputil/smap.go +++ b/maputil/smap.go @@ -29,6 +29,18 @@ func (m SMap) HasValue(val string) bool { return false } +// Load data to the map +func (m SMap) Load(data map[string]string) { + for k, v := range data { + m[k] = v + } +} + +// Set value to the data map +func (m SMap) Set(key string, val any) { + m[key] = strutil.MustString(val) +} + // Value get from the data map func (m SMap) Value(key string) (string, bool) { val, ok := m[key] diff --git a/maputil/smap_test.go b/maputil/smap_test.go index bc6b3c4fb..b2a7b0274 100644 --- a/maputil/smap_test.go +++ b/maputil/smap_test.go @@ -59,4 +59,12 @@ func TestSMap_ToKVPairs(t *testing.T) { str := fmt.Sprint(arr) assert.StrContains(t, str, "k1 23") assert.StrContains(t, str, "k2 ab") + + mp.Set("k3", "true") + assert.Eq(t, "true", mp.Get("k3")) + + mp.Load(map[string]string{ + "k4": "1,2", + }) + assert.Eq(t, "1,2", mp.Get("k4")) } diff --git a/stdio/iface.go b/stdio/iface.go index d4e6dc0a8..36c28812b 100644 --- a/stdio/iface.go +++ b/stdio/iface.go @@ -7,6 +7,11 @@ type Flusher interface { Flush() error } +// Syncer interface +type Syncer interface { + Sync() error +} + // FlushWriter is the interface satisfied by logging destinations. type FlushWriter interface { Flusher @@ -24,7 +29,7 @@ type FlushCloseWriter interface { // SyncCloseWriter is the interface satisfied by logging destinations. // such as os.File type SyncCloseWriter interface { - Flusher + Syncer // WriteCloser the output writer io.WriteCloser } diff --git a/testutil/fakeobj/io.go b/testutil/fakeobj/io.go index 8fd341a8f..bd356fcc4 100644 --- a/testutil/fakeobj/io.go +++ b/testutil/fakeobj/io.go @@ -1,17 +1,20 @@ package fakeobj import ( - "bytes" "errors" + + "github.com/gookit/goutil/byteutil" ) -// Writer implements the io.Writer, io.Flusher, io.Closer. +// Writer implements the io.Writer, stdio.Flusher, io.Closer. type Writer struct { - bytes.Buffer + byteutil.Buffer // ErrOnWrite return error on write, useful for testing ErrOnWrite bool // ErrOnFlush return error on flush, useful for testing ErrOnFlush bool + // ErrOnSync return error on flush, useful for testing + ErrOnSync bool // ErrOnClose return error on close, useful for testing ErrOnClose bool } @@ -67,7 +70,7 @@ func (w *Writer) Close() error { return nil } -// Flush implements +// Flush implements stdio.Flusher func (w *Writer) Flush() error { if w.ErrOnFlush { return errors.New("fake flush error") @@ -76,3 +79,13 @@ func (w *Writer) Flush() error { w.Reset() return nil } + +// Sync implements stdio.Syncer +func (w *Writer) Sync() error { + if w.ErrOnSync { + return errors.New("fake sync error") + } + + w.Reset() + return nil +} diff --git a/testutil/fsmock.go b/testutil/fsmock.go deleted file mode 100644 index c91f20efb..000000000 --- a/testutil/fsmock.go +++ /dev/null @@ -1,13 +0,0 @@ -package testutil - -import ( - "github.com/gookit/goutil/testutil/fakeobj" -) - -// DirEnt implements the fs.DirEntry -type DirEnt = fakeobj.DirEntry - -// NewDirEnt create a fs.DirEntry -func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry { - return fakeobj.NewDirEntry(fpath, isDir...) -} diff --git a/testutil/writer.go b/testutil/writer.go index e8b5c31e0..103ca8b28 100644 --- a/testutil/writer.go +++ b/testutil/writer.go @@ -1,65 +1,21 @@ package testutil import ( - "errors" + "github.com/gookit/goutil/testutil/fakeobj" ) -// TestWriter struct, useful for testing -type TestWriter struct { - Buffer - // ErrOnWrite return error on write, useful for testing - ErrOnWrite bool - // ErrOnFlush return error on flush, useful for testing - ErrOnFlush bool - // ErrOnClose return error on close, useful for testing - ErrOnClose bool -} +// TestWriter struct, useful for testing. alias of fakeobj.Writer +type TestWriter = fakeobj.Writer // NewTestWriter instance func NewTestWriter() *TestWriter { return &TestWriter{} } -// SetErrOnWrite method -func (w *TestWriter) SetErrOnWrite() *TestWriter { - w.ErrOnWrite = true - return w -} - -// SetErrOnFlush method -func (w *TestWriter) SetErrOnFlush() *TestWriter { - w.ErrOnFlush = true - return w -} - -// SetErrOnClose method -func (w *TestWriter) SetErrOnClose() *TestWriter { - w.ErrOnClose = true - return w -} - -// Flush implements -func (w *TestWriter) Flush() error { - if w.ErrOnFlush { - return errors.New("flush error") - } - - w.Reset() - return nil -} - -// Close implements -func (w *TestWriter) Close() error { - if w.ErrOnClose { - return errors.New("close error") - } - return nil -} +// DirEnt implements the fs.DirEntry +type DirEnt = fakeobj.DirEntry -// Write implements -func (w *TestWriter) Write(p []byte) (n int, err error) { - if w.ErrOnWrite { - return 0, errors.New("write error") - } - return w.Buffer.Write(p) +// NewDirEnt create a fs.DirEntry +func NewDirEnt(fpath string, isDir ...bool) *fakeobj.DirEntry { + return fakeobj.NewDirEntry(fpath, isDir...) }