2019-01-11 13:27:28 +03:00
|
|
|
package runtime_test
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-01-29 00:09:40 +03:00
|
|
|
|
|
|
|
"github.com/d5/tengo/objects"
|
2019-01-09 10:17:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMap(t *testing.T) {
|
|
|
|
expect(t, `
|
|
|
|
out = {
|
|
|
|
one: 10 - 9,
|
|
|
|
two: 1 + 1,
|
|
|
|
three: 6 / 2
|
2019-03-18 18:15:26 +03:00
|
|
|
}`, nil, MAP{
|
2019-01-09 10:17:42 +03:00
|
|
|
"one": 1,
|
|
|
|
"two": 2,
|
|
|
|
"three": 3,
|
|
|
|
})
|
|
|
|
|
2019-02-26 09:53:04 +03:00
|
|
|
expect(t, `
|
|
|
|
out = {
|
|
|
|
"one": 10 - 9,
|
|
|
|
"two": 1 + 1,
|
|
|
|
"three": 6 / 2
|
2019-03-18 18:15:26 +03:00
|
|
|
}`, nil, MAP{
|
2019-02-26 09:53:04 +03:00
|
|
|
"one": 1,
|
|
|
|
"two": 2,
|
|
|
|
"three": 3,
|
|
|
|
})
|
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
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)
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
expect(t, `
|
2019-01-11 09:34:28 +03:00
|
|
|
m := {
|
2019-01-09 10:17:42 +03:00
|
|
|
foo: func(x) {
|
|
|
|
return x * 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = m["foo"](2) + m["foo"](3)
|
2019-03-18 18:15:26 +03:00
|
|
|
`, nil, 10)
|
2019-01-09 10:17:42 +03:00
|
|
|
|
|
|
|
// map assignment is copy-by-reference
|
2019-03-18 18:15:26 +03:00
|
|
|
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)
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|