File: error_1_13_test.go

package info (click to toggle)
golang-github-go-errors-errors 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 124 kB
  • sloc: makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,399 bytes parent folder | download
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
// +build go1.13

package errors

import (
	"io"
	"testing"
)

// This test should work only for go 1.13 and latter
func TestIs113(t *testing.T) {
	custErr := errorWithCustomIs{
		Key: "TestForFun",
		Err: io.EOF,
	}

	shouldMatch := errorWithCustomIs{
		Key: "TestForFun",
	}

	shouldNotMatch := errorWithCustomIs{Key: "notOk"}

	if !Is(custErr, shouldMatch) {
		t.Errorf("custErr is not a TestForFun customError")
	}

	if Is(custErr, shouldNotMatch) {
		t.Errorf("custErr is a notOk customError")
	}

	if !Is(custErr, New(shouldMatch)) {
		t.Errorf("custErr is not a New(TestForFun customError)")
	}

	if Is(custErr, New(shouldNotMatch)) {
		t.Errorf("custErr is a New(notOk customError)")
	}

	if !Is(New(custErr), shouldMatch) {
		t.Errorf("New(custErr) is not a TestForFun customError")
	}

	if Is(New(custErr), shouldNotMatch) {
		t.Errorf("New(custErr) is a notOk customError")
	}

	if !Is(New(custErr), New(shouldMatch)) {
		t.Errorf("New(custErr) is not a New(TestForFun customError)")
	}

	if Is(New(custErr), New(shouldNotMatch)) {
		t.Errorf("New(custErr) is a New(notOk customError)")
	}
}

type errorWithCustomIs struct {
	Key string
	Err error
}

func (ewci errorWithCustomIs) Error() string {
	return "[" + ewci.Key + "]: " + ewci.Err.Error()
}

func (ewci errorWithCustomIs) Is(target error) bool {
	matched, ok := target.(errorWithCustomIs)
	return ok && matched.Key == ewci.Key
}