forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
92 lines (81 loc) · 2.13 KB
/
errors_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
// 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"
)
func TestErrorFormatting(t *testing.T) {
t.Parallel()
tests := [...]struct {
name string
err error
defaultVerb string
defaultVerbFlag string
stringVerb string
quoteVerb string
}{
{"WithStack", &v8.JSError{Message: "msg", StackTrace: "stack"}, "msg", "stack", "msg", `"msg"`},
{"WithoutStack", &v8.JSError{Message: "msg"}, "msg", "msg", "msg", `"msg"`},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if s := fmt.Sprintf("%v", tt.err); s != tt.defaultVerb {
t.Errorf("incorrect format for %%v: %s", s)
}
if s := fmt.Sprintf("%+v", tt.err); s != tt.defaultVerbFlag {
t.Errorf("incorrect format for %%+v: %s", s)
}
if s := fmt.Sprintf("%s", tt.err); s != tt.stringVerb {
t.Errorf("incorrect format for %%s: %s", s)
}
if s := fmt.Sprintf("%q", tt.err); s != tt.quoteVerb {
t.Errorf("incorrect format for %%q: %s", s)
}
})
}
}
func TestJSErrorOutput(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
defer ctx.Isolate().Dispose()
defer ctx.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")
if err == 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)
}
if e.Message != "ReferenceError: c is not defined" {
t.Errorf("unexpected error message: %q", e.Message)
}
if e.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`
if e.StackTrace != expectedStack {
t.Errorf("unexpected error stack trace: %q", e.StackTrace)
}
}