package narr_test import ( "git.noahlan.cn/noahlan/ntool/narr" "git.noahlan.cn/noahlan/ntool/ntest/assert" "testing" ) // StringEqualComparer tests func TestStringEqualComparerShouldEquals(t *testing.T) { assert.Eq(t, 0, narr.StringEqualsComparer("a", "a")) } func TestStringEqualComparerShouldNotEquals(t *testing.T) { assert.NotEq(t, 0, narr.StringEqualsComparer("a", "b")) } func TestStringEqualComparerElementNotString(t *testing.T) { assert.Eq(t, -1, narr.StringEqualsComparer(1, "a")) } func TestStringEqualComparerPtr(t *testing.T) { ptrVal := "a" assert.Eq(t, 0, narr.StringEqualsComparer(&ptrVal, "a")) } // ReferenceEqualsComparer tests func TestReferenceEqualsComparerShouldEquals(t *testing.T) { assert.Eq(t, 0, narr.ReferenceEqualsComparer(1, 1)) } func TestReferenceEqualsComparerShouldNotEquals(t *testing.T) { assert.NotEq(t, 0, narr.ReferenceEqualsComparer(1, 2)) } // ElemTypeEqualCompareFunc func TestElemTypeEqualCompareFuncShouldEquals(t *testing.T) { assert.Eq(t, 0, narr.ElemTypeEqualsComparer(1, 2)) } func TestElemTypeEqualCompareFuncShouldNotEquals(t *testing.T) { assert.NotEq(t, 0, narr.ElemTypeEqualsComparer(1, "2")) } func TestDifferencesShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.Differences[string](data, []string{"a", "b"}, narr.StringEqualsComparer) assert.Eq(t, []string{"c"}, result) result = narr.Differences[string]([]string{"a", "b"}, data, narr.StringEqualsComparer) assert.Eq(t, []string{"c"}, result) result = narr.Differences[string]([]string{"a", "b", "d"}, data, narr.StringEqualsComparer) assert.Eq(t, 2, len(result)) } func TestExceptsShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.Excepts(data, []string{"a", "b"}, narr.StringEqualsComparer) assert.Eq(t, []string{"c"}, result.([]string)) } func TestExceptsFirstNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Excepts([1]string{"a"}, []string{"a", "b"}, narr.StringEqualsComparer) } func TestExceptsSecondNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Excepts([]string{"a", "b"}, [1]string{"a"}, narr.StringEqualsComparer) } func TestExceptsFirstEmptyShouldReturnsEmpty(t *testing.T) { data := []string{} result := narr.Excepts(data, []string{"a", "b"}, narr.StringEqualsComparer).([]string) assert.Eq(t, []string{}, result) assert.NotSame(t, &data, &result, "should always returns new slice") } func TestExceptsSecondEmptyShouldReturnsFirst(t *testing.T) { data := []string{"a", "b"} result := narr.Excepts(data, []string{}, narr.StringEqualsComparer).([]string) assert.Eq(t, data, result) assert.NotSame(t, &data, &result, "should always returns new slice") } // Intersects tests func TestIntersectsShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.Intersects(data, []string{"a", "b"}, narr.StringEqualsComparer) assert.Eq(t, []string{"a", "b"}, result.([]string)) } func TestIntersectsFirstNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Intersects([1]string{"a"}, []string{"a", "b"}, narr.StringEqualsComparer) } func TestIntersectsSecondNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Intersects([]string{"a", "b"}, [1]string{"a"}, narr.StringEqualsComparer) } func TestIntersectsFirstEmptyShouldReturnsEmpty(t *testing.T) { data := []string{} second := []string{"a", "b"} result := narr.Intersects(data, second, narr.StringEqualsComparer).([]string) assert.Eq(t, []string{}, result) assert.NotSame(t, &second, &result, "should always returns new slice") } func TestIntersectsSecondEmptyShouldReturnsEmpty(t *testing.T) { data := []string{"a", "b"} second := []string{} result := narr.Intersects(data, second, narr.StringEqualsComparer).([]string) assert.Eq(t, []string{}, result) assert.NotSame(t, &data, &result, "should always returns new slice") } // Union tests func TestUnionShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.Union(data, []string{"a", "b", "d"}, narr.StringEqualsComparer).([]string) assert.Eq(t, []string{"a", "b", "c", "d"}, result) } func TestUnionFirstNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Union([1]string{"a"}, []string{"a", "b"}, narr.StringEqualsComparer) } func TestUnionSecondNotSliceShouldPanic(t *testing.T) { defer func() { if r := recover(); r != nil { return } else { t.Fail() } }() narr.Union([]string{"a", "b"}, [1]string{"a"}, narr.StringEqualsComparer) } func TestUnionFirstEmptyShouldReturnsSecond(t *testing.T) { data := []string{} second := []string{"a", "b"} result := narr.Union(data, second, narr.StringEqualsComparer).([]string) assert.Eq(t, []string{"a", "b"}, result) assert.NotSame(t, &second, &result, "should always returns new slice") } func TestUnionSecondEmptyShouldReturnsFirst(t *testing.T) { data := []string{"a", "b"} second := []string{} result := narr.Union(data, second, narr.StringEqualsComparer).([]string) assert.Eq(t, data, result) assert.NotSame(t, &data, &result, "should always returns new slice") } // Find tests func TestFindShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result, err := narr.Find(data, func(a any) bool { return a == "b" }) assert.Nil(t, err) assert.Eq(t, "b", result) _, err = narr.Find(data, func(a any) bool { return a == "d" }) assert.NotNil(t, err) assert.Eq(t, narr.ErrElementNotFound, err) } func TestFindNotSliceShouldPanic(t *testing.T) { assert.Panics(t, func() { _, _ = narr.Find([1]string{"a"}, func(a any) bool { return a == "b" }) }) } func TestFindEmptyReturnsErrElementNotFound(t *testing.T) { data := []string{} _, err := narr.Find(data, func(a any) bool { return a == "b" }) assert.NotNil(t, err) assert.Eq(t, narr.ErrElementNotFound, err) } // FindOrDefault tests func TestFindOrDefaultShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.FindOrDefault(data, func(a any) bool { return a == "b" }, "d").(string) assert.Eq(t, "b", result) result = narr.FindOrDefault(data, func(a any) bool { return a == "d" }, "d").(string) assert.Eq(t, "d", result) } // TakeWhile tests func TestTakeWhileShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.TakeWhile(data, func(a any) bool { return a == "b" || a == "c" }).([]string) assert.Eq(t, []string{"b", "c"}, result) } func TestTakeWhileNotSliceShouldPanic(t *testing.T) { assert.Panics(t, func() { narr.TakeWhile([1]string{"a"}, func(a any) bool { return a == "b" || a == "c" }) }) } func TestTakeWhileEmptyReturnsEmpty(t *testing.T) { var data []string result := narr.TakeWhile(data, func(a any) bool { return a == "b" || a == "c" }).([]string) assert.Eq(t, []string{}, result) assert.NotSame(t, &data, &result, "should always returns new slice") } // ExceptWhile tests func TestExceptWhileShouldPassed(t *testing.T) { data := []string{ "a", "b", "c", } result := narr.ExceptWhile(data, func(a any) bool { return a == "b" || a == "c" }).([]string) assert.Eq(t, []string{"a"}, result) } func TestExceptWhileNotSliceShouldPanic(t *testing.T) { assert.Panics(t, func() { narr.ExceptWhile([1]string{"a"}, func(a any) bool { return a == "b" || a == "c" }) }) } func TestExceptWhileEmptyReturnsEmpty(t *testing.T) { var data []string result := narr.ExceptWhile(data, func(a any) bool { return a == "b" || a == "c" }).([]string) assert.Eq(t, []string{}, result) assert.NotSame(t, &data, &result, "should always returns new slice") } func TestMap(t *testing.T) { list1 := []map[string]any{ {"name": "tom", "age": 23}, {"name": "john", "age": 34}, } flatArr := narr.Column(list1, func(idx int, obj map[string]any) (val any, find bool) { return obj["age"], true }) assert.NotEmpty(t, flatArr) assert.Contains(t, flatArr, 23) assert.Len(t, flatArr, 2) assert.Eq(t, 34, flatArr[1]) }