mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
refactor: rewrite all unit test functions with assert
This commit is contained in:
@@ -3,6 +3,7 @@ package internal
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -142,7 +143,8 @@ func compare(x, y interface{}) int {
|
|||||||
|
|
||||||
// logFailedInfo make test failed and log error info
|
// logFailedInfo make test failed and log error info
|
||||||
func logFailedInfo(t *testing.T, caseName string, expected, actual interface{}) {
|
func logFailedInfo(t *testing.T, caseName string, expected, actual interface{}) {
|
||||||
errInfo := fmt.Sprintf("Test case %v: expected: %v, actual: %v", caseName, expected, actual)
|
_, file, line, _ := runtime.Caller(2)
|
||||||
|
errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual)
|
||||||
t.Error(errInfo)
|
t.Error(errInfo)
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,34 +8,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestContain(t *testing.T) {
|
func TestContain(t *testing.T) {
|
||||||
t1 := []string{"a", "b", "c", "d"}
|
assert := internal.NewAssert(t, "TestContain")
|
||||||
contain(t, t1, "a", true)
|
|
||||||
contain(t, t1, "e", false)
|
|
||||||
|
|
||||||
var t2 []string
|
assert.Equal(true, Contain([]string{"a", "b", "c"}, "a"))
|
||||||
contain(t, t2, "1", false)
|
assert.Equal(false, Contain([]string{"a", "b", "c"}, "d"))
|
||||||
|
assert.Equal(true, Contain([]string{""}, ""))
|
||||||
|
assert.Equal(false, Contain([]string{}, ""))
|
||||||
|
|
||||||
m := make(map[string]int)
|
var m = map[string]int{"a": 1}
|
||||||
m["a"] = 1
|
assert.Equal(true, Contain(m, "a"))
|
||||||
contain(t, m, "a", true)
|
assert.Equal(false, Contain(m, "b"))
|
||||||
contain(t, m, "b", false)
|
|
||||||
|
|
||||||
s := "hello"
|
assert.Equal(true, Contain("abc", "a"))
|
||||||
contain(t, s, "h", true)
|
assert.Equal(false, Contain("abc", "d"))
|
||||||
contain(t, s, "s", false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func contain(t *testing.T, test interface{}, value interface{}, expected bool) {
|
|
||||||
res := Contain(test, value)
|
|
||||||
if res != expected {
|
|
||||||
internal.LogFailedTestInfo(t, "Contain", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChunk(t *testing.T) {
|
func TestChunk(t *testing.T) {
|
||||||
t1 := []string{"a", "b", "c", "d", "e"}
|
assert := internal.NewAssert(t, "TestChunk")
|
||||||
|
|
||||||
|
arr := []string{"a", "b", "c", "d", "e"}
|
||||||
r1 := [][]interface{}{
|
r1 := [][]interface{}{
|
||||||
{"a"},
|
{"a"},
|
||||||
{"b"},
|
{"b"},
|
||||||
@@ -43,26 +34,26 @@ func TestChunk(t *testing.T) {
|
|||||||
{"d"},
|
{"d"},
|
||||||
{"e"},
|
{"e"},
|
||||||
}
|
}
|
||||||
chunk(t, InterfaceSlice(t1), 1, r1)
|
assert.Equal(r1, Chunk(InterfaceSlice(arr), 1))
|
||||||
|
|
||||||
r2 := [][]interface{}{
|
r2 := [][]interface{}{
|
||||||
{"a", "b"},
|
{"a", "b"},
|
||||||
{"c", "d"},
|
{"c", "d"},
|
||||||
{"e"},
|
{"e"},
|
||||||
}
|
}
|
||||||
chunk(t, InterfaceSlice(t1), 2, r2)
|
assert.Equal(r2, Chunk(InterfaceSlice(arr), 2))
|
||||||
|
|
||||||
r3 := [][]interface{}{
|
r3 := [][]interface{}{
|
||||||
{"a", "b", "c"},
|
{"a", "b", "c"},
|
||||||
{"d", "e"},
|
{"d", "e"},
|
||||||
}
|
}
|
||||||
chunk(t, InterfaceSlice(t1), 3, r3)
|
assert.Equal(r3, Chunk(InterfaceSlice(arr), 3))
|
||||||
|
|
||||||
r4 := [][]interface{}{
|
r4 := [][]interface{}{
|
||||||
{"a", "b", "c", "d"},
|
{"a", "b", "c", "d"},
|
||||||
{"e"},
|
{"e"},
|
||||||
}
|
}
|
||||||
chunk(t, InterfaceSlice(t1), 4, r4)
|
assert.Equal(r4, Chunk(InterfaceSlice(arr), 4))
|
||||||
|
|
||||||
r5 := [][]interface{}{
|
r5 := [][]interface{}{
|
||||||
{"a"},
|
{"a"},
|
||||||
@@ -71,16 +62,7 @@ func TestChunk(t *testing.T) {
|
|||||||
{"d"},
|
{"d"},
|
||||||
{"e"},
|
{"e"},
|
||||||
}
|
}
|
||||||
chunk(t, InterfaceSlice(t1), 5, r5)
|
assert.Equal(r5, Chunk(InterfaceSlice(arr), 5))
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func chunk(t *testing.T, test []interface{}, num int, expected [][]interface{}) {
|
|
||||||
res := Chunk(test, num)
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "Chunk", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertSlice(t *testing.T) {
|
func TestConvertSlice(t *testing.T) {
|
||||||
@@ -101,11 +83,9 @@ func TestEvery(t *testing.T) {
|
|||||||
isEven := func(i, num int) bool {
|
isEven := func(i, num int) bool {
|
||||||
return num%2 == 0
|
return num%2 == 0
|
||||||
}
|
}
|
||||||
res := Every(nums, isEven)
|
|
||||||
if res != false {
|
assert := internal.NewAssert(t, "TestEvery")
|
||||||
internal.LogFailedTestInfo(t, "Every", nums, false, res)
|
assert.Equal(false, Every(nums, isEven))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNone(t *testing.T) {
|
func TestNone(t *testing.T) {
|
||||||
@@ -113,42 +93,34 @@ func TestNone(t *testing.T) {
|
|||||||
check := func(i, num int) bool {
|
check := func(i, num int) bool {
|
||||||
return num%2 == 1
|
return num%2 == 1
|
||||||
}
|
}
|
||||||
res := None(nums, check)
|
|
||||||
if res != false {
|
assert := internal.NewAssert(t, "TestNone")
|
||||||
internal.LogFailedTestInfo(t, "None", nums, false, res)
|
assert.Equal(false, None(nums, check))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSome(t *testing.T) {
|
func TestSome(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 5}
|
nums := []int{1, 2, 3, 5}
|
||||||
isEven := func(i, num int) bool {
|
hasEven := func(i, num int) bool {
|
||||||
return num%2 == 0
|
return num%2 == 0
|
||||||
}
|
}
|
||||||
res := Some(nums, isEven)
|
|
||||||
if res != true {
|
assert := internal.NewAssert(t, "TestSome")
|
||||||
internal.LogFailedTestInfo(t, "Some", nums, true, res)
|
assert.Equal(true, Some(nums, hasEven))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilter(t *testing.T) {
|
func TestFilter(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5}
|
nums := []int{1, 2, 3, 4, 5}
|
||||||
even := func(i, num int) bool {
|
isEven := func(i, num int) bool {
|
||||||
return num%2 == 0
|
return num%2 == 0
|
||||||
}
|
}
|
||||||
e1 := []int{2, 4}
|
|
||||||
r1 := Filter(nums, even)
|
assert := internal.NewAssert(t, "TestFilter")
|
||||||
if !reflect.DeepEqual(r1, e1) {
|
assert.Equal([]int{2, 4}, Filter(nums, isEven))
|
||||||
internal.LogFailedTestInfo(t, "Filter", nums, e1, r1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
type student struct {
|
type student struct {
|
||||||
name string
|
name string
|
||||||
age int
|
age int
|
||||||
}
|
}
|
||||||
|
|
||||||
students := []student{
|
students := []student{
|
||||||
{"a", 10},
|
{"a", 10},
|
||||||
{"b", 11},
|
{"b", 11},
|
||||||
@@ -156,8 +128,7 @@ func TestFilter(t *testing.T) {
|
|||||||
{"d", 13},
|
{"d", 13},
|
||||||
{"e", 14},
|
{"e", 14},
|
||||||
}
|
}
|
||||||
|
studentsOfAageGreat12 := []student{
|
||||||
e2 := []student{
|
|
||||||
{"d", 13},
|
{"d", 13},
|
||||||
{"e", 14},
|
{"e", 14},
|
||||||
}
|
}
|
||||||
@@ -165,12 +136,7 @@ func TestFilter(t *testing.T) {
|
|||||||
return s.age > 12
|
return s.age > 12
|
||||||
}
|
}
|
||||||
|
|
||||||
r2 := Filter(students, filterFunc)
|
assert.Equal(studentsOfAageGreat12, Filter(students, filterFunc))
|
||||||
if !reflect.DeepEqual(r2, e2) {
|
|
||||||
internal.LogFailedTestInfo(t, "Filter", students, e2, r2)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGroupBy(t *testing.T) {
|
func TestGroupBy(t *testing.T) {
|
||||||
@@ -179,22 +145,12 @@ func TestGroupBy(t *testing.T) {
|
|||||||
return (num % 2) == 0
|
return (num % 2) == 0
|
||||||
}
|
}
|
||||||
expectedEven := []int{2, 4, 6}
|
expectedEven := []int{2, 4, 6}
|
||||||
|
expectedOdd := []int{1, 3, 5}
|
||||||
even, odd := GroupBy(nums, evenFunc)
|
even, odd := GroupBy(nums, evenFunc)
|
||||||
|
|
||||||
t.Log("odd", odd)
|
assert := internal.NewAssert(t, "TestGroupBy")
|
||||||
|
assert.Equal(expectedEven, even)
|
||||||
t.Log("even", even)
|
assert.Equal(expectedOdd, odd)
|
||||||
|
|
||||||
if !reflect.DeepEqual(IntSlice(even), expectedEven) {
|
|
||||||
internal.LogFailedTestInfo(t, "GroupBy even", nums, expectedEven, even)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOdd := []int{1, 3, 5}
|
|
||||||
if !reflect.DeepEqual(IntSlice(odd), expectedOdd) {
|
|
||||||
internal.LogFailedTestInfo(t, "GroupBy odd", nums, expectedOdd, odd)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCount(t *testing.T) {
|
func TestCount(t *testing.T) {
|
||||||
@@ -202,12 +158,9 @@ func TestCount(t *testing.T) {
|
|||||||
evenFunc := func(i, num int) bool {
|
evenFunc := func(i, num int) bool {
|
||||||
return (num % 2) == 0
|
return (num % 2) == 0
|
||||||
}
|
}
|
||||||
c := Count(nums, evenFunc)
|
|
||||||
|
|
||||||
if c != 3 {
|
assert := internal.NewAssert(t, "TestCount")
|
||||||
internal.LogFailedTestInfo(t, "Count", nums, 3, c)
|
assert.Equal(3, Count(nums, evenFunc))
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFind(t *testing.T) {
|
func TestFind(t *testing.T) {
|
||||||
@@ -220,10 +173,8 @@ func TestFind(t *testing.T) {
|
|||||||
t.Fatal("found nothing")
|
t.Fatal("found nothing")
|
||||||
}
|
}
|
||||||
|
|
||||||
if res != 2 {
|
assert := internal.NewAssert(t, "TestFind")
|
||||||
internal.LogFailedTestInfo(t, "Find", nums, 2, res)
|
assert.Equal(2, res)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFindFoundNothing(t *testing.T) {
|
func TestFindFoundNothing(t *testing.T) {
|
||||||
@@ -232,20 +183,19 @@ func TestFindFoundNothing(t *testing.T) {
|
|||||||
return num > 1
|
return num > 1
|
||||||
}
|
}
|
||||||
_, ok := Find(nums, findFunc)
|
_, ok := Find(nums, findFunc)
|
||||||
if ok {
|
// if ok {
|
||||||
t.Fatal("found something")
|
// t.Fatal("found something")
|
||||||
}
|
// }
|
||||||
|
assert := internal.NewAssert(t, "TestFindFoundNothing")
|
||||||
|
assert.Equal(false, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFlattenDeep(t *testing.T) {
|
func TestFlattenDeep(t *testing.T) {
|
||||||
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
|
||||||
expected := []string{"a", "b", "c", "d"}
|
expected := []string{"a", "b", "c", "d"}
|
||||||
|
|
||||||
res := FlattenDeep(input)
|
assert := internal.NewAssert(t, "TestFlattenDeep")
|
||||||
if !reflect.DeepEqual(res, expected) {
|
assert.Equal(expected, FlattenDeep(input))
|
||||||
internal.LogFailedTestInfo(t, "FlattenDeep", input, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForEach(t *testing.T) {
|
func TestForEach(t *testing.T) {
|
||||||
@@ -257,24 +207,18 @@ func TestForEach(t *testing.T) {
|
|||||||
numbersAddTwo = append(numbersAddTwo, value+2)
|
numbersAddTwo = append(numbersAddTwo, value+2)
|
||||||
})
|
})
|
||||||
|
|
||||||
if !reflect.DeepEqual(numbersAddTwo, expected) {
|
assert := internal.NewAssert(t, "TestForEach")
|
||||||
internal.LogFailedTestInfo(t, "ForEach", numbers, expected, numbersAddTwo)
|
assert.Equal(expected, numbersAddTwo)
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMap(t *testing.T) {
|
func TestMap(t *testing.T) {
|
||||||
s1 := []int{1, 2, 3, 4}
|
nums := []int{1, 2, 3, 4}
|
||||||
multiplyTwo := func(i, num int) int {
|
multiplyTwo := func(i, num int) int {
|
||||||
return num * 2
|
return num * 2
|
||||||
}
|
}
|
||||||
e1 := []int{2, 4, 6, 8}
|
|
||||||
r1 := Map(s1, multiplyTwo)
|
assert := internal.NewAssert(t, "TestMap")
|
||||||
if !reflect.DeepEqual(r1, e1) {
|
assert.Equal([]int{2, 4, 6, 8}, Map(nums, multiplyTwo))
|
||||||
internal.LogFailedTestInfo(t, "Map", s1, e1, r1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
type student struct {
|
type student struct {
|
||||||
name string
|
name string
|
||||||
@@ -285,8 +229,7 @@ func TestMap(t *testing.T) {
|
|||||||
{"b", 2},
|
{"b", 2},
|
||||||
{"c", 3},
|
{"c", 3},
|
||||||
}
|
}
|
||||||
|
studentsOfAdd10Aage := []student{
|
||||||
e2 := []student{
|
|
||||||
{"a", 11},
|
{"a", 11},
|
||||||
{"b", 12},
|
{"b", 12},
|
||||||
{"c", 13},
|
{"c", 13},
|
||||||
@@ -295,11 +238,8 @@ func TestMap(t *testing.T) {
|
|||||||
s.age += 10
|
s.age += 10
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
r2 := Map(students, mapFunc)
|
|
||||||
if !reflect.DeepEqual(r2, e2) {
|
assert.Equal(studentsOfAdd10Aage, Map(students, mapFunc))
|
||||||
internal.LogFailedTestInfo(t, "Filter", students, e2, r2)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReduce(t *testing.T) {
|
func TestReduce(t *testing.T) {
|
||||||
@@ -307,239 +247,154 @@ func TestReduce(t *testing.T) {
|
|||||||
{},
|
{},
|
||||||
{1},
|
{1},
|
||||||
{1, 2, 3, 4}}
|
{1, 2, 3, 4}}
|
||||||
|
|
||||||
expected := []int{0, 1, 10}
|
expected := []int{0, 1, 10}
|
||||||
|
|
||||||
f := func(i, v1, v2 int) int {
|
f := func(i, v1, v2 int) int {
|
||||||
return v1 + v2
|
return v1 + v2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestReduce")
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
res := Reduce(cases[i], f, 0)
|
actual := Reduce(cases[i], f, 0)
|
||||||
if res != expected[i] {
|
assert.Equal(expected[i], actual)
|
||||||
internal.LogFailedTestInfo(t, "Reduce", cases[i], expected[i], res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntSlice(t *testing.T) {
|
func TestIntSlice(t *testing.T) {
|
||||||
var t1 []interface{}
|
var nums []interface{}
|
||||||
t1 = append(t1, 1, 2, 3, 4, 5)
|
nums = append(nums, 1, 2, 3)
|
||||||
expect := []int{1, 2, 3, 4, 5}
|
|
||||||
intSlice(t, t1, expect)
|
|
||||||
}
|
|
||||||
|
|
||||||
func intSlice(t *testing.T, test interface{}, expected []int) {
|
assert := internal.NewAssert(t, "TestIntSlice")
|
||||||
res := IntSlice(test)
|
assert.Equal([]int{1, 2, 3}, IntSlice(nums))
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "IntSlice", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringSlice(t *testing.T) {
|
func TestStringSlice(t *testing.T) {
|
||||||
var t1 []interface{}
|
var strs []interface{}
|
||||||
t1 = append(t1, "a", "b", "c", "d", "e")
|
strs = append(strs, "a", "b", "c")
|
||||||
expect := []string{"a", "b", "c", "d", "e"}
|
|
||||||
stringSlice(t, t1, expect)
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringSlice(t *testing.T, test interface{}, expected []string) {
|
assert := internal.NewAssert(t, "TestStringSlice")
|
||||||
res := StringSlice(test)
|
assert.Equal([]string{"a", "b", "c"}, StringSlice(strs))
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "StringSlice", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInterfaceSlice(t *testing.T) {
|
func TestInterfaceSlice(t *testing.T) {
|
||||||
t1 := []string{"a", "b", "c", "d", "e"}
|
strs := []string{"a", "b", "c"}
|
||||||
expect := []interface{}{"a", "b", "c", "d", "e"}
|
expect := []interface{}{"a", "b", "c"}
|
||||||
interfaceSlice(t, t1, expect)
|
|
||||||
}
|
|
||||||
|
|
||||||
func interfaceSlice(t *testing.T, test interface{}, expected []interface{}) {
|
assert := internal.NewAssert(t, "TestInterfaceSlice")
|
||||||
res := InterfaceSlice(test)
|
assert.Equal(expect, InterfaceSlice(strs))
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "InterfaceSlice", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteByIndex(t *testing.T) {
|
func TestDeleteByIndex(t *testing.T) {
|
||||||
origin := []string{"a", "b", "c", "d", "e"}
|
assert := internal.NewAssert(t, "TestDeleteByIndex")
|
||||||
|
|
||||||
t1 := []string{"a", "b", "c", "d", "e"}
|
t1 := []string{"a", "b", "c", "d", "e"}
|
||||||
r1 := []string{"b", "c", "d", "e"}
|
r1 := []string{"b", "c", "d", "e"}
|
||||||
deleteByIndex(t, origin, t1, 0, 0, r1)
|
a1, _ := DeleteByIndex(t1, 0)
|
||||||
|
assert.Equal(r1, a1)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c", "d", "e"}
|
t2 := []string{"a", "b", "c", "d", "e"}
|
||||||
r2 := []string{"a", "b", "c", "e"}
|
r2 := []string{"a", "b", "c", "e"}
|
||||||
deleteByIndex(t, origin, t1, 3, 0, r2)
|
a2, _ := DeleteByIndex(t2, 3)
|
||||||
|
assert.Equal(r2, a2)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c", "d", "e"}
|
t3 := []string{"a", "b", "c", "d", "e"}
|
||||||
r3 := []string{"a", "b", "c", "d"}
|
r3 := []string{"c", "d", "e"}
|
||||||
deleteByIndex(t, origin, t1, 4, 0, r3)
|
a3, _ := DeleteByIndex(t3, 0, 2)
|
||||||
|
assert.Equal(r3, a3)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c", "d", "e"}
|
t4 := []string{"a", "b", "c", "d", "e"}
|
||||||
r4 := []string{"c", "d", "e"}
|
r4 := []string{}
|
||||||
deleteByIndex(t, origin, t1, 0, 2, r4)
|
a4, _ := DeleteByIndex(t4, 0, 5)
|
||||||
|
assert.Equal(r4, a4)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c", "d", "e"}
|
t5 := []string{"a", "b", "c", "d", "e"}
|
||||||
r5 := []string{} // var r5 []string{} failed
|
_, err := DeleteByIndex(t5, 1, 1)
|
||||||
deleteByIndex(t, origin, t1, 0, 5, r5)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
// failed
|
_, err = DeleteByIndex(t5, 0, 6)
|
||||||
//t1 = []string{"a", "b", "c", "d","e"}
|
assert.IsNotNil(err)
|
||||||
//r6 := []string{"a", "c", "d","e"}
|
|
||||||
//deleteByIndex(t, origin, t1, 1, 1, r6)
|
|
||||||
|
|
||||||
// failed
|
|
||||||
//t1 = []string{"a", "b", "c", "d","e"}
|
|
||||||
//r7 := []string{}
|
|
||||||
//deleteByIndex(t, origin, t1, 0, 6, r7)
|
|
||||||
}
|
|
||||||
|
|
||||||
func deleteByIndex(t *testing.T, origin, test interface{}, start, end int, expected interface{}) {
|
|
||||||
var res interface{}
|
|
||||||
var err error
|
|
||||||
if end != 0 {
|
|
||||||
res, err = DeleteByIndex(test, start, end)
|
|
||||||
} else {
|
|
||||||
res, err = DeleteByIndex(test, start)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Error("DeleteByIndex Error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "DeleteByIndex", origin, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDrop(t *testing.T) {
|
func TestDrop(t *testing.T) {
|
||||||
drop(t, []int{}, 0, []int{})
|
assert := internal.NewAssert(t, "TestInterfaceSlice")
|
||||||
drop(t, []int{}, 1, []int{})
|
|
||||||
drop(t, []int{}, -1, []int{})
|
|
||||||
|
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, 0, []int{1, 2, 3, 4, 5})
|
assert.Equal([]int{}, Drop([]int{}, 0))
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, 1, []int{2, 3, 4, 5})
|
assert.Equal([]int{}, Drop([]int{}, 1))
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, 5, []int{})
|
assert.Equal([]int{}, Drop([]int{}, -1))
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, 6, []int{})
|
|
||||||
|
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, -1, []int{1, 2, 3, 4})
|
assert.Equal([]int{1, 2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, 0))
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, -5, []int{})
|
assert.Equal([]int{2, 3, 4, 5}, Drop([]int{1, 2, 3, 4, 5}, 1))
|
||||||
drop(t, []int{1, 2, 3, 4, 5}, -6, []int{})
|
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 5))
|
||||||
}
|
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, 6))
|
||||||
|
|
||||||
func drop(t *testing.T, test interface{}, n int, expected interface{}) {
|
assert.Equal([]int{1, 2, 3, 4}, Drop([]int{1, 2, 3, 4, 5}, -1))
|
||||||
res := Drop(test, n)
|
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
|
||||||
if !reflect.DeepEqual(res, expected) {
|
assert.Equal([]int{}, Drop([]int{1, 2, 3, 4, 5}, -6))
|
||||||
internal.LogFailedTestInfo(t, "Drop", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertByIndex(t *testing.T) {
|
func TestInsertByIndex(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestInsertByIndex")
|
||||||
|
|
||||||
t1 := []string{"a", "b", "c"}
|
t1 := []string{"a", "b", "c"}
|
||||||
|
r1, _ := InsertByIndex(t1, 0, "1")
|
||||||
|
assert.Equal([]string{"1", "a", "b", "c"}, r1)
|
||||||
|
|
||||||
r1 := []string{"1", "a", "b", "c"}
|
r2, _ := InsertByIndex(t1, 1, "1")
|
||||||
insertByIndex(t, t1, 0, "1", r1)
|
assert.Equal([]string{"a", "1", "b", "c"}, r2)
|
||||||
|
|
||||||
r2 := []string{"a", "1", "b", "c"}
|
r3, _ := InsertByIndex(t1, 3, "1")
|
||||||
insertByIndex(t, t1, 1, "1", r2)
|
assert.Equal([]string{"a", "b", "c", "1"}, r3)
|
||||||
|
|
||||||
r3 := []string{"a", "b", "c", "1"}
|
r4, _ := InsertByIndex(t1, 0, []string{"1", "2", "3"})
|
||||||
insertByIndex(t, t1, 3, "1", r3)
|
assert.Equal([]string{"1", "2", "3", "a", "b", "c"}, r4)
|
||||||
|
|
||||||
r4 := []string{"1", "2", "3", "a", "b", "c"}
|
r5, _ := InsertByIndex(t1, 3, []string{"1", "2", "3"})
|
||||||
insertByIndex(t, t1, 0, []string{"1", "2", "3"}, r4)
|
assert.Equal([]string{"a", "b", "c", "1", "2", "3"}, r5)
|
||||||
|
|
||||||
r5 := []string{"a", "1", "2", "3", "b", "c"}
|
_, err := InsertByIndex(t1, 4, "1")
|
||||||
insertByIndex(t, t1, 1, []string{"1", "2", "3"}, r5)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
r6 := []string{"a", "b", "1", "2", "3", "c"}
|
_, err = InsertByIndex(t1, 0, 1)
|
||||||
insertByIndex(t, t1, 2, []string{"1", "2", "3"}, r6)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
r7 := []string{"a", "b", "c", "1", "2", "3"}
|
|
||||||
insertByIndex(t, t1, 3, []string{"1", "2", "3"}, r7)
|
|
||||||
}
|
|
||||||
|
|
||||||
func insertByIndex(t *testing.T, test interface{}, index int, value, expected interface{}) {
|
|
||||||
res, err := InsertByIndex(test, index, value)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("InsertByIndex Error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "InsertByIndex", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateByIndex(t *testing.T) {
|
func TestUpdateByIndex(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUpdateByIndex")
|
||||||
|
|
||||||
t1 := []string{"a", "b", "c"}
|
t1 := []string{"a", "b", "c"}
|
||||||
r1 := []string{"1", "b", "c"}
|
r1, _ := UpdateByIndex(t1, 0, "1")
|
||||||
updateByIndex(t, t1, 0, "1", r1)
|
assert.Equal([]string{"1", "b", "c"}, r1)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c"}
|
t2 := []string{"a", "b", "c"}
|
||||||
r2 := []string{"a", "1", "c"}
|
r2, _ := UpdateByIndex(t2, 1, "1")
|
||||||
updateByIndex(t, t1, 1, "1", r2)
|
assert.Equal([]string{"a", "1", "c"}, r2)
|
||||||
|
|
||||||
t1 = []string{"a", "b", "c"}
|
_, err := UpdateByIndex([]string{"a", "b", "c"}, 4, "1")
|
||||||
r3 := []string{"a", "b", "1"}
|
assert.IsNotNil(err)
|
||||||
updateByIndex(t, t1, 2, "1", r3)
|
|
||||||
|
|
||||||
}
|
_, err = UpdateByIndex([]string{"a", "b", "c"}, 0, 1)
|
||||||
|
assert.IsNotNil(err)
|
||||||
func updateByIndex(t *testing.T, test interface{}, index int, value, expected interface{}) {
|
|
||||||
res, err := UpdateByIndex(test, index, value)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("UpdateByIndex Error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "UpdateByIndex", test, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnique(t *testing.T) {
|
func TestUnique(t *testing.T) {
|
||||||
t1 := []int{1, 2, 2, 3}
|
assert := internal.NewAssert(t, "TestUnique")
|
||||||
e1 := []int{1, 2, 3}
|
|
||||||
r1 := Unique(t1)
|
|
||||||
if !reflect.DeepEqual(r1, e1) {
|
|
||||||
internal.LogFailedTestInfo(t, "Unique", t1, e1, r1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
t2 := []string{"a", "a", "b", "c"}
|
assert.Equal([]int{1, 2, 3}, Unique([]int{1, 2, 2, 3}))
|
||||||
e2 := []string{"a", "b", "c"}
|
assert.Equal([]string{"a", "b", "c"}, Unique([]string{"a", "a", "b", "c"}))
|
||||||
r2 := Unique(t2)
|
|
||||||
if !reflect.DeepEqual(r2, e2) {
|
|
||||||
internal.LogFailedTestInfo(t, "Unique", t2, e2, r2)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnion(t *testing.T) {
|
func TestUnion(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUnion")
|
||||||
|
|
||||||
s1 := []int{1, 3, 4, 6}
|
s1 := []int{1, 3, 4, 6}
|
||||||
s2 := []int{1, 2, 5, 6}
|
s2 := []int{1, 2, 5, 6}
|
||||||
s3 := []int{0, 4, 5, 7}
|
s3 := []int{0, 4, 5, 7}
|
||||||
|
|
||||||
expected1 := []int{1, 3, 4, 6, 2, 5, 0, 7}
|
assert.Equal([]int{1, 3, 4, 6, 2, 5, 0, 7}, Union(s1, s2, s3))
|
||||||
res1 := Union(s1, s2, s3)
|
assert.Equal([]int{1, 3, 4, 6, 2, 5}, Union(s1, s2))
|
||||||
if !reflect.DeepEqual(res1, expected1) {
|
assert.Equal([]int{1, 3, 4, 6}, Union(s1))
|
||||||
internal.LogFailedTestInfo(t, "Union", s1, expected1, res1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
expected2 := []int{1, 3, 4, 6}
|
|
||||||
res2 := Union(s1)
|
|
||||||
if !reflect.DeepEqual(res2, expected2) {
|
|
||||||
internal.LogFailedTestInfo(t, "Union", s1, expected2, res2)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntersection(t *testing.T) {
|
func TestIntersection(t *testing.T) {
|
||||||
@@ -560,45 +415,37 @@ func TestIntersection(t *testing.T) {
|
|||||||
Intersection(s1),
|
Intersection(s1),
|
||||||
Intersection(s1, s4),
|
Intersection(s1, s4),
|
||||||
}
|
}
|
||||||
for i := 0; i < len(res); i++ {
|
|
||||||
if !reflect.DeepEqual(res[i], expected[i]) {
|
|
||||||
internal.LogFailedTestInfo(t, "Intersection", "Intersection", expected[i], res[i])
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert := internal.NewAssert(t, "TestIntersection")
|
||||||
|
|
||||||
|
for i := 0; i < len(res); i++ {
|
||||||
|
assert.Equal(res[i], expected[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReverseSlice(t *testing.T) {
|
func TestReverseSlice(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIntersection")
|
||||||
|
|
||||||
s1 := []int{1, 2, 3, 4, 5}
|
s1 := []int{1, 2, 3, 4, 5}
|
||||||
e1 := []int{5, 4, 3, 2, 1}
|
|
||||||
ReverseSlice(s1)
|
ReverseSlice(s1)
|
||||||
if !reflect.DeepEqual(s1, e1) {
|
assert.Equal([]int{5, 4, 3, 2, 1}, s1)
|
||||||
internal.LogFailedTestInfo(t, "ReverseSlice", s1, e1, s1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
s2 := []string{"a", "b", "c", "d", "e"}
|
s2 := []string{"a", "b", "c", "d", "e"}
|
||||||
e2 := []string{"e", "d", "c", "b", "a"}
|
|
||||||
ReverseSlice(s2)
|
ReverseSlice(s2)
|
||||||
if !reflect.DeepEqual(s2, e2) {
|
assert.Equal([]string{"e", "d", "c", "b", "a"}, s2)
|
||||||
internal.LogFailedTestInfo(t, "ReverseSlice", s2, e2, s2)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDifference(t *testing.T) {
|
func TestDifference(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIntersection")
|
||||||
|
|
||||||
s1 := []int{1, 2, 3, 4, 5}
|
s1 := []int{1, 2, 3, 4, 5}
|
||||||
s2 := []int{4, 5, 6}
|
s2 := []int{4, 5, 6}
|
||||||
e1 := []int{1, 2, 3}
|
assert.Equal([]int{1, 2, 3}, Difference(s1, s2))
|
||||||
r1 := Difference(s1, s2)
|
|
||||||
if !reflect.DeepEqual(r1, e1) {
|
|
||||||
internal.LogFailedTestInfo(t, "Difference", s1, e1, r1)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortByField(t *testing.T) {
|
func TestSortByField(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWithout")
|
||||||
|
|
||||||
type student struct {
|
type student struct {
|
||||||
name string
|
name string
|
||||||
age int
|
age int
|
||||||
@@ -609,8 +456,7 @@ func TestSortByField(t *testing.T) {
|
|||||||
{"c", 5},
|
{"c", 5},
|
||||||
{"d", 6},
|
{"d", 6},
|
||||||
}
|
}
|
||||||
|
studentsOfSortByAge := []student{
|
||||||
sortByAge := []student{
|
|
||||||
{"b", 15},
|
{"b", 15},
|
||||||
{"a", 10},
|
{"a", 10},
|
||||||
{"d", 6},
|
{"d", 6},
|
||||||
@@ -618,35 +464,35 @@ func TestSortByField(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err := SortByField(students, "age", "desc")
|
err := SortByField(students, "age", "desc")
|
||||||
if err != nil {
|
assert.IsNil(err)
|
||||||
t.Error("IntSlice Error: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(students, sortByAge) {
|
|
||||||
internal.LogFailedTestInfo(t, "SortByField", students, sortByAge, students)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
assert.Equal(students, studentsOfSortByAge)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithout(t *testing.T) {
|
func TestWithout(t *testing.T) {
|
||||||
s := []int{1, 2, 3, 4, 5}
|
assert := internal.NewAssert(t, "TestWithout")
|
||||||
expected := []int{3, 4, 5}
|
assert.Equal([]int{3, 4, 5}, Without([]int{1, 2, 3, 4, 5}, 1, 2))
|
||||||
res := Without(s, 1, 2)
|
assert.Equal([]int{1, 2, 3, 4, 5}, Without([]int{1, 2, 3, 4, 5}))
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, expected) {
|
|
||||||
internal.LogFailedTestInfo(t, "Without", s, expected, res)
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestShuffle(t *testing.T) {
|
func TestShuffle(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestShuffle")
|
||||||
|
|
||||||
s := []int{1, 2, 3, 4, 5}
|
s := []int{1, 2, 3, 4, 5}
|
||||||
res := Shuffle(s)
|
res := Shuffle(s)
|
||||||
t.Log("Shuffle result: ", res)
|
t.Log("Shuffle result: ", res)
|
||||||
|
|
||||||
if reflect.TypeOf(s) != reflect.TypeOf(res) {
|
assert.Equal(reflect.TypeOf(s), reflect.TypeOf(res))
|
||||||
internal.LogFailedTestInfo(t, "Shuffle", s, res, res)
|
|
||||||
t.FailNow()
|
rv := reflect.ValueOf(res)
|
||||||
}
|
assert.Equal(5, rv.Len())
|
||||||
|
|
||||||
|
assert.Equal(true, rv.Kind() == reflect.Slice)
|
||||||
|
assert.Equal(true, rv.Type().Elem().Kind() == reflect.Int)
|
||||||
|
|
||||||
|
assert.Equal(true, Contain(res, 1))
|
||||||
|
assert.Equal(true, Contain(res, 2))
|
||||||
|
assert.Equal(true, Contain(res, 3))
|
||||||
|
assert.Equal(true, Contain(res, 4))
|
||||||
|
assert.Equal(true, Contain(res, 5))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,5 +165,4 @@ func TestUnwrap(t *testing.T) {
|
|||||||
assert.Equal("", Unwrap("**", "*"))
|
assert.Equal("", Unwrap("**", "*"))
|
||||||
assert.Equal("***", Unwrap("***", "**"))
|
assert.Equal("***", Unwrap("***", "**"))
|
||||||
assert.Equal("**", Unwrap("**", "**"))
|
assert.Equal("**", Unwrap("**", "**"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user