elvish/pkg/eval/vals/has_key_test.go
dunsany 9a26d6c645
Various has-key fixes (#1647)
* Correct slice indexing convention in code comment

A colon is not supported.

* Add more key slices tests

* Unify `has-key` tests with those from `pkg/eval/vals/has_key_test.go`

* Fix key slice format in documentation

Fixes #1646.

* Fix missing bracket

* Fix indexing

* Add more key slices `has-key` tests

* Fix `has-key` test expected value
2023-02-26 20:49:31 +00:00

41 lines
1.3 KiB
Go

package vals
import (
"testing"
"src.elv.sh/pkg/tt"
)
type hasKeyer struct{ key any }
func (h hasKeyer) HasKey(k any) bool { return k == h.key }
func TestHasKey(t *testing.T) {
tt.Test(t, tt.Fn("HasKey", HasKey), tt.Table{
// Map
Args(MakeMap("k", "v"), "k").Rets(true),
Args(MakeMap("k", "v"), "bad").Rets(false),
// HasKeyer
Args(hasKeyer{"valid"}, "valid").Rets(true),
Args(hasKeyer{"valid"}, "invalid").Rets(false),
// Fallback to IterateKeys
Args(keysIterator{vs("lorem")}, "lorem").Rets(true),
Args(keysIterator{vs("lorem")}, "ipsum").Rets(false),
// Fallback to Len
Args(MakeList("lorem", "ipsum"), "0").Rets(true),
Args(MakeList("lorem", "ipsum"), "0..").Rets(true),
Args(MakeList("lorem", "ipsum"), "0..=").Rets(true),
Args(MakeList("lorem", "ipsum"), "..2").Rets(true),
Args(MakeList("lorem", "ipsum"), "..=2").Rets(false),
Args(MakeList("lorem", "ipsum"), "2").Rets(false),
Args(MakeList("lorem", "ipsum", "dolor", "sit"), "0..4").Rets(true),
Args(MakeList("lorem", "ipsum", "dolor", "sit"), "0..=4").Rets(false),
Args(MakeList("lorem", "ipsum", "dolor", "sit"), "1..3").Rets(true),
Args(MakeList("lorem", "ipsum", "dolor", "sit"), "1..5").Rets(false),
Args(MakeList("lorem", "ipsum", "dolor", "sit"), "-2..=-1").Rets(true),
// Non-container
Args(1, "0").Rets(false),
})
}