...

Source file src/github.com/ory/x/pagination/limit_test.go

Documentation: github.com/ory/x/pagination

     1  /*
     2   * Copyright © 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  package pagination
    21  
    22  import (
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestIndex(t *testing.T) {
    30  	for k, c := range []struct {
    31  		s      []string
    32  		offset int
    33  		limit  int
    34  		e      []string
    35  	}{
    36  		{
    37  			s:      []string{"a", "b", "c"},
    38  			offset: 0,
    39  			limit:  100,
    40  			e:      []string{"a", "b", "c"},
    41  		},
    42  		{
    43  			s:      []string{"a", "b", "c"},
    44  			offset: 0,
    45  			limit:  2,
    46  			e:      []string{"a", "b"},
    47  		},
    48  		{
    49  			s:      []string{"a", "b", "c"},
    50  			offset: 1,
    51  			limit:  10,
    52  			e:      []string{"b", "c"},
    53  		},
    54  		{
    55  			s:      []string{"a", "b", "c"},
    56  			offset: 1,
    57  			limit:  2,
    58  			e:      []string{"b", "c"},
    59  		},
    60  		{
    61  			s:      []string{"a", "b", "c"},
    62  			offset: 2,
    63  			limit:  2,
    64  			e:      []string{"c"},
    65  		},
    66  		{
    67  			s:      []string{"a", "b", "c"},
    68  			offset: 3,
    69  			limit:  10,
    70  			e:      []string{},
    71  		},
    72  		{
    73  			s:      []string{"a", "b", "c"},
    74  			offset: 2,
    75  			limit:  10,
    76  			e:      []string{"c"},
    77  		},
    78  		{
    79  			s:      []string{"a", "b", "c"},
    80  			offset: 1,
    81  			limit:  10,
    82  			e:      []string{"b", "c"},
    83  		},
    84  	} {
    85  		t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
    86  			start, end := Index(c.limit, c.offset, len(c.s))
    87  			assert.EqualValues(t, c.e, c.s[start:end])
    88  		})
    89  	}
    90  }
    91  

View as plain text