Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 08abbfe

Browse files
committed
Change Merge to Prefix, And, Or
1 parent 06634e5 commit 08abbfe

File tree

8 files changed

+144
-75
lines changed

8 files changed

+144
-75
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ The main additions are the new assertions for
6262
via `FluentAsync` and `FluentPeriodic` types:
6363
- `Eventually`
6464
- `EventuallyContext`
65-
- Add `FailureMessage.Merge` method to to facilitate accumulating failure messages.
65+
- Add `FailureMessage.Prefix` method together with `And` and `Or` functions
66+
to facilitate creating complex assertions.
6667

6768
### Changed
6869

README.md

+44-48
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,44 @@ $ go test
160160
161161
## Extensibility
162162
163-
### Custom fluent assertions
163+
### Custom predicates
164164
165-
You can take advantage of the `verify.FailureMessage` and `verify.Fluent*` types
166-
to create your own fluent assertions for a given type.
165+
For the most basic scenarios, you can use one of the
166+
`Check`, `Should`, `ShouldNot` assertions.
167167
168-
For reference, take a look at the implementation
169-
of existing fluent assertions in this repository
170-
(for example [comparable.go](verify/comparable.go)).
168+
```go
169+
package test
170+
171+
import (
172+
"strings"
173+
"testing"
174+
175+
"github.com/fluentassert/verify"
176+
)
177+
178+
func TestShould(t *testing.T) {
179+
got := "wrong"
180+
181+
chars := "abc"
182+
verify.Obj(got).Should(func(got string) bool {
183+
return strings.ContainsAny(got, chars)
184+
}).Assertf(t, "does not contain any of: %s", chars)
185+
}
186+
```
187+
188+
```sh
189+
$ go test
190+
--- FAIL: TestShould (0.00s)
191+
should_test.go:16: does not contain any of: abc
192+
object does not meet the predicate criteria
193+
got: "wrong"
194+
```
171195
172196
### Custom assertion function
173197
174-
For simple cases, you can simply prepare a function that returns `FailureMessage`.
198+
You can create a function that returns `FailureMessage`.
199+
Use `And` and `Or` functions together with `FailureMessage.Prefix` method
200+
to create complex assertions.
175201
176202
```go
177203
package test
@@ -194,62 +220,32 @@ func TestCustom(t *testing.T) {
194220
}
195221

196222
func verifyA(got A) verify.FailureMessage {
197-
var msg verify.FailureMessage
198-
msg.Merge("got.String assertion:",
199-
verify.String(got.Str).Contain("ok"),
200-
)
201-
msg.Merge("got.Ok assertion:",
202-
verify.True(got.Ok),
223+
return verify.And(
224+
verify.String(got.Str).Contain("ok").Prefix("got.String: "),
225+
verify.True(got.Ok).Prefix("got.Ok: "),
203226
)
204-
return msg
205227
}
206228
```
207229
208230
```sh
209231
$ go test
210232
--- FAIL: TestCustom (0.00s)
211233
custom_test.go:17:
212-
got.String assertion:
213-
the value does not contain the substring
234+
got.String: the value does not contain the substring
214235
got: "something was wrong"
215236
substr: "ok"
216237

217-
got.Ok assertion:
218-
the value is false
238+
got.Ok: the value is false
219239
```
220240
221-
### Custom predicates
222-
223-
For the most basic scenarios, you can also use one of the
224-
`Check`, `Should`, `ShouldNot` assertions.
225-
226-
```go
227-
package test
228-
229-
import (
230-
"strings"
231-
"testing"
232-
233-
"github.com/fluentassert/verify"
234-
)
235-
236-
func TestShould(t *testing.T) {
237-
got := "wrong"
241+
### Custom fluent assertions
238242
239-
chars := "abc"
240-
verify.Obj(got).Should(func(got string) bool {
241-
return strings.ContainsAny(got, chars)
242-
}).Assertf(t, "does not contain any of: %s", chars)
243-
}
244-
```
243+
You can take advantage of the `FailureMessage` and `Fluent*` types
244+
to create your own fluent assertions for a given type.
245245
246-
```sh
247-
$ go test
248-
--- FAIL: TestShould (0.00s)
249-
should_test.go:16: does not contain any of: abc
250-
object does not meet the predicate criteria
251-
got: "wrong"
252-
```
246+
For reference, take a look at the implementation
247+
of existing fluent assertions in this repository
248+
(for example [comparable.go](comparable.go)).
253249
254250
## Supported Go versions
255251

