2023-06-18 04:28:11 +03:00
|
|
|
package loz_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-07-03 18:34:19 +03:00
|
|
|
|
2023-06-18 04:28:11 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/loz"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
|
|
|
"github.com/neilotoole/sq/testh/tutil"
|
2023-06-18 04:28:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAll(t *testing.T) {
|
|
|
|
gotAny := loz.All[any]()
|
|
|
|
require.Equal(t, []any{}, gotAny)
|
|
|
|
|
|
|
|
gotStrings := loz.All("hello", "world")
|
|
|
|
require.Equal(t, []string{"hello", "world"}, gotStrings)
|
2023-11-19 03:05:48 +03:00
|
|
|
|
|
|
|
wantInts := []int{1, 2, 3}
|
|
|
|
gotInts := loz.All(wantInts...)
|
|
|
|
require.Equal(t, wantInts, gotInts)
|
|
|
|
require.False(t, &gotInts == &wantInts,
|
|
|
|
"wantInts and gotInts should not be the same slice")
|
2023-06-18 04:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToSliceType(t *testing.T) {
|
|
|
|
input1 := []any{"hello", "world"}
|
|
|
|
|
|
|
|
var got []string
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
got, ok = loz.ToSliceType[any, string](input1...)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Len(t, got, 2)
|
|
|
|
require.Equal(t, []string{"hello", "world"}, got)
|
|
|
|
}
|
2023-07-03 18:34:19 +03:00
|
|
|
|
|
|
|
func TestApply(t *testing.T) {
|
|
|
|
input := []string{"hello", "world"}
|
|
|
|
want := []string{"'hello'", "'world'"}
|
|
|
|
got := loz.Apply(input, stringz.SingleQuote)
|
|
|
|
require.Equal(t, want, got)
|
|
|
|
}
|
2023-07-08 18:21:27 +03:00
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
func TestAlignSliceLengths(t *testing.T) {
|
|
|
|
gotA, gotB := loz.AlignSliceLengths(
|
2023-07-08 18:21:27 +03:00
|
|
|
[]int{1, 2, 3},
|
|
|
|
[]int{1, 2, 3, 4},
|
|
|
|
7,
|
|
|
|
)
|
|
|
|
require.Equal(t, []int{1, 2, 3, 7}, gotA)
|
|
|
|
require.Equal(t, []int{1, 2, 3, 4}, gotB)
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
gotA, gotB = loz.AlignSliceLengths(
|
2023-07-08 18:21:27 +03:00
|
|
|
[]int{1, 2, 3, 4},
|
|
|
|
[]int{1, 2, 3},
|
|
|
|
7,
|
|
|
|
)
|
|
|
|
require.Equal(t, []int{1, 2, 3, 4}, gotA)
|
|
|
|
require.Equal(t, []int{1, 2, 3, 7}, gotB)
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
gotA, gotB = loz.AlignSliceLengths(nil, nil, 7)
|
2023-07-08 18:21:27 +03:00
|
|
|
require.Nil(t, gotA)
|
|
|
|
require.Nil(t, gotB)
|
|
|
|
|
2023-08-16 18:09:50 +03:00
|
|
|
gotA, gotB = loz.AlignSliceLengths([]int{}, []int{}, 7)
|
2023-07-08 18:21:27 +03:00
|
|
|
require.True(t, gotA != nil && len(gotA) == 0)
|
|
|
|
require.True(t, gotB != nil && len(gotB) == 0)
|
|
|
|
}
|
2023-08-16 18:09:50 +03:00
|
|
|
|
|
|
|
func TestAlignMatrixWidth(t *testing.T) {
|
|
|
|
const defaultVal int = 7
|
|
|
|
testCases := []struct {
|
|
|
|
in [][]int
|
|
|
|
want [][]int
|
|
|
|
}{
|
|
|
|
{nil, nil},
|
|
|
|
{[][]int{}, [][]int{}},
|
|
|
|
{[][]int{{1, 2, 3}}, [][]int{{1, 2, 3}}},
|
|
|
|
{[][]int{{1, 2, 3}, {1, 2}}, [][]int{{1, 2, 3}, {1, 2, 7}}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
t.Run(tutil.Name(i), func(t *testing.T) {
|
|
|
|
loz.AlignMatrixWidth(tc.in, defaultVal)
|
|
|
|
require.EqualValues(t, tc.want, tc.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSliceZeroed(t *testing.T) {
|
|
|
|
require.True(t, loz.IsSliceZeroed([]any{}))
|
|
|
|
require.True(t, loz.IsSliceZeroed[any](nil))
|
|
|
|
require.True(t, loz.IsSliceZeroed([]int{0, 0}))
|
|
|
|
require.False(t, loz.IsSliceZeroed([]int{0, 1}))
|
|
|
|
require.True(t, loz.IsSliceZeroed([]string{"", ""}))
|
|
|
|
require.False(t, loz.IsSliceZeroed([]string{"", "a"}))
|
|
|
|
}
|