Add tons of builtin helpers and array operations.
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-24 12:11:46 -04:00
parent c9d96236dd
commit 99cea83f57
23 changed files with 413 additions and 49 deletions

View File

@@ -354,6 +354,16 @@ func TestArrayDuplication(t *testing.T) {
runVmTests(t, tests)
}
func TestArrayMerging(t *testing.T) {
tests := []vmTestCase{
{"[] + [1]", []int{1}},
{"[1] + [2]", []int{1, 2}},
{"[1, 2] + [3, 4]", []int{1, 2, 3, 4}},
}
runVmTests(t, tests)
}
func TestHashLiterals(t *testing.T) {
tests := []vmTestCase{
{
@@ -378,6 +388,26 @@ func TestHashLiterals(t *testing.T) {
runVmTests(t, tests)
}
func TestHashMerging(t *testing.T) {
tests := []vmTestCase{
{
`{} + {"a": 1}`,
map[object.HashKey]int64{
(&object.String{Value: "a"}).HashKey(): 1,
},
},
{
`{"a": 1} + {"b": 2}`,
map[object.HashKey]int64{
(&object.String{Value: "a"}).HashKey(): 1,
(&object.String{Value: "b"}).HashKey(): 2,
},
},
}
runVmTests(t, tests)
}
func TestSelectorExpressions(t *testing.T) {
tests := []vmTestCase{
{`{"foo": 5}.foo`, 5},
@@ -677,7 +707,7 @@ func TestBuiltinFunctions(t *testing.T) {
{
`len(1)`,
&object.Error{
Message: "argument to `len` not supported, got INTEGER",
Message: "argument to `len` not supported, got int",
},
},
{`len("one", "two")`,
@@ -692,14 +722,14 @@ func TestBuiltinFunctions(t *testing.T) {
{`first([])`, Null},
{`first(1)`,
&object.Error{
Message: "argument to `first` must be ARRAY, got INTEGER",
Message: "argument to `first` must be array, got int",
},
},
{`last([1, 2, 3])`, 3},
{`last([])`, Null},
{`last(1)`,
&object.Error{
Message: "argument to `last` must be ARRAY, got INTEGER",
Message: "argument to `last` must be array, got int",
},
},
{`rest([1, 2, 3])`, []int{2, 3}},
@@ -707,7 +737,7 @@ func TestBuiltinFunctions(t *testing.T) {
{`push([], 1)`, []int{1}},
{`push(1, 1)`,
&object.Error{
Message: "argument to `push` must be ARRAY, got INTEGER",
Message: "argument to `push` must be array, got int",
},
},
{`input()`, ""},