forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexception_test.go
79 lines (66 loc) · 1.77 KB
/
exception_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
// Copyright 2021 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 (
"errors"
"strings"
"testing"
v8 "github.com/tommie/v8go"
)
func TestNewError(t *testing.T) {
t.Parallel()
tsts := []struct {
New func(*v8.Isolate, string) *v8.Exception
WantType string
}{
{v8.NewRangeError, "RangeError"},
{v8.NewReferenceError, "ReferenceError"},
{v8.NewSyntaxError, "SyntaxError"},
{v8.NewTypeError, "TypeError"},
{v8.NewWasmCompileError, "CompileError"},
{v8.NewWasmLinkError, "LinkError"},
{v8.NewWasmRuntimeError, "RuntimeError"},
{v8.NewError, "Error"},
}
for _, tst := range tsts {
t.Run(tst.WantType, func(t *testing.T) {
iso := v8.NewIsolate()
defer iso.Dispose()
got := tst.New(iso, "amessage")
if !got.IsNativeError() {
t.Error("IsNativeError returned false, want true")
}
if got := got.Error(); !strings.Contains(got, " "+tst.WantType+":") {
t.Errorf("Error(): got %q, want containing %q", got, tst.WantType)
}
})
}
}
func TestExceptionAs(t *testing.T) {
iso := v8.NewIsolate()
defer iso.Dispose()
want := v8.NewRangeError(iso, "faked error")
var got *v8.Exception
if !want.As(&got) {
t.Fatalf("As failed")
}
if got != want {
t.Errorf("As: got %#v, want %#v", got, want)
}
}
func TestExceptionIs(t *testing.T) {
iso := v8.NewIsolate()
defer iso.Dispose()
t.Run("ok", func(t *testing.T) {
ex := v8.NewRangeError(iso, "faked error")
if !ex.Is(v8.NewRangeError(iso, "faked error")) {
t.Fatalf("Is: got false, want true")
}
})
t.Run("notok", func(t *testing.T) {
if (&v8.Exception{}).Is(errors.New("other error")) {
t.Fatalf("Is: got true, want false")
}
})
}