2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2017-12-17 13:20:03 +08:00
|
|
|
|
2020-04-16 07:00:26 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
2020-04-16 07:00:26 +08:00
|
|
|
)
|
2017-12-22 04:49:14 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestNsCmd(t *testing.T) {
|
2018-05-22 08:08:11 +08:00
|
|
|
Test(t,
|
2020-08-17 04:55:57 +08:00
|
|
|
That("put (ns [&name=value])[name]").Puts("value"),
|
2022-01-03 08:10:40 +08:00
|
|
|
That("var n: = (ns [&name=value]); put $n:name").Puts("value"),
|
2020-09-05 03:31:47 +08:00
|
|
|
That("ns [&[]=[]]").Throws(errs.BadValue{
|
2020-08-17 04:55:57 +08:00
|
|
|
What: `key of argument of "ns"`,
|
|
|
|
Valid: "string", Actual: "list"}),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2020-08-17 04:55:57 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestMakeMap(t *testing.T) {
|
|
|
|
Test(t,
|
2020-05-06 06:23:23 +08:00
|
|
|
That("make-map []").Puts(vals.EmptyMap),
|
|
|
|
That("make-map [[k v]]").Puts(vals.MakeMap("k", "v")),
|
|
|
|
That("make-map [[k v] [k v2]]").Puts(vals.MakeMap("k", "v2")),
|
|
|
|
That("make-map [[k1 v1] [k2 v2]]").
|
|
|
|
Puts(vals.MakeMap("k1", "v1", "k2", "v2")),
|
|
|
|
That("make-map [kv]").Puts(vals.MakeMap("k", "v")),
|
2021-09-11 21:51:26 +08:00
|
|
|
That("make-map [{ } [k v]]").
|
2020-05-06 06:23:23 +08:00
|
|
|
Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "input to make-map", Valid: "iterable", Actual: "fn"},
|
2021-09-11 21:51:26 +08:00
|
|
|
"make-map [{ } [k v]]"),
|
|
|
|
That("make-map [[k v] [k]]").
|
2020-05-06 06:23:23 +08:00
|
|
|
Throws(
|
|
|
|
errs.BadValue{
|
|
|
|
What: "input to make-map", Valid: "iterable with 2 elements",
|
|
|
|
Actual: "list with 1 elements"},
|
2021-09-11 21:51:26 +08:00
|
|
|
"make-map [[k v] [k]]"),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2020-05-06 06:23:23 +08:00
|
|
|
|
2023-01-05 02:56:00 +08:00
|
|
|
func TestConj(t *testing.T) {
|
|
|
|
Test(t,
|
|
|
|
That("conj [] a").Puts(vals.MakeList("a")),
|
|
|
|
That("conj [a b]").Puts(vals.MakeList("a", "b")),
|
|
|
|
That("conj [a b] c").Puts(vals.MakeList("a", "b", "c")),
|
|
|
|
That("conj [a b] c d").Puts(vals.MakeList("a", "b", "c", "d")),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestAssoc(t *testing.T) {
|
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
That(`put (assoc [0] 0 zero)[0]`).Puts("zero"),
|
|
|
|
That(`put (assoc [&] k v)[k]`).Puts("v"),
|
|
|
|
That(`put (assoc [&k=v] k v2)[k]`).Puts("v2"),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDissoc(t *testing.T) {
|
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
That(`has-key (dissoc [&k=v] k) k`).Puts(false),
|
2021-09-11 21:51:26 +08:00
|
|
|
That("dissoc foo 0").Throws(ErrorWithMessage("cannot dissoc")),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2018-03-03 14:08:09 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestHasKey(t *testing.T) {
|
|
|
|
Test(t,
|
2023-02-27 04:49:31 +08:00
|
|
|
That(`has-key [&k=v] k`).Puts(true),
|
|
|
|
That(`has-key [&k=v] bad`).Puts(false),
|
|
|
|
That(`has-key [lorem ipsum] 0`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum] 0..`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum] 0..=`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum] ..2`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum] ..=2`).Puts(false),
|
|
|
|
That(`has-key [lorem ipsum] 2`).Puts(false),
|
|
|
|
That(`has-key [lorem ipsum dolor sit] 0..4`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum dolor sit] 0..=4`).Puts(false),
|
|
|
|
That(`has-key [lorem ipsum dolor sit] 1..3`).Puts(true),
|
|
|
|
That(`has-key [lorem ipsum dolor sit] 1..5`).Puts(false),
|
|
|
|
That(`has-key [lorem ipsum dolor sit] -2..=-1`).Puts(true),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasValue(t *testing.T) {
|
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
That(`has-value [&lorem=ipsum &foo=bar] lorem`).Puts(false),
|
|
|
|
That(`has-value [&lorem=ipsum &foo=bar] bar`).Puts(true),
|
|
|
|
That(`has-value [foo bar] bar`).Puts(true),
|
|
|
|
That(`has-value [foo bar] badehose`).Puts(false),
|
2023-05-20 00:25:36 +08:00
|
|
|
That(`has-value [[foo] [bar]] [foo]`).Puts(true),
|
2018-03-03 14:08:09 +08:00
|
|
|
That(`has-value "foo" o`).Puts(true),
|
|
|
|
That(`has-value "foo" d`).Puts(false),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|
2018-03-03 14:08:09 +08:00
|
|
|
|
2021-01-10 22:48:21 +08:00
|
|
|
func TestKeys(t *testing.T) {
|
|
|
|
Test(t,
|
2018-03-03 14:08:09 +08:00
|
|
|
That(`keys [&]`).DoesNothing(),
|
|
|
|
That(`keys [&a=foo]`).Puts("a"),
|
2017-12-17 13:20:03 +08:00
|
|
|
// Windows does not have an external sort command. Disabled until we have a
|
|
|
|
// builtin sort command.
|
2020-05-26 00:45:08 +08:00
|
|
|
That(`keys [&a=foo &b=bar] | order`).Puts("a", "b"),
|
2021-09-11 21:51:26 +08:00
|
|
|
That("keys (num 1)").Throws(ErrorWithMessage("cannot iterate keys of number")),
|
2021-06-25 06:24:43 +08:00
|
|
|
thatOutputErrorIsBubbled("keys [&a=foo]"),
|
2021-01-10 22:48:21 +08:00
|
|
|
)
|
|
|
|
}
|