1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package goja
import "testing"
func TestGoMapReflectGetSet(t *testing.T) {
const SCRIPT = `
m.c = m.a + m.b;
`
vm := New()
m := map[string]string{
"a": "4",
"b": "2",
}
vm.Set("m", m)
_, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if c := m["c"]; c != "42" {
t.Fatalf("Unexpected value: '%s'", c)
}
}
func TestGoMapReflectIntKey(t *testing.T) {
const SCRIPT = `
m[2] = m[0] + m[1];
`
vm := New()
m := map[int]int{
0: 40,
1: 2,
}
vm.Set("m", m)
_, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if c := m[2]; c != 42 {
t.Fatalf("Unexpected value: '%d'", c)
}
}
func TestGoMapReflectDelete(t *testing.T) {
const SCRIPT = `
delete m.a;
`
vm := New()
m := map[string]string{
"a": "4",
"b": "2",
}
vm.Set("m", m)
_, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if _, exists := m["a"]; exists {
t.Fatal("a still exists")
}
if b := m["b"]; b != "2" {
t.Fatalf("Unexpected b: '%s'", b)
}
}
func TestGoMapReflectJSON(t *testing.T) {
const SCRIPT = `
function f(m) {
return JSON.stringify(m);
}
`
vm := New()
m := map[string]string{
"t": "42",
}
_, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
f := vm.Get("f")
if call, ok := AssertFunction(f); ok {
v, err := call(nil, ([]Value{vm.ToValue(m)})...)
if err != nil {
t.Fatal(err)
}
if !v.StrictEquals(asciiString(`{"t":"42"}`)) {
t.Fatalf("Unexpected value: %v", v)
}
} else {
t.Fatalf("Not a function: %v", f)
}
}
func TestGoMapReflectProto(t *testing.T) {
const SCRIPT = `
m.hasOwnProperty("t");
`
vm := New()
m := map[string]string{
"t": "42",
}
vm.Set("m", m)
v, err := vm.RunString(SCRIPT)
if err != nil {
t.Fatal(err)
}
if !v.StrictEquals(valueTrue) {
t.Fatalf("Expected true, got %v", v)
}
}
|