forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinspector_test.go
130 lines (114 loc) · 3.39 KB
/
inspector_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
124
125
126
127
128
129
130
package v8go_test
import (
"reflect"
"testing"
v8 "github.com/tommie/v8go"
)
type consoleAPIMessage struct {
Message string
ErrorLevel v8.MessageErrorLevel
}
type consoleAPIMessageRecorder struct {
messages []consoleAPIMessage
}
func (r *consoleAPIMessageRecorder) ConsoleAPIMessage(msg v8.ConsoleAPIMessage) {
r.messages = append(r.messages, consoleAPIMessage{
Message: msg.Message,
ErrorLevel: msg.ErrorLevel,
})
}
type IsolateWithInspector struct {
iso *v8.Isolate
inspector *v8.Inspector
inspectorClient *v8.InspectorClient
}
func NewIsolateWithInspectorClient(handler v8.ConsoleAPIMessageHandler) *IsolateWithInspector {
iso := v8.NewIsolate()
client := v8.NewInspectorClient(handler)
inspector := v8.NewInspector(iso, client)
return &IsolateWithInspector{
iso,
inspector,
client,
}
}
func (iso *IsolateWithInspector) Dispose() {
iso.inspector.Dispose()
iso.inspectorClient.Dispose()
iso.iso.Dispose()
}
type ContextWithInspector struct {
*v8.Context
iso *IsolateWithInspector
}
func (iso *IsolateWithInspector) NewContext() *ContextWithInspector {
context := v8.NewContext(iso.iso)
iso.inspector.ContextCreated(context)
return &ContextWithInspector{context, iso}
}
func (ctx *ContextWithInspector) Dispose() {
ctx.iso.inspector.ContextDestroyed(ctx.Context)
ctx.Context.Close()
}
func TestMonitorConsoleLogLevelt(t *testing.T) {
t.Parallel()
recorder := consoleAPIMessageRecorder{}
iso := NewIsolateWithInspectorClient(&recorder)
defer iso.Dispose()
context := iso.NewContext()
defer context.Dispose()
_, err := context.RunScript(`
console.log("Log msg");
console.info("Info msg");
console.debug("Debug msg");
console.warn("Warn msg");
console.error("Error msg");
`, "")
if err != nil {
t.Fatal("Error occurred", err)
}
actual := recorder.messages
expected := []consoleAPIMessage{
{Message: "Log msg", ErrorLevel: v8.ErrorLevelLog},
{Message: "Info msg", ErrorLevel: v8.ErrorLevelInfo},
{Message: "Debug msg", ErrorLevel: v8.ErrorLevelDebug},
{Message: "Warn msg", ErrorLevel: v8.ErrorLevelWarning},
{Message: "Error msg", ErrorLevel: v8.ErrorLevelError},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Unexpected messages. \nExpected: %v\nGot: %v", expected, actual)
}
}
// Verify utf-16 conversion. Internally, the strings are represented by a
// StringView, which is undocumented. Experiements shows that the values
// returned are an utf-16le encoded array, and a length.
//
// The length is assumed to be the size of the array, not the number of
// characters. This test verifies that, by writing a character that needs
// several utf-16 elements for endocing.
//
// https://v8.github.io/api/head/classv8__inspector_1_1StringView.html
func TestMonitorConsoleLogWideCharacters(t *testing.T) {
t.Parallel()
recorder := consoleAPIMessageRecorder{}
iso := NewIsolateWithInspectorClient(&recorder)
defer iso.Dispose()
context := iso.NewContext()
defer context.Dispose()
_, err := context.RunScript(`
console.log("This character takes up multiple utf-16 values: 𐀀");
`, "")
if err != nil {
t.Fatal("Error occurred", err)
}
actual := recorder.messages
expected := []consoleAPIMessage{
{
Message: "This character takes up multiple utf-16 values: 𐀀",
ErrorLevel: v8.ErrorLevelLog,
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Unexpected messages. \nExpected: %v\nGot: %v", expected, actual)
}
}