summaryrefslogtreecommitdiff
path: root/errors/debug_test.go
blob: bc866bc84af625ba0aa42eca8fe19533c45ca289 (plain)
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
// +build debug

package errors

import (
	"fmt"
	"regexp"
	"strings"
	"testing"
)

var errorLines = strings.Split(strings.TrimSpace(`
	.*/errors/debug_test.go:\d+:  github.com/ValeryPiashchynski/errors.func1:
	.*/errors/debug_test.go:\d+:  ...T.func2:
	.*/errors/debug_test.go:\d+:  ...func3:
	.*/errors/debug_test.go:\d+:  ...func4: func2 invoke func3: Network error:
	func4 operation: error in action
`), "\n")

var errorLineREs = make([]*regexp.Regexp, len(errorLines))

func init() {
	for i, s := range errorLines {
		errorLineREs[i] = regexp.MustCompile(fmt.Sprintf("^%s", s))
	}
}

func TestsDebug(t *testing.T) {
	got := printErr(t, func1())
	lines := strings.Split(got, "\n")
	for i, re := range errorLineREs {
		if i >= len(lines) {
			// Handled by line number check.
			break
		}
		if !re.MatchString(lines[i]) {
			t.Errorf("error does not match at line %v, got:\n\t%q\nwant:\n\t%q", i, lines[i], re)
		}
	}
	if got, want := len(lines), len(errorLines); got != want {
		t.Errorf("got %v lines of errors, want %v", got, want)
	}
}

type T struct{}

func printErr(t *testing.T, err error) string {
	return err.Error()
}

func func1() error {
	var t T
	return t.func2()
}

func (T) func2() error {
	o := Op("func2 invoke func3")
	return E(o, func3())
}

func func3() error {
	return func4()
}

func func4() error {
	o := Op("func4 operation")
	return E(o, Network, Str("error in action"))
}

///Users/0xdev/Projects/repo/errors/debug_test.go:53: github.com/ValeryPiashchynski/errors.func1:
///Users/0xdev/Projects/repo/errors/debug_test.go:58: ...T.func2:
///Users/0xdev/Projects/repo/errors/debug_test.go:62: ...func3:
///Users/0xdev/Projects/repo/errors/debug_test.go:67: ...func4: func2 invoke func3: Network error:
//func4 operation: error in action