-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
79 lines (71 loc) · 2.05 KB
/
Copy patherrors_test.go
File metadata and controls
79 lines (71 loc) · 2.05 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
package hocon_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/o3co/go.hocon"
)
func TestParseError_Error(t *testing.T) {
err := &hocon.ParseError{Message: "unexpected token", Line: 3, Col: 5}
got := err.Error()
if got == "" {
t.Fatal("Error() returned empty string")
}
// must contain line info
if got != "parse error at line 3, col 5: unexpected token" {
t.Errorf("unexpected format: %q", got)
}
}
func TestResolveError_Error(t *testing.T) {
err := &hocon.ResolveError{Message: "circular reference", Path: "a.b"}
got := err.Error()
if got == "" {
t.Fatal("Error() returned empty string")
}
}
func TestConfigError_Error(t *testing.T) {
err := &hocon.ConfigError{Message: "missing key", Path: "server.host"}
got := err.Error()
if got == "" {
t.Fatal("Error() returned empty string")
}
}
func TestParseError_IsError(t *testing.T) {
pe := &hocon.ParseError{Message: "oops"}
var err error = pe
var target *hocon.ParseError
if !errors.As(err, &target) {
t.Fatal("errors.As failed for ParseError")
}
}
func TestErrNotResolved_Sentinel(t *testing.T) {
if hocon.ErrNotResolved == nil {
t.Fatal("ErrNotResolved must be defined as a sentinel")
}
wrapped := fmt.Errorf("getter at path %q: %w", "foo.bar", hocon.ErrNotResolved)
if !errors.Is(wrapped, hocon.ErrNotResolved) {
t.Fatal("errors.Is must match ErrNotResolved through wrapping")
}
}
func TestParseString_ErrorHasLineCol(t *testing.T) {
// Trigger a parse error and verify the public ParseError has Line/Col populated.
_, err := hocon.ParseString("{ a = 1")
if err == nil {
t.Fatal("expected error")
}
var pe *hocon.ParseError
if !errors.As(err, &pe) {
t.Fatalf("expected *hocon.ParseError, got %T", err)
}
if pe.Line == 0 {
t.Error("expected Line > 0 in ParseError")
}
if pe.Col == 0 {
t.Error("expected Col > 0 in ParseError")
}
// Message should contain only the description, not the "parse error at line..." prefix.
if strings.Contains(pe.Message, "parse error at") {
t.Errorf("Message should not contain 'parse error at' prefix, got: %s", pe.Message)
}
}