Skip to content

Commit

Permalink
update: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 30, 2019
1 parent fb17e51 commit 8726fd2
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 132 deletions.
11 changes: 0 additions & 11 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,6 @@ func Reverse(ss []string)
func StringsRemove(ss []string, s string) []string
```

### Calc

> package `github.com/gookit/goutil/calc`

```go
func DataSize(size uint64) string
func ElapsedTime(startTime time.Time) string
func HowLongAgo(sec int64) string
func Percent(val, total int) float64
```

### CLI Util

> package `github.com/gookit/goutil/cliutil`
Expand Down
40 changes: 0 additions & 40 deletions calc/number.go

This file was deleted.

4 changes: 2 additions & 2 deletions dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func TestPrint(t *testing.T) {

Fprint(1, buf, 123)
str := buf.String()
is.Contains(str, "\x1b[0;35mPRINT AT github.com/gookit/goutil/dump.TestPrint(LINE ")
is.Contains(str, "\x1b[0m\nint(123)")
is.Contains(str, "PRINT AT github.com/gookit/goutil/dump.TestPrint(LINE ")
is.Contains(str, "int(123)")

// disable position for test
Config.NoPosition = true
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions fmtutil/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fmtutil_test

import (
"testing"

"github.com/gookit/goutil/fmtutil"
"github.com/stretchr/testify/assert"
)

func TestDataSize(t *testing.T) {
tests := []struct {
args uint64
want string
}{
{346, "346B"},
{3467, "3.39K"},
{346778, "338.65K"},
{1200346778, "1.12G"},
}

for _, tt := range tests {
assert.Equal(t, tt.want, fmtutil.DataSize(tt.args))
}
}
26 changes: 26 additions & 0 deletions fmtutil/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fmtutil_test

import (
"testing"

"github.com/gookit/goutil/fmtutil"
"github.com/stretchr/testify/assert"
)

func TestHowLongAgo(t *testing.T) {
tests := []struct {
args int64
want string
}{
{-36, "unknown"},
{36, "36 secs"},
{346, "5 mins"},
{3467, "57 mins"},
{346778, "4 days"},
{1200346778, "13892 days"},
}

for _, tt := range tests {
assert.Equal(t, tt.want, fmtutil.HowLongAgo(tt.args))
}
}
23 changes: 0 additions & 23 deletions format/string.go

This file was deleted.

54 changes: 0 additions & 54 deletions format/time.go

This file was deleted.

2 changes: 1 addition & 1 deletion maputil/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

// MergeStringMap simple merge two string map
// MergeStringMap simple merge two string map. merge src to dst map
func MergeStringMap(src, dst map[string]string, ignoreCase bool) map[string]string {
for k, v := range src {
if ignoreCase {
Expand Down
16 changes: 16 additions & 0 deletions maputil/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package maputil_test

import (
"testing"

"github.com/gookit/goutil/maputil"
"github.com/stretchr/testify/assert"
)

func TestMergeStringMap(t *testing.T) {
ret := maputil.MergeStringMap(map[string]string{"A": "v0"}, map[string]string{"A": "v1"}, false)
assert.Equal(t, map[string]string{"A": "v0"}, ret)

ret = maputil.MergeStringMap(map[string]string{"A": "v0"}, map[string]string{"a": "v1"}, true)
assert.Equal(t, map[string]string{"a": "v0"}, ret)
}
29 changes: 29 additions & 0 deletions mathutil/number_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mathutil_test

import (
"testing"
"time"

"github.com/gookit/goutil/mathutil"
"github.com/stretchr/testify/assert"
)

func TestPercent(t *testing.T) {
assert.Equal(t, float64(34), mathutil.Percent(34, 100))
assert.Equal(t, float64(0), mathutil.Percent(34, 0))
assert.Equal(t, float64(-100), mathutil.Percent(34, -34))
}

func TestElapsedTime(t *testing.T) {
ct := time.Now()
time.Sleep(time.Second * 1)
assert.Contains(t, mathutil.ElapsedTime(ct), "100")
}

func TestDataSize(t *testing.T) {
assert.Equal(t, "3.38K", mathutil.DataSize(3456))
}

func TestHowLongAgo(t *testing.T) {
assert.Equal(t, "57 mins", mathutil.HowLongAgo(3456))
}
2 changes: 1 addition & 1 deletion strutil/find_similar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewComparator(src, dst string) *SimilarComparator {
// rate, ok :c.Similar(0.3)
func (c *SimilarComparator) Similar(minDifferRate float32) (float32, bool) {
dist := c.editDistance([]byte(c.src), []byte(c.dst))
differRate := float32(dist) / float32(max(len(c.src), len(c.dst))+4)
differRate := dist / float32(max(len(c.src), len(c.dst))+4)

return differRate, differRate >= minDifferRate
}
Expand Down
8 changes: 8 additions & 0 deletions strutil/strutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ func TestSimilarity(t *testing.T) {
func TestSplit(t *testing.T) {
ss := strutil.Split("a, , b,c", ",")
assert.Equal(t, `[]string{"a", "b", "c"}`, fmt.Sprintf("%#v", ss))

ss = strutil.Split(" ", ",")
assert.Nil(t, ss)
}

func TestSubstr(t *testing.T) {
assert.Equal(t, "abc", strutil.Substr("abcDef", 0, 3))
assert.Equal(t, "cD", strutil.Substr("abcDef", 2, 2))
}

func TestUpperFirst(t *testing.T) {
Expand Down

0 comments on commit 8726fd2

Please sign in to comment.