Skip to content

Commit

Permalink
👔 up: test,map - update some map util func and test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 7, 2023
1 parent e1e16d5 commit 4334f37
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 70 deletions.
12 changes: 12 additions & 0 deletions maputil/smap.go
Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions maputil/smap_test.go
Expand Up @@ -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"))
}
7 changes: 6 additions & 1 deletion stdio/iface.go
Expand Up @@ -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
Expand All @@ -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
}
21 changes: 17 additions & 4 deletions 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
}
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
13 changes: 0 additions & 13 deletions testutil/fsmock.go

This file was deleted.

60 changes: 8 additions & 52 deletions 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...)
}

0 comments on commit 4334f37

Please sign in to comment.