MCPcopy Index your code
hub / github.com/pkg/errors / TestAs

Function TestAs

go113_test.go:80–137  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

78func (c customErr) Error() string { return c.msg }
79
80func TestAs(t *testing.T) {
81 var err = customErr{msg: "test message"}
82
83 type args struct {
84 err error
85 target interface{}
86 }
87 tests := []struct {
88 name string
89 args args
90 want bool
91 }{
92 {
93 name: "with stack",
94 args: args{
95 err: WithStack(err),
96 target: new(customErr),
97 },
98 want: true,
99 },
100 {
101 name: "with message",
102 args: args{
103 err: WithMessage(err, "test"),
104 target: new(customErr),
105 },
106 want: true,
107 },
108 {
109 name: "with message format",
110 args: args{
111 err: WithMessagef(err, "%s", "test"),
112 target: new(customErr),
113 },
114 want: true,
115 },
116 {
117 name: "std errors compatibility",
118 args: args{
119 err: fmt.Errorf("wrap it: %w", err),
120 target: new(customErr),
121 },
122 want: true,
123 },
124 }
125 for _, tt := range tests {
126 t.Run(tt.name, func(t *testing.T) {
127 if got := As(tt.args.err, tt.args.target); got != tt.want {
128 t.Errorf("As() = %v, want %v", got, tt.want)
129 }
130
131 ce := tt.args.target.(*customErr)
132 if !reflect.DeepEqual(err, *ce) {
133 t.Errorf("set target error failed, target error is %v", *ce)
134 }
135 })
136 }
137}

Callers

nothing calls this directly

Calls 4

WithStackFunction · 0.85
WithMessageFunction · 0.85
WithMessagefFunction · 0.85
AsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…