xgo/runtime/vm_map_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

51 lines
1.1 KiB
Go

package runtime_test
import (
"testing"
"github.com/d5/tengo/objects"
)
func TestMap(t *testing.T) {
expect(t, `
out = {
one: 10 - 9,
two: 1 + 1,
three: 6 / 2
}`, nil, MAP{
"one": 1,
"two": 2,
"three": 3,
})
expect(t, `
out = {
"one": 10 - 9,
"two": 1 + 1,
"three": 6 / 2
}`, nil, MAP{
"one": 1,
"two": 2,
"three": 3,
})
expect(t, `out = {foo: 5}["foo"]`, nil, 5)
expect(t, `out = {foo: 5}["bar"]`, nil, objects.UndefinedValue)
expect(t, `key := "foo"; out = {foo: 5}[key]`, nil, 5)
expect(t, `out = {}["foo"]`, nil, objects.UndefinedValue)
expect(t, `
m := {
foo: func(x) {
return x * 2
}
}
out = m["foo"](2) + m["foo"](3)
`, nil, 10)
// map assignment is copy-by-reference
expect(t, `m1 := {k1: 1, k2: "foo"}; m2 := m1; m1.k1 = 5; out = m2.k1`, nil, 5)
expect(t, `m1 := {k1: 1, k2: "foo"}; m2 := m1; m2.k1 = 3; out = m1.k1`, nil, 3)
expect(t, `func() { m1 := {k1: 1, k2: "foo"}; m2 := m1; m1.k1 = 5; out = m2.k1 }()`, nil, 5)
expect(t, `func() { m1 := {k1: 1, k2: "foo"}; m2 := m1; m2.k1 = 3; out = m1.k1 }()`, nil, 3)
}