-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtag_test.go
59 lines (49 loc) · 1.2 KB
/
tag_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
package goerr_test
import (
"fmt"
"testing"
"github.com/m-mizutani/goerr/v2"
)
func ExampleNewTag() {
t1 := goerr.NewTag("DB error")
err := goerr.New("error message", goerr.Tag(t1))
if goErr := goerr.Unwrap(err); goErr != nil {
if goErr.HasTag(t1) {
fmt.Println("DB error")
}
}
// Output: DB error
}
func TestNewTag(t *testing.T) {
tagValue := "test_tag"
tag := goerr.NewTag(tagValue)
if tag.String() != tagValue {
t.Errorf("expected tag value to be %s, got %s", tagValue, tag.String())
}
}
func TestWithTags(t *testing.T) {
tag1 := goerr.NewTag("tag1")
tag2 := goerr.NewTag("tag2")
tag3 := goerr.NewTag("tag3")
err := goerr.New("error message", goerr.Tag(tag1), goerr.Tag(tag2))
if goErr := goerr.Unwrap(err); goErr != nil {
if !goErr.HasTag(tag1) {
t.Errorf("expected error to have tag1")
}
if !goErr.HasTag(tag2) {
t.Errorf("expected error to have tag2")
}
if goErr.HasTag(tag3) {
t.Errorf("expected error to not have tag3")
}
}
}
func TestHasTag(t *testing.T) {
tag := goerr.NewTag("test_tag")
err := goerr.New("error message", goerr.Tag(tag))
if goErr := goerr.Unwrap(err); goErr != nil {
if !goErr.HasTag(tag) {
t.Errorf("expected error to have tag")
}
}
}