github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ttlcache/options_test.go (about)

     1  package ttlcache
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_optionFunc_apply(t *testing.T) {
    11  	var called bool
    12  
    13  	optionFunc[string, string](func(_ *options[string, string]) {
    14  		called = true
    15  	}).apply(nil)
    16  	assert.True(t, called)
    17  }
    18  
    19  func Test_applyOptions(t *testing.T) {
    20  	var opts options[string, string]
    21  
    22  	applyOptions(&opts,
    23  		WithCapacity[string, string](12),
    24  		WithTTL[string, string](time.Hour),
    25  	)
    26  
    27  	assert.Equal(t, uint64(12), opts.capacity)
    28  	assert.Equal(t, time.Hour, opts.ttl)
    29  }
    30  
    31  func Test_WithCapacity(t *testing.T) {
    32  	var opts options[string, string]
    33  
    34  	WithCapacity[string, string](12).apply(&opts)
    35  	assert.Equal(t, uint64(12), opts.capacity)
    36  }
    37  
    38  func Test_WithTTL(t *testing.T) {
    39  	var opts options[string, string]
    40  
    41  	WithTTL[string, string](time.Hour).apply(&opts)
    42  	assert.Equal(t, time.Hour, opts.ttl)
    43  }
    44  
    45  func Test_WithLoader(t *testing.T) {
    46  	var opts options[string, string]
    47  
    48  	l := LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] {
    49  		return nil
    50  	})
    51  	WithLoader[string, string](l).apply(&opts)
    52  	assert.NotNil(t, opts.loader)
    53  }
    54  
    55  func Test_WithDisableTouchOnHit(t *testing.T) {
    56  	var opts options[string, string]
    57  
    58  	WithDisableTouchOnHit[string, string]().apply(&opts)
    59  	assert.True(t, opts.disableTouchOnHit)
    60  }