...

Source file src/github.com/jellydator/ttlcache/v3/options_test.go

Documentation: github.com/jellydator/ttlcache/v3

     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_WithVersion(t *testing.T) {
    46  	var opts options[string, string]
    47  
    48  	WithVersion[string, string](true).apply(&opts)
    49  	assert.Equal(t, true, opts.enableVersionTracking)
    50  
    51  	WithVersion[string, string](false).apply(&opts)
    52  	assert.Equal(t, false, opts.enableVersionTracking)
    53  }
    54  
    55  func Test_WithLoader(t *testing.T) {
    56  	var opts options[string, string]
    57  
    58  	l := LoaderFunc[string, string](func(_ *Cache[string, string], _ string) *Item[string, string] {
    59  		return nil
    60  	})
    61  	WithLoader[string, string](l).apply(&opts)
    62  	assert.NotNil(t, opts.loader)
    63  }
    64  
    65  func Test_WithDisableTouchOnHit(t *testing.T) {
    66  	var opts options[string, string]
    67  
    68  	WithDisableTouchOnHit[string, string]().apply(&opts)
    69  	assert.True(t, opts.disableTouchOnHit)
    70  }
    71  

View as plain text