github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/edit/binding_map_test.go (about)

     1  package edit
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/markusbkk/elvish/pkg/eval"
     7  	. "github.com/markusbkk/elvish/pkg/eval/evaltest"
     8  )
     9  
    10  func TestBindingMap(t *testing.T) {
    11  	// TODO
    12  	TestWithSetup(t, func(ev *eval.Evaler) {
    13  		ev.ExtendBuiltin(eval.BuildNs().AddGoFn("binding-map", makeBindingMap))
    14  	},
    15  		// Checking key and value when constructing
    16  		That("binding-map [&[]={ }]").
    17  			Throws(ErrorWithMessage("must be key or string")),
    18  		That("binding-map [&foo={ }]").
    19  			Throws(ErrorWithMessage("bad key: foo")),
    20  		That("binding-map [&a=string]").
    21  			Throws(ErrorWithMessage("value should be function")),
    22  
    23  		// repr prints a binding-map like an ordinary map
    24  		That("repr (binding-map [&])").Prints("[&]\n"),
    25  		// Keys are always sorted
    26  		That("repr (binding-map [&a=$nop~ &b=$nop~ &c=$nop~])").
    27  			Prints("[&a=<builtin nop> &b=<builtin nop> &c=<builtin nop>]\n"),
    28  
    29  		// Indexing
    30  		That("eq $nop~ (binding-map [&a=$nop~])[a]").Puts(true),
    31  		// Checking key
    32  		That("put (binding-map [&a=$nop~])[foo]").
    33  			Throws(ErrorWithMessage("bad key: foo")),
    34  
    35  		// Assoc
    36  		That("count (assoc (binding-map [&a=$nop~]) b $nop~)").Puts(2),
    37  		// Checking key
    38  		That("(assoc (binding-map [&a=$nop~]) foo $nop~)").
    39  			Throws(ErrorWithMessage("bad key: foo")),
    40  		// Checking value
    41  		That("(assoc (binding-map [&a=$nop~]) b foo)").
    42  			Throws(ErrorWithMessage("value should be function")),
    43  
    44  		// Dissoc
    45  		That("count (dissoc (binding-map [&a=$nop~]) a)").Puts(0),
    46  		// Allows bad key - no op
    47  		That("count (dissoc (binding-map [&a=$nop~]) foo)").Puts(1),
    48  	)
    49  }