Skip to content

Commit

Permalink
Introduce RunES6String and replace RunString to not support ES6
Browse files Browse the repository at this point in the history
this should again lower the amount of time test take for close to zero
additional scripting.
  • Loading branch information
mstoykov committed May 1, 2020
1 parent d3f563a commit 3d55b1d
Show file tree
Hide file tree
Showing 18 changed files with 399 additions and 399 deletions.
12 changes: 6 additions & 6 deletions js/common/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ func TestBind(t *testing.T) {
{"Methods", bridgeTestMethodsType{}, func(t *testing.T, obj interface{}, rt *goja.Runtime) {
t.Run("unexportedFn", func(t *testing.T) {
_, err := RunString(rt, `obj.unexportedFn()`)
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedFn' at <eval>:1:30(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedFn' at <eval>:1:17(3)")
})
t.Run("ExportedFn", func(t *testing.T) {
_, err := RunString(rt, `obj.exportedFn()`)
assert.NoError(t, err)
})
t.Run("unexportedPtrFn", func(t *testing.T) {
_, err := RunString(rt, `obj.unexportedPtrFn()`)
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedPtrFn' at <eval>:1:33(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedPtrFn' at <eval>:1:20(3)")
})
t.Run("ExportedPtrFn", func(t *testing.T) {
_, err := RunString(rt, `obj.exportedPtrFn()`)
switch obj.(type) {
case *bridgeTestMethodsType:
assert.NoError(t, err)
case bridgeTestMethodsType:
assert.EqualError(t, err, "TypeError: Object has no member 'exportedPtrFn' at <eval>:1:31(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'exportedPtrFn' at <eval>:1:18(3)")
default:
assert.Fail(t, "INVALID TYPE")
}
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestBind(t *testing.T) {
_, err := RunString(rt, `obj.contextInject()`)
switch impl := obj.(type) {
case bridgeTestContextInjectType:
assert.EqualError(t, err, "TypeError: Object has no member 'contextInject' at <eval>:1:31(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'contextInject' at <eval>:1:18(3)")
case *bridgeTestContextInjectType:
assert.EqualError(t, err, "GoError: contextInject() can only be called from within default()")
assert.Equal(t, nil, impl.ctx)
Expand All @@ -546,7 +546,7 @@ func TestBind(t *testing.T) {
_, err := RunString(rt, `obj.contextInjectPtr()`)
switch impl := obj.(type) {
case bridgeTestContextInjectPtrType:
assert.EqualError(t, err, "TypeError: Object has no member 'contextInjectPtr' at <eval>:1:34(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'contextInjectPtr' at <eval>:1:21(3)")
case *bridgeTestContextInjectPtrType:
assert.NoError(t, err)
assert.Equal(t, ctxPtr, impl.ctxPtr)
Expand All @@ -566,7 +566,7 @@ func TestBind(t *testing.T) {
}
case bridgeTestCounterType:
_, err := RunString(rt, `obj.count()`)
assert.EqualError(t, err, "TypeError: Object has no member 'count' at <eval>:1:23(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'count' at <eval>:1:10(3)")
default:
assert.Fail(t, "UNKNOWN TYPE")
}
Expand Down
9 changes: 7 additions & 2 deletions js/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import (
"github.com/loadimpact/k6/js/compiler"
)

// Runs an ES6 string in the given runtime. Use this rather than writing ES5 in tests.
// RunString Runs an string in the given runtime. Use this if writing ES5 in tests isn't a problem.
func RunString(rt *goja.Runtime, src string) (goja.Value, error) {
return rt.RunString(src)
}

// RunES6String Runs an ES6 string in the given runtime. Use this rather than writing ES5 in tests.
func RunES6String(rt *goja.Runtime, src string) (goja.Value, error) {
var err error
c := compiler.New()
src, _, err = c.Transform(src, "__string__")
if err != nil {
return goja.Undefined(), err
}
return rt.RunString(src)
return RunString(rt, src)
}

// Throws a JS error; avoids re-wrapping GoErrors.
Expand Down
4 changes: 2 additions & 2 deletions js/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (

func TestRunString(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
_, err := RunString(goja.New(), `let a = 1;`)
_, err := RunES6String(goja.New(), `let a = 1;`)
assert.NoError(t, err)
})
t.Run("Invalid", func(t *testing.T) {
_, err := RunString(goja.New(), `let a = #;`)
_, err := RunES6String(goja.New(), `let a = #;`)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SyntaxError: __string__: Unexpected character '#' (1:8)\n> 1 | let a = #;\n")
})
Expand Down
Loading

0 comments on commit 3d55b1d

Please sign in to comment.