github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/map_/test/api_test.go (about)

     1  package test
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/m4gshm/gollections/convert/ptr"
    10  	"github.com/m4gshm/gollections/map_"
    11  	"github.com/m4gshm/gollections/map_/clone"
    12  	"github.com/m4gshm/gollections/map_/filter"
    13  	"github.com/m4gshm/gollections/map_/group"
    14  	"github.com/m4gshm/gollections/map_/resolv"
    15  	"github.com/m4gshm/gollections/op"
    16  	"github.com/m4gshm/gollections/slice"
    17  	"github.com/m4gshm/gollections/slice/clone/sort"
    18  )
    19  
    20  type entity struct{ val string }
    21  
    22  var (
    23  	first  = entity{"1_first"}
    24  	second = entity{"2_second"}
    25  	third  = entity{"3_third"}
    26  
    27  	entities = map[int]*entity{1: &first, 2: &second, 3: &third}
    28  )
    29  
    30  func Test_Clone(t *testing.T) {
    31  	c := clone.Of(entities)
    32  
    33  	assert.Equal(t, entities, c)
    34  	assert.NotSame(t, entities, c)
    35  
    36  	for k := range entities {
    37  		assert.Same(t, entities[k], c[k])
    38  	}
    39  }
    40  
    41  func Test_DeepClone(t *testing.T) {
    42  	c := clone.Deep(entities, func(e *entity) *entity { return ptr.Of(*e) })
    43  
    44  	assert.Equal(t, entities, c)
    45  	assert.NotSame(t, entities, c)
    46  
    47  	for i := range entities {
    48  		assert.Equal(t, entities[i], c[i])
    49  		assert.NotSame(t, entities[i], c[i])
    50  	}
    51  }
    52  
    53  func Test_Keys(t *testing.T) {
    54  	keys := map_.Keys(entities)
    55  	assert.Equal(t, slice.Of(1, 2, 3), sort.Asc(keys))
    56  }
    57  
    58  func Test_Values(t *testing.T) {
    59  	values := map_.Values(entities)
    60  	assert.Equal(t, slice.Of(&first, &second, &third), sort.By(values, func(e *entity) string { return e.val }))
    61  }
    62  
    63  func Test_ConvertValues(t *testing.T) {
    64  	var strValues map[int]string = map_.ConvertValues(entities, func(e *entity) string { return e.val })
    65  
    66  	assert.Equal(t, "1_first", strValues[1])
    67  	assert.Equal(t, "2_second", strValues[2])
    68  	assert.Equal(t, "3_third", strValues[3])
    69  }
    70  
    71  func Test_ValuesConverted(t *testing.T) {
    72  	var values []string = map_.ValuesConverted(entities, func(e *entity) string { return e.val })
    73  	assert.Equal(t, slice.Of("1_first", "2_second", "3_third"), sort.Asc(values))
    74  }
    75  
    76  type rows[T any] struct {
    77  	in     []T
    78  	cursor int
    79  }
    80  
    81  func (r *rows[T]) hasNext() bool    { return r.cursor < len(r.in) }
    82  func (r *rows[T]) next() (T, error) { e := r.in[r.cursor]; r.cursor++; return e, nil }
    83  
    84  func Test_OfLoop(t *testing.T) {
    85  	stream := &rows[int]{slice.Of(1, 2, 3), 0}
    86  	result, _ := map_.OfLoop(stream, (*rows[int]).hasNext, func(r *rows[int]) (bool, int, error) {
    87  		n, err := r.next()
    88  		return n%2 == 0, n, err
    89  	})
    90  
    91  	assert.Equal(t, 2, result[true])
    92  	assert.Equal(t, 1, result[false])
    93  }
    94  
    95  func Test_OfLoopResolv(t *testing.T) {
    96  	stream := &rows[int]{slice.Of(1, 2, 3, 4), 0}
    97  	result, _ := map_.OfLoopResolv(stream, (*rows[int]).hasNext, func(r *rows[int]) (bool, int, error) {
    98  		n, err := r.next()
    99  		return n%2 == 0, n, err
   100  	}, resolv.Last[bool, int])
   101  
   102  	assert.Equal(t, 4, result[true])
   103  	assert.Equal(t, 3, result[false])
   104  }
   105  
   106  func Test_Generate(t *testing.T) {
   107  	counter := 0
   108  	result, _ := map_.Generate(func() (bool, int, bool, error) { counter++; return counter%2 == 0, counter, counter < 4, nil })
   109  
   110  	assert.Equal(t, 2, result[true])
   111  	assert.Equal(t, 1, result[false])
   112  }
   113  
   114  func Test_GenerateResolv(t *testing.T) {
   115  	counter := 0
   116  	result, _ := map_.GenerateResolv(func() (bool, int, bool, error) {
   117  		counter++
   118  		return counter%2 == 0, counter, counter < 5, nil
   119  	}, func(exists bool, k bool, old, new int) int {
   120  		return op.IfElse(exists, op.IfElse(k, new, old), new)
   121  	})
   122  
   123  	assert.Equal(t, 4, result[true])
   124  	assert.Equal(t, 1, result[false])
   125  }
   126  
   127  func Test_GroupOfLoop(t *testing.T) {
   128  	stream := &rows[int]{slice.Of(1, 2, 3), 0}
   129  	result, _ := group.OfLoop(stream, (*rows[int]).hasNext, func(r *rows[int]) (bool, int, error) {
   130  		n, err := r.next()
   131  		return n%2 == 0, n, err
   132  	})
   133  
   134  	assert.Equal(t, slice.Of(2), result[true])
   135  	assert.Equal(t, slice.Of(1, 3), result[false])
   136  }
   137  
   138  func Test_StringRepresentation(t *testing.T) {
   139  	order := slice.Of(4, 3, 2, 1)
   140  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   141  	actual := map_.ToStringOrdered(order, elements)
   142  
   143  	expected := "[4:4 3:3 2:2 1:1]"
   144  	assert.Equal(t, expected, actual)
   145  }
   146  
   147  func Test_Reduce(t *testing.T) {
   148  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   149  	k, _ := map_.Reduce(elements, func(k, k2 int, _, _ string) (int, string) {
   150  		return k + k2, ""
   151  	})
   152  
   153  	assert.Equal(t, 1+2+3+4, k)
   154  }
   155  
   156  func Test_MatchAny(t *testing.T) {
   157  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   158  	ok := map_.HasAny(elements, func(k int, v string) bool {
   159  		return k == 2 || v == "4"
   160  	})
   161  
   162  	assert.True(t, ok)
   163  
   164  	noOk := map_.HasAny(elements, func(k int, _ string) bool {
   165  		return k > 5
   166  	})
   167  
   168  	assert.False(t, noOk)
   169  }
   170  
   171  func Test_ToSlice(t *testing.T) {
   172  	result := map_.ToSlice(entities, func(key int, val *entity) string { return strconv.Itoa(key) + ":" + val.val })
   173  	assert.Equal(t, slice.Of("1:1_first", "2:2_second", "3:3_third"), sort.Asc(result))
   174  }
   175  
   176  func Test_ToSliceErrorable(t *testing.T) {
   177  	result, _ := map_.ToSlicee(entities, func(key int, val *entity) (int, error) {
   178  		v, err := strconv.Atoi(string(val.val[0]))
   179  		return v + key, err
   180  	})
   181  	assert.Equal(t, slice.Of(2, 4, 6), sort.Asc(result))
   182  }
   183  
   184  func Test_Filter(t *testing.T) {
   185  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   186  
   187  	result := map_.Filter(elements, func(key int, val string) bool { return key <= 2 || val == "4" })
   188  	check := map_.KeyChecker(result)
   189  	assert.Equal(t, 3, len(result))
   190  	assert.True(t, check(1))
   191  	assert.True(t, check(2))
   192  	assert.False(t, check(3))
   193  	assert.True(t, check(4))
   194  }
   195  
   196  func Test_FilterKeys(t *testing.T) {
   197  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   198  
   199  	result := filter.Keys(elements, func(key int) bool { return key <= 2 })
   200  	check := map_.KeyChecker(result)
   201  	assert.Equal(t, 2, len(result))
   202  	assert.True(t, check(1))
   203  	assert.True(t, check(2))
   204  	assert.False(t, check(3))
   205  	assert.False(t, check(4))
   206  }
   207  
   208  func Test_FilterValues(t *testing.T) {
   209  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   210  
   211  	result := filter.Values(elements, func(val string) bool { return val <= "2" })
   212  	check := map_.KeyChecker(result)
   213  	assert.Equal(t, 2, len(result))
   214  	assert.True(t, check(1))
   215  	assert.True(t, check(2))
   216  	assert.False(t, check(3))
   217  	assert.False(t, check(4))
   218  }
   219  
   220  func Test_Getter(t *testing.T) {
   221  	elements := map[int]string{4: "4", 2: "2", 1: "1", 3: "3"}
   222  
   223  	getter := map_.Getter(elements)
   224  	assert.Equal(t, "1", getter(1))
   225  	assert.Equal(t, "2", getter(2))
   226  	assert.Equal(t, "3", getter(3))
   227  	assert.Equal(t, "4", getter(4))
   228  
   229  	nilGetter := map_.Getter[map[int]string](nil)
   230  
   231  	assert.Equal(t, "", nilGetter(0))
   232  
   233  	getterOk := map_.GetterOk(elements)
   234  
   235  	_, ok := getterOk(1)
   236  	assert.True(t, ok)
   237  	_, ok = getterOk(0)
   238  	assert.False(t, ok)
   239  }