Uh oh!
There was an error while loading. Please reload this page.
forked from rogchap/v8go
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
Latest commit
119 lines (105 loc) · 2.77 KB
/
Copy patherrors_test.go
File metadata and controls
119 lines (105 loc) · 2.77 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
// Copyright 2019 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"fmt"
"testing"
v8 "rogchap.com/v8go"
)
funcTestJSErrorFormat(t*testing.T) {
t.Parallel()
tests:= [...]struct {
namestring
errerror
defaultVerbstring
defaultVerbFlagstring
stringVerbstring
quoteVerbstring
}{
{"WithStack", &v8.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
{"WithoutStack", &v8.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
}
for_, tt:=rangetests {
tt:=tt
t.Run(tt.name, func(t*testing.T) {
t.Parallel()
ifs:=fmt.Sprintf("%v", tt.err); s!=tt.defaultVerb {
t.Errorf("incorrect format for %%v: %s", s)
}
ifs:=fmt.Sprintf("%+v", tt.err); s!=tt.defaultVerbFlag {
t.Errorf("incorrect format for %%+v: %s", s)
}
ifs:=fmt.Sprintf("%s", tt.err); s!=tt.stringVerb {
t.Errorf("incorrect format for %%s: %s", s)
}
ifs:=fmt.Sprintf("%q", tt.err); s!=tt.quoteVerb {
t.Errorf("incorrect format for %%q: %s", s)
}
})
}
}
funcTestJSErrorOutput(t*testing.T) {
t.Parallel()
ctx:=v8.NewContext(nil)
deferctx.Isolate().Dispose()
deferctx.Close()
math:=`
function add(a, b) {
return a + b;
}
function addMore(a, b) {
return add(a, c);
}`
main:=`
let a = add(3, 5);
let b = addMore(a, 6);
b;
`
ctx.RunScript(math, "math.js")
_, err:=ctx.RunScript(main, "main.js")
iferr==nil {
t.Error("expected error but got <nil>")
return
}
e, ok:=err.(*v8.JSError)
if!ok {
t.Errorf("expected error of type JSError, got %T", err)
}
ife.Message!="ReferenceError: c is not defined" {
t.Errorf("unexpected error message: %q", e.Message)
}
ife.Location!="math.js:7:17" {
t.Errorf("unexpected error location: %q", e.Location)
}
expectedStack:=`ReferenceError: c is not defined
at addMore (math.js:7:17)
at main.js:3:10`
ife.StackTrace!=expectedStack {
t.Errorf("unexpected error stack trace: %q", e.StackTrace)
}
}
funcTestJSErrorFormat_forSyntaxError(t*testing.T) {
t.Parallel()
iso:=v8.NewIsolate()
deferiso.Dispose()
ctx:=v8.NewContext(iso)
deferctx.Close()
script:=`
let x = 1;
let y = x + ;
let z = x + z;
`
_, err:=ctx.RunScript(script, "xyz.js")
jsErr:=err.(*v8.JSError)
ifjsErr.StackTrace!=jsErr.Message {
t.Errorf("unexpected StackTrace %q not equal to Message %q", jsErr.StackTrace, jsErr.Message)
}
ifjsErr.Location=="" {
t.Errorf("missing Location")
}
msg:=fmt.Sprintf("%+v", err)
ifmsg!="SyntaxError: Unexpected token ';' (at xyz.js:3:15)" {
t.Errorf("unexpected verbose error message: %q", msg)
}
}