Skip to content

Commit

Permalink
✅ test: all - add more unit test case and update some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 27, 2023
1 parent 3dfc858 commit 5b354e6
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 33 deletions.
24 changes: 12 additions & 12 deletions fsutil/operate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,38 @@ func TestCreateFile(t *testing.T) {
// return
// }

file, err := fsutil.CreateFile("./testdata/test.txt", 0664, 0666)
file, err := fsutil.CreateFile("testdata/test.txt", 0664, 0666)
if assert.NoErr(t, err) {
assert.Eq(t, "./testdata/test.txt", file.Name())
assert.Eq(t, "testdata/test.txt", file.Name())
assert.NoErr(t, file.Close())
assert.NoErr(t, os.Remove(file.Name()))
}

file, err = fsutil.CreateFile("./testdata/sub/test.txt", 0664, 0777)
file, err = fsutil.CreateFile("testdata/sub/test.txt", 0664, 0777)
if assert.NoErr(t, err) {
assert.Eq(t, "./testdata/sub/test.txt", file.Name())
assert.Eq(t, "testdata/sub/test.txt", file.Name())
assert.NoErr(t, file.Close())
assert.NoErr(t, os.RemoveAll("./testdata/sub"))
assert.NoErr(t, os.RemoveAll("testdata/sub"))
}

file, err = fsutil.CreateFile("./testdata/sub/sub2/test.txt", 0664, 0777)
file, err = fsutil.CreateFile("testdata/sub/sub2/test.txt", 0664, 0777)
if assert.NoErr(t, err) {
assert.Eq(t, "./testdata/sub/sub2/test.txt", file.Name())
assert.Eq(t, "testdata/sub/sub2/test.txt", file.Name())
assert.NoErr(t, file.Close())
assert.NoErr(t, os.RemoveAll("./testdata/sub"))
assert.NoErr(t, os.RemoveAll("testdata/sub"))
}

fpath := "./testdata/sub/sub3/test-must-create.txt"
assert.NoErr(t, fsutil.RmFileIfExist(fpath))
fpath := "testdata/sub/sub3/test-must-create.txt"
file = fsutil.MustCreateFile(fpath, 0, 0766)
assert.NoErr(t, file.Close())
assert.NoErr(t, fsutil.RmFileIfExist(fpath))

err = fsutil.RemoveSub("./testdata/sub")
err = fsutil.RemoveSub("testdata/sub")
assert.NoErr(t, err)
}

