hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

slice_test.go (2982B)

    1 // Copyright 2019 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package collections
   15 
   16 import (
   17 	"errors"
   18 	"testing"
   19 
   20 	qt "github.com/frankban/quicktest"
   21 )
   22 
   23 var (
   24 	_ Slicer              = (*tstSlicer)(nil)
   25 	_ Slicer              = (*tstSlicerIn1)(nil)
   26 	_ Slicer              = (*tstSlicerIn2)(nil)
   27 	_ testSlicerInterface = (*tstSlicerIn1)(nil)
   28 	_ testSlicerInterface = (*tstSlicerIn1)(nil)
   29 )
   30 
   31 type testSlicerInterface interface {
   32 	Name() string
   33 }
   34 
   35 type testSlicerInterfaces []testSlicerInterface
   36 
   37 type tstSlicerIn1 struct {
   38 	TheName string
   39 }
   40 
   41 type tstSlicerIn2 struct {
   42 	TheName string
   43 }
   44 
   45 type tstSlicer struct {
   46 	TheName string
   47 }
   48 
   49 func (p *tstSlicerIn1) Slice(in any) (any, error) {
   50 	items := in.([]any)
   51 	result := make(testSlicerInterfaces, len(items))
   52 	for i, v := range items {
   53 		switch vv := v.(type) {
   54 		case testSlicerInterface:
   55 			result[i] = vv
   56 		default:
   57 			return nil, errors.New("invalid type")
   58 		}
   59 	}
   60 	return result, nil
   61 }
   62 
   63 func (p *tstSlicerIn2) Slice(in any) (any, error) {
   64 	items := in.([]any)
   65 	result := make(testSlicerInterfaces, len(items))
   66 	for i, v := range items {
   67 		switch vv := v.(type) {
   68 		case testSlicerInterface:
   69 			result[i] = vv
   70 		default:
   71 			return nil, errors.New("invalid type")
   72 		}
   73 	}
   74 	return result, nil
   75 }
   76 
   77 func (p *tstSlicerIn1) Name() string {
   78 	return p.TheName
   79 }
   80 
   81 func (p *tstSlicerIn2) Name() string {
   82 	return p.TheName
   83 }
   84 
   85 func (p *tstSlicer) Slice(in any) (any, error) {
   86 	items := in.([]any)
   87 	result := make(tstSlicers, len(items))
   88 	for i, v := range items {
   89 		switch vv := v.(type) {
   90 		case *tstSlicer:
   91 			result[i] = vv
   92 		default:
   93 			return nil, errors.New("invalid type")
   94 		}
   95 	}
   96 	return result, nil
   97 }
   98 
   99 type tstSlicers []*tstSlicer
  100 
  101 func TestSlice(t *testing.T) {
  102 	t.Parallel()
  103 	c := qt.New(t)
  104 
  105 	for i, test := range []struct {
  106 		args     []any
  107 		expected any
  108 	}{
  109 		{[]any{"a", "b"}, []string{"a", "b"}},
  110 		{[]any{&tstSlicer{"a"}, &tstSlicer{"b"}}, tstSlicers{&tstSlicer{"a"}, &tstSlicer{"b"}}},
  111 		{[]any{&tstSlicer{"a"}, "b"}, []any{&tstSlicer{"a"}, "b"}},
  112 		{[]any{}, []any{}},
  113 		{[]any{nil}, []any{nil}},
  114 		{[]any{5, "b"}, []any{5, "b"}},
  115 		{[]any{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}, testSlicerInterfaces{&tstSlicerIn1{"a"}, &tstSlicerIn2{"b"}}},
  116 		{[]any{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}, []any{&tstSlicerIn1{"a"}, &tstSlicer{"b"}}},
  117 	} {
  118 		errMsg := qt.Commentf("[%d] %v", i, test.args)
  119 
  120 		result := Slice(test.args...)
  121 
  122 		c.Assert(test.expected, qt.DeepEquals, result, errMsg)
  123 	}
  124 }