summaryrefslogtreecommitdiff
path: root/errors/debug_test.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-10-22 10:57:48 +0300
committerValery Piashchynski <[email protected]>2020-10-22 10:57:48 +0300
commit77726578db6d539b151a5c4d36300b11e54a0bee (patch)
tree9c5247e50900bf6ab1b6ea2d54d11046be600ae0 /errors/debug_test.go
parent1102a5c1faf17ec3153b62b25749fafafd2c98eb (diff)
A
Diffstat (limited to 'errors/debug_test.go')
-rwxr-xr-xerrors/debug_test.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/errors/debug_test.go b/errors/debug_test.go
new file mode 100755
index 00000000..bc866bc8
--- /dev/null
+++ b/errors/debug_test.go
@@ -0,0 +1,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