github.com/lambdatest/go-gitignore@v0.0.0-20230214141342-7fe15342e580/cache_test.go (about)

     1  package gitignore_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/denormal/go-gitignore"
     7  )
     8  
     9  func TestCache(t *testing.T) {
    10  	// populate the cache with the defined tests
    11  	_cache := gitignore.NewCache()
    12  	for _k, _v := range _CACHETEST {
    13  		_cache.Set(_k, _v)
    14  	}
    15  
    16  	// attempt to retrieve the values from the cache
    17  	//		- if a GitIgnore instance is returned, ensure it is the correct
    18  	//		  instance, and not some other instance
    19  	for _k, _v := range _CACHETEST {
    20  		_found := _cache.Get(_k)
    21  		if _found != _v {
    22  			t.Errorf("cache Get() mismatch; expected %v, got %v",
    23  				_v, _found,
    24  			)
    25  		}
    26  	}
    27  
    28  	// ensure unknown cache keys return nil
    29  	for _, _k := range _CACHEUNKNOWN {
    30  		_found := _cache.Get(_k)
    31  		if _found != nil {
    32  			t.Errorf("cache.Get() unexpected return for key %q; "+
    33  				"expected nil, got %v",
    34  				_k, _found,
    35  			)
    36  		}
    37  	}
    38  
    39  	// ensure we can update the cache
    40  	_ignore := null()
    41  	for _k, _ := range _CACHETEST {
    42  		_cache.Set(_k, _ignore)
    43  	}
    44  	for _k, _ := range _CACHETEST {
    45  		_found := _cache.Get(_k)
    46  		if _found != _ignore {
    47  			t.Errorf("cache Get() mismatch; expected %v, got %v",
    48  				_ignore, _found,
    49  			)
    50  		}
    51  	}
    52  } // TestCache()