and.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package verify
2+
3+
// And accumalates non-empty failure messages.
4+
func And(assertions ...FailureMessage) FailureMessage {
5+
var msg FailureMessage
6+
for _, assertion := range assertions {
7+
if assertion == "" {
8+
continue
9+
}
10+
if msg == "" {
11+
msg = assertion
12+
continue
13+
}
14+
msg = msg + "\n\n" + assertion
15+
}
16+
return msg
17+
}

and_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package verify_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/fluentassert/verify"
7+
)
8+
9+
func TestAnd(t *testing.T) {
10+
t.Run("Empty", func(t *testing.T) {
11+
msg := verify.And()
12+
assertPassed(t, msg)
13+
})
14+
t.Run("NoneFailed", func(t *testing.T) {
15+
msg := verify.And("", "")
16+
assertPassed(t, msg)
17+
})
18+
t.Run("OneFailed", func(t *testing.T) {
19+
msg := verify.And("", "failure")
20+
assertFailed(t, msg, "failure")
21+
})
22+
t.Run("TwoFailed", func(t *testing.T) {
23+
msg := verify.And("one", "two")
24+
assertFailed(t, msg, "one\n\ntwo")
25+
})
26+
}

failmsg.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,10 @@ func (msg FailureMessage) Requiref(t interface {
7575
return false
7676
}
7777

78-
// Merge accumalates a non-empty failure message.
79-
//
80-
// Merge can be used for creating custom assertions.
81-
func (msg *FailureMessage) Merge(header string, failureMessage FailureMessage) {
82-
if failureMessage == "" {
83-
return
84-
}
85-
if *msg == "" {
86-
*msg = FailureMessage(header) + "\n" + failureMessage
87-
return
78+
// Prefix adds prefix if the failure message is not empty.
79+
func (msg FailureMessage) Prefix(s string) FailureMessage {
80+
if msg == "" {
81+
return ""
8882
}
89-
*msg = *msg + "\n\n" + FailureMessage(header) + "\n" + failureMessage
83+
return FailureMessage(s) + msg
9084
}

failmsg_test.go

+7-15
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,14 @@ func TestFailureMessage(t *testing.T) {
127127
})
128128
})
129129

130-
t.Run("Merge", func(t *testing.T) {
131-
t.Run("BothEmpty", func(t *testing.T) {
132-
var first, second verify.FailureMessage
133-
first.Merge("assertion", second)
134-
assertPassed(t, first)
135-
})
136-
t.Run("ArgIsNotEmpty", func(t *testing.T) {
137-
var msg verify.FailureMessage
138-
msg.Merge("assertion", verify.FailureMessage("failure"))
139-
assertFailed(t, msg, "assertion\nfailure")
130+
t.Run("Prefix", func(t *testing.T) {
131+
t.Run("Empty", func(t *testing.T) {
132+
got := verify.FailureMessage("").Prefix("[fail]")
133+
assertEqual(t, got, "")
140134
})
141-
t.Run("NoneIsEmpty", func(t *testing.T) {
142-
var msg verify.FailureMessage
143-
msg.Merge("first", verify.FailureMessage("error"))
144-
msg.Merge("second", verify.FailureMessage("failure"))
145-
assertFailed(t, msg, "first\nerror\n\nsecond\nfailure")
135+
t.Run("Empty", func(t *testing.T) {
136+
got := verify.FailureMessage("errored").Prefix("[fail] ")
137+
assertEqual(t, got, "[fail] errored")
146138
})
147139
})
148140
}

or.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package verify
2+
3+
// Or accumalates failure messages if all are not empty.
4+
func Or(assertions ...FailureMessage) FailureMessage {
5+
var msg FailureMessage
6+
for _, assertion := range assertions {
7+
if assertion == "" {
8+
return ""
9+
}
10+
if msg == "" {
11+
msg = assertion
12+
continue
13+
}
14+
msg = msg + "\n\n" + assertion
15+
}
16+
return msg
17+
}

or_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package verify_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/fluentassert/verify"
7+
)
8+
9+
func TestOr(t *testing.T) {
10+
t.Run("Empty", func(t *testing.T) {
11+
msg := verify.Or()
12+
assertPassed(t, msg)
13+
})
14+
t.Run("NoneFailed", func(t *testing.T) {
15+
msg := verify.Or("", "")
16+
assertPassed(t, msg)
17+
})
18+
t.Run("OneFailed", func(t *testing.T) {
19+
msg := verify.Or("", "failure")
20+
assertPassed(t, msg)
21+
})
22+
t.Run("TwoFailed", func(t *testing.T) {
23+
msg := verify.Or("one", "two")
24+
assertFailed(t, msg, "one\n\ntwo")
25+
})
26+
}

0 commit comments

Comments
 (0)