forked from clarkmcc/go-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_test.go
115 lines (101 loc) · 3.54 KB
/
evaluate_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
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
package typescript
import (
"context"
"errors"
"fmt"
"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
)
var (
amdModuleScript = strings.TrimSpace(`
define("myModule", ["exports"], function (exports, core_1) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.multiply = void 0;
var multiply = function (a, b) { return a * b; };
exports.multiply = multiply;
});
`)
)
func TestEvaluateCtx(t *testing.T) {
// This test hits a lot of things:
// #1 - We test that we can load the almond AMD module loader
// #2 - We test that we can load our own 'evaluate before' script that declares an AMD module
// #3 - We test that we can import the AMD module in a type script script and use a function from the module
t.Run("evaluate with custom AMD module", func(t *testing.T) {
script := "import { multiply } from 'myModule'; multiply(5, 5)"
result, err := EvaluateCtx(context.Background(), strings.NewReader(script),
WithAlmondModuleLoader(),
WithTranspile(),
WithEvaluateBefore(strings.NewReader(amdModuleScript)),
WithTranspileOptions(func(config *Config) {
config.Verbose = true
}))
require.NoError(t, err)
require.Equal(t, int64(25), result.ToInteger())
})
// Ensures the context cancellation works correctly with the goja runtime
t.Run("context cancellation", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
result, err := EvaluateCtx(ctx, strings.NewReader("var a = 10;"))
fmt.Println(err)
require.True(t, errors.Is(err, context.Canceled))
require.Nil(t, result)
})
// A syntax error in the evaluate befores should return an error
t.Run("evaluate 'evaluate before' error", func(t *testing.T) {
_, err := Evaluate(strings.NewReader("var a = 10;"),
WithEvaluateBefore(strings.NewReader("let a: number = 10;")))
require.Error(t, err)
require.Contains(t, err.Error(), "evaluating evaluate befores: SyntaxError")
})
t.Run("unreadable 'evaluate before'", func(t *testing.T) {
_, err := Evaluate(strings.NewReader("var a = 10;"),
WithEvaluateBefore(&failingReader{}))
require.Error(t, err)
require.Contains(t, err.Error(), "reading evaluate befores")
})
t.Run("with goja ES6 support", func(t *testing.T) {
// Template literals
result, err := Evaluate(strings.NewReader("var a = 10; `${a}`"))
require.NoError(t, err)
require.Equal(t, "10", result.Export())
// Arrow functions
result, err = Evaluate(strings.NewReader("(() => 10)();"))
require.NoError(t, err)
require.Equal(t, int64(10), result.Export())
})
t.Run("custom runtime", func(t *testing.T) {
runtime := goja.New()
result, err := Evaluate(strings.NewReader("var a = 10; a;"),
WithEvaluationRuntime(runtime))
require.NoError(t, err)
require.Equal(t, int64(10), result.ToInteger())
})
t.Run("script hook", func(t *testing.T) {
script := "var a = 10;"
_, err := Evaluate(strings.NewReader(script),
WithScriptHook(func(s string) (string, error) {
require.Equal(t, script, s)
return script, nil
}))
require.NoError(t, err)
})
t.Run("pre-transpile hook", func(t *testing.T) {
s1 := "let a: number = 10"
_, err := Evaluate(strings.NewReader(s1),
WithTranspile(),
WithScriptPreTranspileHook(func(s2 string) (string, error) {
assert.Equal(t, s1, s2)
return s2, nil
}))
assert.NoError(t, err)
})
}
var _ io.Reader = &failingReader{}
type failingReader struct{}
func (f *failingReader) Read(p []byte) (n int, err error) { return 0, fmt.Errorf("intentional error") }