forked from lazytiger/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv8_script_test.go
39 lines (33 loc) · 963 Bytes
/
v8_script_test.go
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
package v8
import "testing"
import "runtime"
func Test_ReturnValue(t *testing.T) {
template := engine.NewObjectTemplate()
template.Bind("Call", func() *Value {
val := engine.NewObject()
obj := val.ToObject()
obj.SetProperty("name", engine.NewString("test object"))
obj.SetProperty("id", engine.NewInteger(1234))
return val
})
engine.NewContext(template).Scope(func(cs ContextScope) {
script := engine.Compile([]byte(`
a = Call();
if (a.name != "test object" || a.id != 1234) {
{};
} else {
a;
}
`), nil)
retVal := cs.Run(script)
if !retVal.IsObject() {
t.Fatalf("expected object")
}
retObj := retVal.ToObject()
if !retObj.HasProperty("name") || retObj.GetProperty("name").ToString() != "test object" ||
!retObj.HasProperty("id") || retObj.GetProperty("id").ToNumber() != 1234 {
t.Fatalf("value should be %q not %q", "{\"name\":\"test object\",\"id\":1234}", string(ToJSON(retVal)))
}
})
runtime.GC()
}