-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibjs_test.go
123 lines (106 loc) · 2.87 KB
/
libjs_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
116
117
118
119
120
121
122
123
package frizzante
import (
"github.com/evanw/esbuild/pkg/api"
"rogchap.com/v8go"
"strings"
"testing"
)
func TestNewJavaScriptContext(test *testing.T) {
_, err := newJavaScriptContext(map[string]v8go.FunctionCallback{})
if err != nil {
test.Fatal(err)
}
}
func TestJavaScriptRun(test *testing.T) {
// Simple.
script := "1+1"
actual, destroy, javaScriptError := JavaScriptRun(script, map[string]v8go.FunctionCallback{})
if javaScriptError != nil {
test.Fatal(javaScriptError)
}
defer destroy()
if actual.Int32() != 2 {
test.Fatalf("script was expected to return 2, received '%d' instead", actual.Int32())
}
// Complex and with JsDoc.
script = `
/**
* @param {boolean} payload
* @returns
*/
function uuid(short = false) {
let dt = new Date().getTime()
const BLUEPRINT = short ? 'xyxxyxyx' : 'xxxxxxxx-xxxx-yxxx-yxxx-xxxxxxxxxxxx'
const RESULT = BLUEPRINT.replace(/[xy]/g, function run(c) {
const r = (dt + Math.random() * 16) % 16 | 0
dt = Math.floor(dt / 16)
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
})
return RESULT
}
const result = {
long: uuid(),
short: uuid(true),
}
result
`
actual, destroy, javaScriptError = JavaScriptRun(script, map[string]v8go.FunctionCallback{})
if javaScriptError != nil {
test.Fatal(javaScriptError)
}
defer destroy()
obj := actual.Object()
if !obj.Has("long") {
test.Fatal("actual value was expected to have a 'long' key")
}
if !obj.Has("short") {
test.Fatal("actual value was expected to have a 'short' key")
}
long, longError := obj.Get("long")
if longError != nil {
test.Fatal(longError)
}
short, shortError := obj.Get("short")
if shortError != nil {
test.Fatal(shortError)
}
longPieces := strings.Split(long.String(), "-")
if len(longPieces) != 5 {
test.Fatalf("long string was expected to be composed of 5 part separated by 4 -, received '%s' instead", long.String())
}
shortPieces := strings.Split(short.String(), "-")
if len(shortPieces) != 1 {
test.Fatalf("string was expected to be composed of 1 part, received '%s' instead", short.String())
}
}
func TestJavaScriptBundle(test *testing.T) {
script := `
import { writable } from 'svelte/store'
const test = writable("hello")
test.subscribe(function updated(value){
signal(value)
})
`
cjs, bundleError := JavaScriptBundle("www", api.FormatCommonJS, script)
if bundleError != nil {
test.Fatal(bundleError)
}
actual := ""
expected := "hello"
_, destroy, javaScriptError := JavaScriptRun(cjs, map[string]v8go.FunctionCallback{
"signal": func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args()
if len(args) > 0 {
actual = args[0].String()
}
return nil
},
})
if javaScriptError != nil {
test.Fatal(javaScriptError)
}
defer destroy()
if actual != expected {
test.Fatalf("script was expected to update the actual value to '%s', received '%s' instead.", expected, actual)
}
}