-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy patherr_test.go
103 lines (93 loc) · 2.86 KB
/
err_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package meta
import (
"errors"
"testing"
"github.com/pingcap/check"
)
func TestValidateErrIs(t *testing.T) {
var c *check.C
err0 := &ValidateErr{
Type: "dummy",
Target: "test",
LHS: "LHS",
RHS: "RHS",
Value: 1,
}
// identical errors are equal
c.Assert(errors.Is(err0, err0), check.IsTrue)
c.Assert(errors.Is(ErrValidate, ErrValidate), check.IsTrue)
c.Assert(errors.Is(ErrValidate, &ValidateErr{}), check.IsTrue)
c.Assert(errors.Is(&ValidateErr{}, ErrValidate), check.IsTrue)
// not equal for different error types
c.Assert(errors.Is(err0, errors.New("")), check.IsFalse)
// default Value matches any error
c.Assert(errors.Is(err0, ErrValidate), check.IsTrue)
// error with values are not matching default ones
c.Assert(errors.Is(ErrValidate, err0), check.IsFalse)
err1 := &ValidateErr{
Type: TypeConflict,
Target: "test",
LHS: "LHS",
RHS: "RHS",
Value: 2,
}
c.Assert(errors.Is(err1, ErrValidate), check.IsTrue)
// errors with different values are not equal
c.Assert(errors.Is(err0, err1), check.IsFalse)
c.Assert(errors.Is(err1, err0), check.IsFalse)
// errors with different types are not equal
err0.Value = 2
c.Assert(errors.Is(err0, err1), check.IsFalse)
c.Assert(errors.Is(err1, err0), check.IsFalse)
err2 := &ValidateErr{
Type: TypeMismatch,
Target: "test",
LHS: "LHS",
RHS: "RHS",
Value: map[string]any{
"key1": 1,
"key2": "2",
},
}
c.Assert(errors.Is(err2, ErrValidate), check.IsTrue)
c.Assert(errors.Is(err1, err2), check.IsFalse)
c.Assert(errors.Is(err2, err1), check.IsFalse)
err3 := &ValidateErr{
Type: TypeMismatch,
Target: "test",
LHS: "LHS",
RHS: "RHS",
Value: []float64{1.0, 2.0},
}
c.Assert(errors.Is(err3, ErrValidate), check.IsTrue)
// different values are not equal
c.Assert(errors.Is(err2, err3), check.IsFalse)
c.Assert(errors.Is(err3, err2), check.IsFalse)
err4 := &ValidateErr{
Type: TypeMismatch,
Target: "test",
LHS: "LHS",
RHS: "RHS",
Value: nil,
}
c.Assert(errors.Is(err4, ErrValidate), check.IsTrue)
// nil Value matches any error if other fields are with the same values
c.Assert(errors.Is(err3, err4), check.IsTrue)
c.Assert(errors.Is(err4, err3), check.IsFalse)
err4.Value = 0
c.Assert(errors.Is(err4, ErrValidate), check.IsTrue)
c.Assert(errors.Is(err3, err4), check.IsFalse)
c.Assert(errors.Is(err4, err3), check.IsFalse)
}