-
Notifications
You must be signed in to change notification settings - Fork 13
/
transpiler_test.go
61 lines (53 loc) · 1.84 KB
/
transpiler_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
package typescript
import (
"context"
"github.com/clarkmcc/go-typescript/versions"
v4_2_3 "github.com/clarkmcc/go-typescript/versions/v4.2.3"
"github.com/dop251/goja"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
func TestCompileVariousScripts(t *testing.T) {
runtime := goja.New()
registry := versions.NewRegistry()
registry.Register("v4.2.3", v4_2_3.Source)
t.Run("let", func(t *testing.T) {
compiled, err := TranspileString("let a: number = 10;", WithCompileOptions(map[string]interface{}{
"module": "none",
}), WithVersion("v4.2.3"), WithRegistry(registry), WithRuntime(runtime))
require.NoError(t, err)
require.Equal(t, "var a = 10;", compiled)
})
t.Run("arrow function", func(t *testing.T) {
compiled, err := TranspileString("((): number => 10)()", WithCompileOptions(map[string]interface{}{
"module": "none",
}), WithVersion("v4.2.3"), WithRegistry(registry), WithRuntime(runtime))
require.NoError(t, err)
require.Equal(t, "(function () { return 10; })();", compiled)
})
}
//func TestCompileErrors(t *testing.T) {
// t.Run("bad syntax", func(t *testing.T) {
// _, err := TranspileString("asdjaksdhkjasd")
// require.Error(t, err)
// })
//}
func TestCancelContext(t *testing.T) {
runtime := goja.New()
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := TranspileCtx(ctx, strings.NewReader("let a: number = 10;"), WithRuntime(runtime))
require.Error(t, err)
}
func TestBadConfig(t *testing.T) {
_, err := TranspileString("let a: number = 10;", withFailOnInitialize())
require.Error(t, err)
}
func TestTranspile(t *testing.T) {
registry := versions.NewRegistry()
registry.Register("v4.2.3", v4_2_3.Source)
output, err := Transpile(strings.NewReader("let a: number = 10;"), WithRegistry(registry), WithVersion("v4.2.3"))
require.NoError(t, err)
require.Equal(t, "var a = 10;", output)
}