github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/paginate_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package util
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestPaginateSlice(t *testing.T) {
    15  	stringSlice := []string{"a", "b", "c", "d", "e"}
    16  	result, ok := PaginateSlice(stringSlice, 1, 2).([]string)
    17  	assert.True(t, ok)
    18  	assert.EqualValues(t, []string{"a", "b"}, result)
    19  
    20  	result, ok = PaginateSlice(stringSlice, 100, 2).([]string)
    21  	assert.True(t, ok)
    22  	assert.EqualValues(t, []string{}, result)
    23  
    24  	result, ok = PaginateSlice(stringSlice, 3, 2).([]string)
    25  	assert.True(t, ok)
    26  	assert.EqualValues(t, []string{"e"}, result)
    27  
    28  	result, ok = PaginateSlice(stringSlice, 1, 0).([]string)
    29  	assert.True(t, ok)
    30  	assert.EqualValues(t, []string{"a", "b", "c", "d", "e"}, result)
    31  
    32  	result, ok = PaginateSlice(stringSlice, 1, -1).([]string)
    33  	assert.True(t, ok)
    34  	assert.EqualValues(t, []string{"a", "b", "c", "d", "e"}, result)
    35  
    36  	type Test struct {
    37  		Val int
    38  	}
    39  
    40  	testVar := []*Test{{Val: 2}, {Val: 3}, {Val: 4}}
    41  	testVar, ok = PaginateSlice(testVar, 1, 50).([]*Test)
    42  	assert.True(t, ok)
    43  	assert.EqualValues(t, []*Test{{Val: 2}, {Val: 3}, {Val: 4}}, testVar)
    44  
    45  	testVar, ok = PaginateSlice(testVar, 2, 2).([]*Test)
    46  	assert.True(t, ok)
    47  	assert.EqualValues(t, []*Test{{Val: 4}}, testVar)
    48  }