61890b15cb
* wip * move print and JSON functions to modules * builtin functions are not replacable now * builtin functions are added for default nil symbol table * importables: builtin modules and source modules * refactoring runtime tests * fix tests * update documentation * cleanup * clean up cli * fix REPL prints
55 lines
2.2 KiB
Go
55 lines
2.2 KiB
Go
package runtime_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/d5/tengo/objects"
|
|
)
|
|
|
|
func TestArray(t *testing.T) {
|
|
expect(t, `out = [1, 2 * 2, 3 + 3]`, nil, ARR{1, 4, 6})
|
|
|
|
// array copy-by-reference
|
|
expect(t, `a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2`, nil, ARR{5, 2, 3})
|
|
expect(t, `func () { a1 := [1, 2, 3]; a2 := a1; a1[0] = 5; out = a2 }()`, nil, ARR{5, 2, 3})
|
|
|
|
// array index set
|
|
expectError(t, `a1 := [1, 2, 3]; a1[3] = 5`, nil, "index out of bounds")
|
|
|
|
// index operator
|
|
arr := ARR{1, 2, 3, 4, 5, 6}
|
|
arrStr := `[1, 2, 3, 4, 5, 6]`
|
|
arrLen := 6
|
|
for idx := 0; idx < arrLen; idx++ {
|
|
expect(t, fmt.Sprintf("out = %s[%d]", arrStr, idx), nil, arr[idx])
|
|
expect(t, fmt.Sprintf("out = %s[0 + %d]", arrStr, idx), nil, arr[idx])
|
|
expect(t, fmt.Sprintf("out = %s[1 + %d - 1]", arrStr, idx), nil, arr[idx])
|
|
expect(t, fmt.Sprintf("idx := %d; out = %s[idx]", idx, arrStr), nil, arr[idx])
|
|
}
|
|
|
|
expect(t, fmt.Sprintf("%s[%d]", arrStr, -1), nil, objects.UndefinedValue)
|
|
expect(t, fmt.Sprintf("%s[%d]", arrStr, arrLen), nil, objects.UndefinedValue)
|
|
|
|
// slice operator
|
|
for low := 0; low < arrLen; low++ {
|
|
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, low), nil, ARR{})
|
|
for high := low; high <= arrLen; high++ {
|
|
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, low, high), nil, arr[low:high])
|
|
expect(t, fmt.Sprintf("out = %s[0 + %d : 0 + %d]", arrStr, low, high), nil, arr[low:high])
|
|
expect(t, fmt.Sprintf("out = %s[1 + %d - 1 : 1 + %d - 1]", arrStr, low, high), nil, arr[low:high])
|
|
expect(t, fmt.Sprintf("out = %s[:%d]", arrStr, high), nil, arr[:high])
|
|
expect(t, fmt.Sprintf("out = %s[%d:]", arrStr, low), nil, arr[low:])
|
|
}
|
|
}
|
|
|
|
expect(t, fmt.Sprintf("out = %s[:]", arrStr), nil, arr)
|
|
expect(t, fmt.Sprintf("out = %s[%d:]", arrStr, -1), nil, arr)
|
|
expect(t, fmt.Sprintf("out = %s[:%d]", arrStr, arrLen+1), nil, arr)
|
|
expect(t, fmt.Sprintf("out = %s[%d:%d]", arrStr, 2, 2), nil, ARR{})
|
|
|
|
expectError(t, fmt.Sprintf("%s[:%d]", arrStr, -1), nil, "invalid slice index")
|
|
expectError(t, fmt.Sprintf("%s[%d:]", arrStr, arrLen+1), nil, "invalid slice index")
|
|
expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 0, -1), nil, "invalid slice index")
|
|
expectError(t, fmt.Sprintf("%s[%d:%d]", arrStr, 2, 1), nil, "invalid slice index")
|
|
}
|