github.com/andoma-go/puddle/v2@v2.2.1/resource_list_test.go (about)

     1  package puddle
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestResList_Append(t *testing.T) {
    11  	r := require.New(t)
    12  
    13  	arr := []*Resource[any]{
    14  		new(Resource[any]),
    15  		new(Resource[any]),
    16  		new(Resource[any]),
    17  	}
    18  
    19  	list := resList[any](arr)
    20  
    21  	list.append(new(Resource[any]))
    22  	r.Len(list, 4)
    23  	list.append(new(Resource[any]))
    24  	r.Len(list, 5)
    25  	list.append(new(Resource[any]))
    26  	r.Len(list, 6)
    27  }
    28  
    29  func TestResList_PopBack(t *testing.T) {
    30  	r := require.New(t)
    31  
    32  	arr := []*Resource[any]{
    33  		new(Resource[any]),
    34  		new(Resource[any]),
    35  		new(Resource[any]),
    36  	}
    37  
    38  	list := resList[any](arr)
    39  
    40  	list.popBack()
    41  	r.Len(list, 2)
    42  	list.popBack()
    43  	r.Len(list, 1)
    44  	list.popBack()
    45  	r.Len(list, 0)
    46  
    47  	r.Panics(func() { list.popBack() })
    48  }
    49  
    50  func TestResList_PanicsWithBugReportIfResourceDoesNotExist(t *testing.T) {
    51  	arr := []*Resource[any]{
    52  		new(Resource[any]),
    53  		new(Resource[any]),
    54  		new(Resource[any]),
    55  	}
    56  
    57  	list := resList[any](arr)
    58  
    59  	assert.PanicsWithValue(t, "BUG: removeResource could not find res in slice", func() {
    60  		list.remove(new(Resource[any]))
    61  	})
    62  }