-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathengine_require_test.go
More file actions
121 lines (114 loc) · 2.37 KB
/
engine_require_test.go
File metadata and controls
121 lines (114 loc) · 2.37 KB
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
116
117
118
119
120
121
package engine
import (
"fmt"
"github.com/ZenLiuCN/fn"
"os"
"strconv"
"strings"
"testing"
)
func validate(im, msg string) {
defer func() {
switch x := recover().(type) {
case error:
panic(fmt.Errorf("%s %w", msg, x))
case nil:
default:
panic(fmt.Errorf("%s %+v", msg, x))
}
}()
e := NewEngine()
e.Debug = true
defer e.Free()
call := fn.Panic1(e.RunJs(
//language=javascript
fmt.Sprintf(`
import {Some,v} from '%s'
console.log('import')
console.log(Some())
const x=()=>v
x()
`, im))).Export().(int64)
e.Await()
if call != 1 {
panic("should only call once: " + strconv.Itoa(int(call)))
}
_, _ = fmt.Fprintf(os.Stdout, "done: %s \n", msg)
}
func TestRequireFunc(t *testing.T) {
code := []byte(
//language=javascript
`
export let v=0
export function Some(){
console.log("run",v)
return v++
}
`)
_ = os.WriteFile("func.js", code, os.ModePerm)
defer func() {
_ = os.Remove("func.js")
}()
_ = os.Mkdir("func_", os.ModePerm)
_ = os.WriteFile("func_/index.js", code, os.ModePerm)
_ = os.WriteFile("func_/func.js", code, os.ModePerm)
defer func() {
_ = os.RemoveAll("func_")
}()
validate("func", "pwd without extension")
validate("./func", "relative without extension")
validate("./func_", "relative with index")
validate("func_/", "pwd with index")
validate("func_/index", "pwd with index")
validate("func_/func", "pwd with filename")
}
func TestRequireWithFailure(t *testing.T) {
e := NewEngine()
e.Debug = true
defer e.Free()
_, err := e.RunJs(
//language=javascript
fmt.Sprintf(`
import {Some,v} from '%s'
console.log('import')
console.log(Some())
const x=()=>v
x()
`, "func"))
e.Await()
if err == nil || !strings.HasSuffix(err.Error(), "vm.js[2:21]\timport {Some,v} from ♦'func'") {
panic(err)
}
}
func TestRequireWithFailure2(t *testing.T) {
code := []byte(
//language=javascript
`
import {Some as s1,v} from 'func/some'
export function Some(){
console.log("run",v)
s1()
return v++
}
`)
_ = os.WriteFile("func.js", code, os.ModePerm)
defer func() {
_ = os.Remove("func.js")
}()
e := NewEngine()
e.Debug = true
defer e.Free()
_, err := e.RunJs(
//language=javascript
fmt.Sprintf(`
import {Some,v} from '%s'
console.log('import')
console.log(Some())
const x=()=>v
x()
`, "func"))
e.Await()
if err == nil || !strings.HasSuffix(err.Error(), "vm.js[2:21]\timport {Some,v} from ♦'func'") {
panic(err)
}
}