xgo/runtime/vm_objects_limit_test.go
Daniel 61890b15cb
module refactor (#148)
* 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
2019-03-18 08:15:26 -07:00

49 lines
1.1 KiB
Go

package runtime_test
import (
"testing"
"github.com/d5/tengo/objects"
)
func TestObjectsLimit(t *testing.T) {
testAllocsLimit(t, `5`, 0)
testAllocsLimit(t, `5 + 5`, 1)
testAllocsLimit(t, `a := [1, 2, 3]`, 1)
testAllocsLimit(t, `a := 1; b := 2; c := 3; d := [a, b, c]`, 1)
testAllocsLimit(t, `a := {foo: 1, bar: 2}`, 1)
testAllocsLimit(t, `a := 1; b := 2; c := {foo: a, bar: b}`, 1)
testAllocsLimit(t, `
f := func() {
return 5 + 5
}
a := f() + 5
`, 2)
testAllocsLimit(t, `
f := func() {
return 5 + 5
}
a := f()
`, 1)
testAllocsLimit(t, `
a := []
f := func() {
a = append(a, 5)
}
f()
f()
f()
`, 4)
}
func testAllocsLimit(t *testing.T, src string, limit int64) {
expect(t, src, Opts().Skip2ndPass(), objects.UndefinedValue) // no limit
expect(t, src, Opts().MaxAllocs(limit).Skip2ndPass(), objects.UndefinedValue)
expect(t, src, Opts().MaxAllocs(limit+1).Skip2ndPass(), objects.UndefinedValue)
if limit > 1 {
expectError(t, src, Opts().MaxAllocs(limit-1).Skip2ndPass(), "allocation limit exceeded")
}
if limit > 2 {
expectError(t, src, Opts().MaxAllocs(limit-2).Skip2ndPass(), "allocation limit exceeded")
}
}