func TestQuickOpenFile(t *testing.T) {
fpath := "./testdata/quick-open-file.txt"
fpath := "testdata/quick-open-file.txt"
assert.NoErr(t, fsutil.RmFileIfExist(fpath))

file, err := fsutil.QuickOpenFile(fpath)
Expand Down
6 changes: 4 additions & 2 deletions fsutil/opread.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func ReadExistFile(filePath string) []byte {
return nil
}

// TextScanner from filepath or io.Reader, will panic on in type error
// TextScanner from filepath or io.Reader, will panic on in type error.
// Will scan parse text to tokens: Ident, Int, Float, Char, String, RawString, Comment, etc.
//
// Usage:
//
Expand All @@ -123,7 +124,8 @@ func TextScanner(in any) *scanner.Scanner {
return &s
}

// LineScanner create from filepath or io.Reader
// LineScanner create from filepath or io.Reader, will panic on in type error.
// Will scan and parse text to lines.
//
// s := fsutil.LineScanner("/path/to/file")
// for s.Scan() {
Expand Down
33 changes: 32 additions & 1 deletion fsutil/opread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fsutil_test
import (
"strings"
"testing"
"text/scanner"

"github.com/gookit/goutil/fsutil"
"github.com/gookit/goutil/testutil/assert"
Expand All @@ -29,10 +30,16 @@ func TestReadReader(t *testing.T) {
fr := fakeobj.NewReader()
assert.Empty(t, fsutil.ReadReader(fr))

fr.ErrOnRead = true
assert.Panics(t, func() {
fr.ErrOnRead = true
fsutil.ReadReader(fr)
})

_, err := fsutil.ReadStringOrErr(fr)
assert.Err(t, err)

_, err = fsutil.ReadStringOrErr([]string{"invalid-type"})
assert.Err(t, err)
}

func TestGetContents(t *testing.T) {
Expand All @@ -52,3 +59,27 @@ func TestGetContents(t *testing.T) {
fsutil.ReadFile("/path-not-exist")
})
}

func TestTextScanner(t *testing.T) {
r := strings.NewReader("hello\ngolang")

ts := fsutil.TextScanner(r)
assert.Neq(t, scanner.EOF, ts.Scan())
assert.Eq(t, "hello", ts.TokenText())

assert.Panics(t, func() {
fsutil.TextScanner([]string{"invalid-type"})
})
}

func TestLineScanner(t *testing.T) {
r := strings.NewReader("hello\ngolang")

ls := fsutil.LineScanner(r)
assert.True(t, ls.Scan())
assert.Eq(t, "hello", ls.Text())

assert.Panics(t, func() {
fsutil.LineScanner([]string{"invalid-type"})
})
}
17 changes: 11 additions & 6 deletions internal/checkfn/check.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package checkfn

import (
"fmt"
"reflect"
"strings"

Expand All @@ -25,27 +26,31 @@ func IsEmpty(v any) bool {

// Contains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
// data allow types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// Returns:
// - valid: data is valid
// - found: element was found
//
// return (false, false) if impossible.
// return (true, false) if element was not found.
// return (true, true) if element was found.
func Contains(data, elem any) (valid, found bool) {
dataRv := reflect.ValueOf(data)
dataRt := reflect.TypeOf(data)
if dataRt == nil {
if data == nil {
return false, false
}

dataRv := reflect.ValueOf(data)
dataRt := reflect.TypeOf(data)
dataKind := dataRt.Kind()

// string
if dataKind == reflect.String {
return true, strings.Contains(dataRv.String(), reflect.ValueOf(elem).String())
return true, strings.Contains(dataRv.String(), fmt.Sprint(elem))
}

// map
Expand Down
3 changes: 2 additions & 1 deletion reflects/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,6 @@ func IsEmptyReal(v reflect.Value) bool {
case reflect.Invalid:
return true
}
return false

return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}
21 changes: 12 additions & 9 deletions reflects/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func TestIsEqual(t *testing.T) {
is.False(reflects.IsEqual([]string{}, data))
}

// ST for testing
type ST struct {
v any
}

func TestIsEmpty(t *testing.T) {
is := assert.New(t)

Expand All @@ -58,11 +63,10 @@ func TestIsEmpty(t *testing.T) {
is.True(reflects.IsEmpty(reflect.ValueOf(float32(0))))
is.True(reflects.IsEmpty(reflect.ValueOf(comdef.StringMatchFunc(nil))))

type T struct {
v any //lint:ignore U1000 for test
}
rv := reflect.ValueOf(T{}).Field(0)
rv := reflect.ValueOf(ST{}).Field(0)
is.True(reflects.IsEmpty(rv))

is.True(reflects.IsEmpty(reflect.ValueOf(ST{})))
}

func TestIsEmptyValue(t *testing.T) {
Expand All @@ -78,13 +82,12 @@ func TestIsEmptyValue(t *testing.T) {
is.True(reflects.IsEmptyValue(reflect.ValueOf(float32(0))))
is.True(reflects.IsEmptyReal(reflect.ValueOf(comdef.StringMatchFunc(nil))))

type T struct {
v any //lint:ignore U1000 for test
}
rv := reflect.ValueOf(T{}).Field(0)
rv := reflect.ValueOf(ST{}).Field(0)
is.True(reflects.IsEmptyValue(rv))

rv = reflect.ValueOf(&T{v: "abc"})
is.True(reflects.IsEmptyReal(reflect.ValueOf(ST{})))

rv = reflect.ValueOf(&ST{v: "abc"})
is.False(reflects.IsEmptyValue(rv))
}

Expand Down
4 changes: 3 additions & 1 deletion reflects/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func TypeElem(t reflect.Type) reflect.Type {
}
}

// Len get reflect value length
// Len get reflect value length. allow: intX, uintX, floatX, string, map, array, chan, slice.
//
// Note: (u)intX use width. float to string then calc len.
func Len(v reflect.Value) int {
v = reflect.Indirect(v)

Expand Down
2 changes: 1 addition & 1 deletion strutil/textscan/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (t *StringToken) HasMore() bool {
}

// ScanMore implements
func (t *StringToken) ScanMore(ts *TextScanner) error {
func (t *StringToken) ScanMore(_ *TextScanner) error {
return nil
}

Expand Down

0 comments on commit 5b354e6

Please sign in to comment.