github.com/elves/elvish@v0.15.0/pkg/eval/vals/has_key_test.go (about)

     1  package vals
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/elves/elvish/pkg/tt"
     7  )
     8  
     9  type hasKeyer struct{ key interface{} }
    10  
    11  func (h hasKeyer) HasKey(k interface{}) bool { return k == h.key }
    12  
    13  func TestHasKey(t *testing.T) {
    14  	Test(t, Fn("HasKey", HasKey), Table{
    15  		// Map
    16  		Args(MakeMap("k", "v"), "k").Rets(true),
    17  		Args(MakeMap("k", "v"), "bad").Rets(false),
    18  		// HasKeyer
    19  		Args(hasKeyer{"valid"}, "valid").Rets(true),
    20  		Args(hasKeyer{"valid"}, "invalid").Rets(false),
    21  		// Fallback to IterateKeys
    22  		Args(keysIterator{vs("lorem")}, "lorem").Rets(true),
    23  		Args(keysIterator{vs("lorem")}, "ipsum").Rets(false),
    24  		// Fallback to Len
    25  		Args(MakeList("lorem", "ipsum"), "0").Rets(true),
    26  		Args(MakeList("lorem", "ipsum"), "0:").Rets(true),
    27  		Args(MakeList("lorem", "ipsum"), "2").Rets(false),
    28  	})
    29  }