github.com/tanqiangyes/govalidator@v0.0.5/arrays_test.go (about)

     1  package govalidator
     2  
     3  import "testing"
     4  
     5  func TestEach(t *testing.T) {
     6  	// TODO Maybe refactor?
     7  	t.Parallel()
     8  	acc := 0
     9  	data := []interface{}{1, 2, 3, 4, 5}
    10  	var fn Iterator[any] = func(value interface{}, index int) {
    11  		acc = acc + value.(int)
    12  	}
    13  	Each(data, fn)
    14  	if acc != 15 {
    15  		t.Errorf("Expected Each(..) to be %v, got %v", 15, acc)
    16  	}
    17  }
    18  
    19  func TestReduce(t *testing.T) {
    20  	// TODO Maybe refactor?
    21  	t.Parallel()
    22  	data := []int{1, 2, 3, 4, 5}
    23  	var fn ReduceIterator[int] = func(init int, val int) int {
    24  		return init + val
    25  	}
    26  	acc := Reduce(data, fn, 0)
    27  	if acc != 15 {
    28  		t.Errorf("Expected Each(..) to be %v, got %v", 15, acc)
    29  	}
    30  }
    31  
    32  func TestMap(t *testing.T) {
    33  	// TODO Maybe refactor?
    34  	t.Parallel()
    35  	data := []interface{}{1, 2, 3, 4, 5}
    36  	var fn ResultIterator[any] = func(value interface{}, index int) interface{} {
    37  		return value.(int) * 3
    38  	}
    39  	result := Map(data, fn)
    40  	for i, d := range result {
    41  		if d != fn(data[i], i) {
    42  			t.Errorf("Expected Map(..) to be %v, got %v", fn(data[i], i), d)
    43  		}
    44  	}
    45  }
    46  
    47  func TestFind(t *testing.T) {
    48  	// TODO Maybe refactor?
    49  	t.Parallel()
    50  	findElement := 96
    51  	data := []interface{}{1, 2, 3, 4, findElement, 5}
    52  	var fn1 ConditionIterator[any] = func(value interface{}, index int) bool {
    53  		return value.(int) == findElement
    54  	}
    55  	var fn2 ConditionIterator[any] = func(value interface{}, index int) bool {
    56  		value, _ = value.(string)
    57  		return value == "govalidator"
    58  	}
    59  	val1 := Find(data, fn1)
    60  	val2 := Find(data, fn2)
    61  	if val1 != findElement {
    62  		t.Errorf("Expected Find(..) to be %v, got %v", findElement, val1)
    63  	}
    64  	if val2 != nil {
    65  		t.Errorf("Expected Find(..) to be %v, got %v", nil, val2)
    66  	}
    67  }
    68  
    69  func TestFilter(t *testing.T) {
    70  	// TODO Maybe refactor?
    71  	t.Parallel()
    72  	data := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    73  	answer := []interface{}{2, 4, 6, 8, 10}
    74  	var fn ConditionIterator[any] = func(value interface{}, index int) bool {
    75  		return value.(int)%2 == 0
    76  	}
    77  	result := Filter(data, fn)
    78  	for i := range result {
    79  		if result[i] != answer[i] {
    80  			t.Errorf("Expected Filter(..) to be %v, got %v", answer[i], result[i])
    81  		}
    82  	}
    83  }
    84  
    85  func TestCount(t *testing.T) {
    86  	// TODO Maybe refactor?
    87  	t.Parallel()
    88  	data := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    89  	count := 5
    90  	var fn ConditionIterator[any] = func(value interface{}, index int) bool {
    91  		return value.(int)%2 == 0
    92  	}
    93  	result := Count(data, fn)
    94  	if result != count {
    95  		t.Errorf("Expected Count(..) to be %v, got %v", count, result)
    96  	}
    97  }