Skip to content

Commit

Permalink
✅ test: structs,cli - add more unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 29, 2023
1 parent ba4db05 commit ff6a0db
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 63 deletions.
1 change: 1 addition & 0 deletions basefn/extfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestHowLongAgo(t *testing.T) {
}{
{-36, "unknown"},
{36, "36 secs"},
{60, "1 min"},
{346, "5 mins"},
{3467, "57 mins"},
{346778, "4 days"},
Expand Down
2 changes: 2 additions & 0 deletions cliutil/cliutil_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cliutil_test

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -89,6 +90,7 @@ func TestWorkdir(t *testing.T) {
assert.NotEmpty(t, cliutil.BinDir())
assert.NotEmpty(t, cliutil.BinFile())
assert.NotEmpty(t, cliutil.BinName())
fmt.Println(cliutil.GetTermSize())
}

func TestColorPrint(t *testing.T) {
Expand Down
42 changes: 19 additions & 23 deletions cliutil/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,17 @@ func ReadFirstRune(question string) (rune, error) {
// ok := ReadAsBool("are you OK? [y/N]", false)
func ReadAsBool(tip string, defVal bool) bool {
fChar, err := ReadFirstByte(tip)
if err != nil {
panic(err)
}

if fChar != 0 {
if err == nil && fChar != 0 {
return ByteIsYes(fChar)
}
return defVal
}

// ReadPassword from console terminal
func ReadPassword(question ...string) string {
if len(question) > 0 {
print(question[0])
} else {
print("Enter Password: ")
}

bs, err := term.ReadPassword(syscallStdinFd())
if err != nil {
return ""
}

println() // new line
return string(bs)
}

// Confirm with user input
func Confirm(tip string, defVal ...bool) bool {
var defV bool
mark := " [y/N]: "

var defV bool
if len(defVal) > 0 && defVal[0] {
defV = true
mark = " [Y/n]: "
Expand All @@ -139,3 +118,20 @@ func InputIsYes(ans string) bool {
func ByteIsYes(ans byte) bool {
return ans == 'y' || ans == 'Y'
}

// ReadPassword from console terminal
func ReadPassword(question ...string) string {
if len(question) > 0 {
print(question[0])
} else {
print("Enter Password: ")
}

bs, err := term.ReadPassword(syscallStdinFd())
if err != nil {
return ""
}

println() // new line
return string(bs)
}
3 changes: 3 additions & 0 deletions cliutil/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func TestRead_cases(t *testing.T) {
ans := cliutil.Confirm("continue?", false)
fmt.Println(ans)
assert.True(t, ans)
ans = cliutil.Confirm("continue?", true)
fmt.Println(ans)
assert.True(t, ans)
})
}

Expand Down
50 changes: 41 additions & 9 deletions comdef/comdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ type StringWriteStringer interface {
fmt.Stringer
}

type (
// MarshalFunc define
MarshalFunc func(v any) ([]byte, error)

// UnmarshalFunc define
UnmarshalFunc func(bts []byte, ptr any) error
)

// Int64able interface
type Int64able interface {
Int64() (int64, error)
}

//
//
// Matcher type
//
//

// Matcher interface
type Matcher[T any] interface {
Match(s T) bool
}

// MatchFunc definition. implements Matcher interface
type MatchFunc[T any] func(v T) bool

// Match satisfies the Matcher interface
func (fn MatchFunc[T]) Match(v T) bool {
return fn(v)
}

// StringMatcher interface
type StringMatcher interface {
Match(s string) bool
Expand All @@ -33,15 +65,15 @@ func (fn StringMatchFunc) Match(s string) bool {
return fn(s)
}

type (
// MarshalFunc define
MarshalFunc func(v any) ([]byte, error)
// StringHandler interface
type StringHandler interface {
Handle(s string) string
}

// UnmarshalFunc define
UnmarshalFunc func(bts []byte, ptr any) error
)
// StringHandleFunc definition
type StringHandleFunc func(s string) string

// Int64able interface
type Int64able interface {
Int64() (int64, error)
// Handle satisfies the StringHandler interface
func (fn StringHandleFunc) Handle(s string) string {
return fn(s)
}
17 changes: 13 additions & 4 deletions comdef/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ type Uint interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

// Xint interface type. all int or uint types
type Xint interface {
// Xint interface type. alias of Integer
type Xint = Integer

// Integer interface type. all int or uint types
type Integer interface {
Int | Uint
}

Expand All @@ -30,16 +33,22 @@ type XintOrFloat interface {
Int | Uint | Float
}

// SortedType interface type.
// that supports the operators < <= >= >.
// SortedType interface type. same of constraints.Ordered
//
// it can be ordered, that supports the operators < <= >= >.
//
// contains: (x)int, float, ~string types
type SortedType interface {
Int | Uint | Float | ~string
}

// Compared type. alias of constraints.ScalarType
type Compared = ScalarType

// ScalarType interface type.
//
// it can be ordered, that supports the operators < <= >= >.
//
// contains: (x)int, float, ~string, ~bool types
type ScalarType interface {
Int | Uint | Float | ~string | ~bool
Expand Down
26 changes: 26 additions & 0 deletions errorx/assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package errorx_test

import (
"testing"

"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/testutil/assert"
)

func TestAssert_methods(t *testing.T) {
// IsFalse
assert.NoErr(t, errorx.IsFalse(false))
assert.Err(t, errorx.IsFalse(true))

// IsTrue
assert.NoErr(t, errorx.IsTrue(true))
assert.Err(t, errorx.IsTrue(false))

// IsIn
assert.NoErr(t, errorx.IsIn(1, []int{1, 2, 3}))
assert.Err(t, errorx.IsIn(4, []int{1, 2, 3}))

// NotIn
assert.NoErr(t, errorx.NotIn(4, []int{1, 2, 3}))
assert.Err(t, errorx.NotIn(1, []int{1, 2, 3}))
}
21 changes: 15 additions & 6 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gookit/goutil"
"github.com/gookit/goutil/netutil/httpreq"
"github.com/gookit/goutil/structs"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
Expand All @@ -15,23 +16,31 @@ func TestNewErrGroup(t *testing.T) {

eg := goutil.NewErrGroup()
eg.Add(func() error {
resp, err := httpreq.Get(testSrvAddr+"/get", nil)
resp, err := httpreq.Get(testSrvAddr + "/get")
if err != nil {
return err
}

fmt.Println(testutil.ParseBodyToReply(resp.Body))
return nil
}, func() error {
resp, err := httpreq.Post(testSrvAddr+"/post", "hi")
if err != nil {
return err
}

resp := httpreq.MustResp(httpreq.Post(testSrvAddr+"/post", "hi"))
fmt.Println(testutil.ParseBodyToReply(resp.Body))
return nil
})

err := eg.Wait()
assert.NoErr(t, err)
}

func TestQuickRun_methods(t *testing.T) {
qr := goutil.NewQuickRun()
qr.Add(func(ctx *structs.Data) error {
resp := httpreq.MustResp(httpreq.Get(testSrvAddr + "/get"))
rr := testutil.ParseBodyToReply(resp.Body)
assert.Eq(t, "GET", rr.Method)
return nil
})

assert.NoErr(t, qr.Run())
}
29 changes: 16 additions & 13 deletions structs/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import (
// LiteData simple map[string]any struct. no lock
type LiteData = Data

// NewLiteData create, not lock
// NewLiteData create, not locked
func NewLiteData(data map[string]any) *Data {
if data == nil {
data = make(map[string]any)
}

return &LiteData{
data: data,
}
return &LiteData{data: data}
}

/*************************************************************
Expand Down Expand Up @@ -82,7 +79,7 @@ func (d *Data) ResetData() {

// Merge load new data
func (d *Data) Merge(mp map[string]any) {
d.data = maputil.SimpleMerge(d.data, mp)
d.data = maputil.SimpleMerge(mp, d.data)
}

// Set value to data
Expand Down Expand Up @@ -151,21 +148,27 @@ func (d *Data) String() string {
return maputil.ToString(d.data)
}

// OrderedMap data TODO
type OrderedMap struct {
// OrderedData data TODO
type OrderedData struct {
maputil.Data
len int
cap int
keys []string
// vals []any
}

// NewOrderedMap instance.
func NewOrderedMap(len int) *OrderedMap {
return &OrderedMap{len: len}
// NewOrderedData instance.
func NewOrderedData(cap int) *OrderedData {
return &OrderedData{cap: cap, Data: make(maputil.Data, cap)}
}

// Load data
func (om *OrderedData) Load(data map[string]any) {
om.Data.Load(data)
om.keys = om.Data.Keys()
}

// Set key and value to map
func (om *OrderedMap) Set(key string, val any) {
func (om *OrderedData) Set(key string, val any) {
om.keys = append(om.keys, key)
om.Data.Set(key, val)
}
18 changes: 18 additions & 0 deletions structs/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func TestLiteData_Data(t *testing.T) {
assert.True(t, ok)
assert.Eq(t, 234, v)

d.Merge(map[string]any{
"key1": "def",
"key4": "value4",
})
assert.Eq(t, "def", d.StrVal("key1"))
assert.Eq(t, "value4", d.GetVal("key4"))

d.ResetData()
assert.Empty(t, d.Data())
}
Expand Down Expand Up @@ -69,3 +76,14 @@ func TestDataStore_EnableLock(t *testing.T) {
assert.Eq(t, "def", md.Get("key1"))
assert.NotEmpty(t, md.String())
}

func TestNewOrderedData(t *testing.T) {
od := structs.NewOrderedData(10)
od.Set("key0", 234)
od.Load(map[string]any{
"key1": "abc",
"key2": true,
})

assert.NotEmpty(t, od.Keys())
}
1 change: 0 additions & 1 deletion structs/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func ParseTagValueDefault(field, tagVal string) (mp maputil.SMap, err error) {
// ParseTagValueQuick quick parse tag value string by sep(;)
func ParseTagValueQuick(tagVal string, defines []string) maputil.SMap {
parseFn := ParseTagValueDefine(";", defines)

mp, _ := parseFn("", tagVal)
return mp
}
Expand Down
15 changes: 15 additions & 0 deletions structs/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,18 @@ func TestParseTagValueNamed(t *testing.T) {
_, err = structs.ParseTagValueNamed("name", "name=n;default=inhere", "name")
assert.ErrSubMsg(t, err, "parse tag error on field 'name': invalid")
}

func TestParseTagValueQuick(t *testing.T) {
fields := []string{"name", "default"}
mp := structs.ParseTagValueQuick("", fields)
assert.Empty(t, mp)

mp = structs.ParseTagValueQuick("inhere", fields)
assert.NotEmpty(t, mp)
assert.Eq(t, "inhere", mp.Str("name"))

mp = structs.ParseTagValueQuick(";tom", fields)
assert.NotEmpty(t, mp)
assert.Eq(t, "", mp.Str("name"))
assert.Eq(t, "tom", mp.Str("default"))
}
Loading

0 comments on commit ff6a0db

Please sign in to comment.