Fix some unit tests

This commit is contained in:
Guillaume Ballet 2019-11-18 19:36:21 +01:00
parent 7cecc39fd8
commit c003b9e321
4 changed files with 32 additions and 30 deletions

View file

@ -106,9 +106,9 @@ func randomSource() *rand.Rand {
func (re *JSRE) runEventLoop() {
defer close(re.closed)
vm := goja.New()
re.vm = goja.New()
r := randomSource()
vm.SetRandSource(r.Float64)
re.vm.SetRandSource(r.Float64)
registry := map[*jsTimer]*jsTimer{}
ready := make(chan *jsTimer)
@ -150,22 +150,22 @@ func (re *JSRE) runEventLoop() {
}
return goja.Undefined()
}
vm.Set("_setTimeout", setTimeout)
vm.Set("_setInterval", setInterval)
vm.RunString(`var setTimeout = function(args) {
re.vm.Set("_setTimeout", setTimeout)
re.vm.Set("_setInterval", setInterval)
re.vm.RunString(`var setTimeout = function(args) {
if (arguments.length < 1) {
throw TypeError("Failed to execute 'setTimeout': 1 argument required, but only 0 present.");
}
return _setTimeout.apply(this, arguments);
}`)
vm.RunString(`var setInterval = function(args) {
re.vm.RunString(`var setInterval = function(args) {
if (arguments.length < 1) {
throw TypeError("Failed to execute 'setInterval': 1 argument required, but only 0 present.");
}
return _setInterval.apply(this, arguments);
}`)
vm.Set("clearTimeout", clearTimeout)
vm.Set("clearInterval", clearTimeout)
re.vm.Set("clearTimeout", clearTimeout)
re.vm.Set("clearInterval", clearTimeout)
var waitForCallbacks bool
@ -185,9 +185,9 @@ loop:
arguments = make([]interface{}, 1)
}
arguments[0] = timer.call.Arguments[0]
call, isFunc := goja.AssertFunction(vm.Get(`Function.call.call`))
call, isFunc := goja.AssertFunction(timer.call.Arguments[0])
if !isFunc {
panic(vm.ToValue("js error: Function.call.call is not a function"))
panic(re.vm.ToValue("js error: timer/timeout callback is not a function"))
}
call(goja.Null(), timer.call.Arguments...)
@ -202,7 +202,7 @@ loop:
}
case req := <-re.evalQueue:
// run the code, send the result back
req.fn(vm)
req.fn(re.vm)
close(req.done)
if waitForCallbacks && (len(registry) == 0) {
break loop

View file

@ -20,25 +20,24 @@ import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"time"
"github.com/robertkrimen/otto"
"github.com/dop251/goja"
)
type testNativeObjectBinding struct{}
type testNativeObjectBinding struct {
vm *goja.Runtime
}
type msg struct {
Msg string
}
func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value {
m, err := call.Argument(0).ToString()
if err != nil {
return otto.UndefinedValue()
}
v, _ := call.Otto.ToValue(&msg{m})
return v
func (no *testNativeObjectBinding) TestMethod(call goja.FunctionCall) goja.Value {
m := call.Argument(0).ToString().String()
return no.vm.ToValue(&msg{m})
}
func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
@ -51,7 +50,8 @@ func newWithTestJS(t *testing.T, testjs string) (*JSRE, string) {
t.Fatal("cannot create test.js:", err)
}
}
return New(dir, os.Stdout), dir
jsre := New(dir, os.Stdout)
return jsre, dir
}
func TestExec(t *testing.T) {
@ -66,11 +66,11 @@ func TestExec(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}
@ -90,11 +90,11 @@ func TestNatto(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}
@ -105,7 +105,7 @@ func TestBind(t *testing.T) {
jsre := New("", os.Stdout)
defer jsre.Stop(false)
jsre.Bind("no", &testNativeObjectBinding{})
jsre.Bind("no", &testNativeObjectBinding{vm: jsre.vm})
_, err := jsre.Run(`no.TestMethod("testMsg")`)
if err != nil {
@ -125,11 +125,11 @@ func TestLoadScript(t *testing.T) {
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !val.IsString() {
if val.ExportType().Kind() != reflect.String {
t.Errorf("expected string value, got %v", val)
}
exp := "testMsg"
got, _ := val.ToString()
got := val.ToString().String()
if exp != got {
t.Errorf("expected '%v', got '%v'", exp, got)
}

View file

@ -1,6 +1,8 @@
package goja
import "reflect"
import (
"reflect"
)
type baseFuncObject struct {
baseObject

View file

@ -436,7 +436,7 @@ func (p *valueProperty) get(this Value) Value {
}
return _undefined
}
call, _ := p.getterFunc.self.assertCallable()
call, r := p.getterFunc.self.assertCallable()
return call(FunctionCall{
This: this,
})