This repository was archived by the owner on Feb 17, 2025. It is now read-only.
File tree 4 files changed +38
-0
lines changed
4 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
8
8
## [ Unreleased] ( https://github.com/fluentassert/verify/compare/v1.1.0...HEAD )
9
9
10
+ ### Added
11
+
12
+ - Add ` FailureMessage.Err ` method together with ` AssertionError ` type
13
+ to represent assertion results as ` error ` type.
14
+
10
15
## [ 1.1.0] ( https://github.com/fluentassert/verify/releases/tag/v1.1.0 ) - 2024-02-06
11
16
12
17
This release adds length assertions.
Original file line number Diff line number Diff line change
1
+ package verify
2
+
3
+ // AssertionError is an error type used to represent failure messages from assertions.
4
+ // It is compatible with the error interface and can be used in instances where an error shall be returned instead of failing a test.
5
+ type AssertionError struct {
6
+ Message FailureMessage
7
+ }
8
+
9
+ // Error returns the failure message as a string. It makes AssertionError compatible with the error interface.
10
+ func (err * AssertionError ) Error () string {
11
+ return string (err .Message )
12
+ }
Original file line number Diff line number Diff line change @@ -82,3 +82,12 @@ func (msg FailureMessage) Prefix(s string) FailureMessage {
82
82
}
83
83
return FailureMessage (s ) + msg
84
84
}
85
+
86
+ // Err returns the failure message as an error type, or nil if the message is empty.
87
+ func (msg FailureMessage ) Err () * AssertionError {
88
+ if msg == "" {
89
+ return nil
90
+ }
91
+
92
+ return & AssertionError {Message : msg }
93
+ }
Original file line number Diff line number Diff line change @@ -137,6 +137,18 @@ func TestFailureMessage(t *testing.T) {
137
137
assertEqual (t , got , "[fail] errored" )
138
138
})
139
139
})
140
+
141
+ t .Run ("AsError" , func (t * testing.T ) {
142
+ t .Run ("With Message" , func (t * testing.T ) {
143
+ got := verify .FailureMessage ("failed" ).Err ()
144
+ assertEqual (t , got .Error (), "failed" )
145
+ })
146
+
147
+ t .Run ("Empty" , func (t * testing.T ) {
148
+ got := verify .FailureMessage ("" ).Err ()
149
+ assertEqual (t , got , nil )
150
+ })
151
+ })
140
152
}
141
153
142
154
type errorMock struct {
You can’t perform that action at this time.
0 commit comments