2019-12-18 18:38:21 +08:00
|
|
|
package edit
|
2019-11-26 05:54:21 +08:00
|
|
|
|
2021-09-08 19:46:38 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"src.elv.sh/pkg/eval"
|
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2022-11-27 00:57:53 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
2022-12-25 19:51:46 +08:00
|
|
|
"src.elv.sh/pkg/eval/vars"
|
2021-09-08 19:46:38 +08:00
|
|
|
)
|
2019-11-26 05:54:21 +08:00
|
|
|
|
|
|
|
func TestBindingMap(t *testing.T) {
|
|
|
|
// TODO
|
2023-07-23 06:04:18 +08:00
|
|
|
TestWithEvalerSetup(t, func(ev *eval.Evaler) {
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.ExtendBuiltin(eval.BuildNs().AddGoFn("binding-map", makeBindingMap))
|
2021-09-08 19:46:38 +08:00
|
|
|
},
|
|
|
|
// Checking key and value when constructing
|
|
|
|
That("binding-map [&[]={ }]").
|
|
|
|
Throws(ErrorWithMessage("must be key or string")),
|
|
|
|
That("binding-map [&foo={ }]").
|
|
|
|
Throws(ErrorWithMessage("bad key: foo")),
|
|
|
|
That("binding-map [&a=string]").
|
|
|
|
Throws(ErrorWithMessage("value should be function")),
|
|
|
|
|
|
|
|
// repr prints a binding-map like an ordinary map
|
|
|
|
That("repr (binding-map [&])").Prints("[&]\n"),
|
|
|
|
// Keys are always sorted
|
|
|
|
That("repr (binding-map [&a=$nop~ &b=$nop~ &c=$nop~])").
|
|
|
|
Prints("[&a=<builtin nop> &b=<builtin nop> &c=<builtin nop>]\n"),
|
|
|
|
|
|
|
|
// Indexing
|
|
|
|
That("eq $nop~ (binding-map [&a=$nop~])[a]").Puts(true),
|
|
|
|
// Checking key
|
|
|
|
That("put (binding-map [&a=$nop~])[foo]").
|
|
|
|
Throws(ErrorWithMessage("bad key: foo")),
|
|
|
|
|
|
|
|
// Assoc
|
|
|
|
That("count (assoc (binding-map [&a=$nop~]) b $nop~)").Puts(2),
|
|
|
|
// Checking key
|
|
|
|
That("(assoc (binding-map [&a=$nop~]) foo $nop~)").
|
|
|
|
Throws(ErrorWithMessage("bad key: foo")),
|
|
|
|
// Checking value
|
|
|
|
That("(assoc (binding-map [&a=$nop~]) b foo)").
|
|
|
|
Throws(ErrorWithMessage("value should be function")),
|
|
|
|
|
|
|
|
// Dissoc
|
|
|
|
That("count (dissoc (binding-map [&a=$nop~]) a)").Puts(0),
|
|
|
|
// Allows bad key - no op
|
|
|
|
That("count (dissoc (binding-map [&a=$nop~]) foo)").Puts(1),
|
|
|
|
)
|
2019-11-26 05:54:21 +08:00
|
|
|
}
|
2022-11-27 00:57:53 +08:00
|
|
|
|
2022-12-25 19:51:46 +08:00
|
|
|
// The happy path of bindingHelp is tested in modes that use bindingHelp.
|
2022-11-27 00:57:53 +08:00
|
|
|
|
2022-12-25 19:51:46 +08:00
|
|
|
func TestBindingHelp_NoBinding(t *testing.T) {
|
|
|
|
ns := eval.BuildNs().
|
|
|
|
AddGoFn("a", func() {}).
|
|
|
|
AddVar("binding", vars.FromInit(bindingsMap{vals.EmptyMap})).
|
|
|
|
Ns()
|
2022-11-27 00:57:53 +08:00
|
|
|
|
|
|
|
// A bindings map with no relevant binding
|
2022-12-25 21:50:17 +08:00
|
|
|
if got := bindingTips(ns, "binding", bindingTip("do a", "a")); len(got) > 0 {
|
2022-12-25 19:51:46 +08:00
|
|
|
t.Errorf("got %v, want empty text", got)
|
2022-11-27 00:57:53 +08:00
|
|
|
}
|
|
|
|
}
|