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

View file

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

View file

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

View file

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