Skip to content

Commit 921735b

Browse files
committed
impr: return pass/fail flag
1 parent 3868418 commit 921735b

3 files changed

Lines changed: 101 additions & 72 deletions

File tree

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ If you want simple test assertions and feel like [testify](https://pkg.go.dev/gi
44

55
Highlights:
66

7-
- Minimal API: `Equal`, `Err`, and `True` assertions.
8-
- Correctly compares `time.Time` values and other types with an `Equal` method.
9-
- Flexible error assertions: check if an error exists, check its value, type, or any combination of these.
10-
- Zero hassle.
7+
- Minimal API: `Equal`, `Err`, and `True` assertions.
8+
- Correctly compares `time.Time` values and other types with an `Equal` method.
9+
- Flexible error assertions: check if an error exists, check its value, type, or any combination of these.
10+
- Zero hassle.
1111

1212
Be is new, but it's ready for production (or maybe I should say "testing" :) I've used it in three very different projects — a CLI tool, an API server, and a database engine — and it worked great every time.
1313

@@ -19,6 +19,8 @@ Install with go get:
1919
go get github.com/nalgeon/be
2020
```
2121

22+
### Equality
23+
2224
`Equal` asserts that two values are equal:
2325

2426
```go
@@ -47,6 +49,8 @@ func Test(t *testing.T) {
4749
}
4850
```
4951

52+
### Errors
53+
5054
`Err` asserts that there is an error:
5155

5256
```go
@@ -119,6 +123,8 @@ func Test(t *testing.T) {
119123
}
120124
```
121125

126+
### True/false
127+
122128
`True` asserts that an expression is true:
123129

124130
```go
@@ -129,6 +135,21 @@ func Test(t *testing.T) {
129135
}
130136
```
131137

138+
### Conditional checks (work in progress)
139+
140+
Each `be` function also returns a `bool` that shows whether the assertion passed or failed. You can use this to stop early or run extra checks:
141+
142+
```go
143+
func Test(t *testing.T) {
144+
s := "go is awesome"
145+
if be.True(t, len(s) > 0) {
146+
be.Equal(t, s[0:2], "go")
147+
be.Equal(t, s[len(s)-2:], "me")
148+
}
149+
// ok
150+
}
151+
```
152+
132153
That's it!
133154

134155
## Design decisions

be.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,32 @@ type equaler[T any] interface {
2929
}
3030

3131
// Equal asserts that got is equal to any of the wanted values.
32-
func Equal[T any](tb testing.TB, got T, wants ...T) {
32+
// Returns true if there is a match, false otherwise.
33+
func Equal[T any](tb testing.TB, got T, wants ...T) bool {
3334
tb.Helper()
3435

3536
if len(wants) == 0 {
3637
tb.Fatal("no wants given")
37-
return
38+
return false
3839
}
3940

4041
// Check if got matches any of the wants.
4142
for _, want := range wants {
4243
if areEqual(got, want) {
43-
return
44+
return true
4445
}
4546
}
4647

4748
// There are no matches, report the failure.
4849
if len(wants) == 1 {
4950
// There is only one want, report it directly.
5051
tb.Errorf("got: %#v; want: %#v", got, wants[0])
51-
return
52+
return false
5253
}
54+
5355
// There are multiple wants, report a summary.
5456
tb.Errorf("got: %#v; want any of: %v", got, wants)
57+
return false
5558
}
5659

5760
// Err asserts that the got error matches any of the wanted values.
@@ -65,23 +68,24 @@ func Equal[T any](tb testing.TB, got T, wants ...T) {
6568
// - Otherwise fails the check.
6669
//
6770
// If no wants are given, checks if got is not nil.
68-
func Err(tb testing.TB, got error, wants ...any) {
71+
// Returns true if there is a match, false otherwise.
72+
func Err(tb testing.TB, got error, wants ...any) bool {
6973
tb.Helper()
7074

7175
// If no wants are given, we expect got to be a non-nil error.
7276
if len(wants) == 0 {
7377
if got == nil {
7478
tb.Error("got: <nil>; want: error")
7579
}
76-
return
80+
return got != nil
7781
}
7882

7983
// Special case: there's only one want, it's nil, but got is not nil.
8084
// This is a fatal error, so we fail the test immediately.
8185
if len(wants) == 1 && wants[0] == nil {
8286
if got != nil {
8387
tb.Fatalf("unexpected error: %v", got)
84-
return
88+
return false
8589
}
8690
}
8791

@@ -90,7 +94,7 @@ func Err(tb testing.TB, got error, wants ...any) {
9094
for _, want := range wants {
9195
errMsg := checkErr(got, want)
9296
if errMsg == "" {
93-
return
97+
return true
9498
}
9599
if message == "" {
96100
message = errMsg
@@ -101,18 +105,22 @@ func Err(tb testing.TB, got error, wants ...any) {
101105
if len(wants) == 1 {
102106
// There is only one want, report it directly.
103107
tb.Error(message)
104-
return
108+
return false
105109
}
110+
106111
// There are multiple wants, report a summary.
107112
tb.Errorf("got: %T(%v); want any of: %v", got, got, wants)
113+
return false
108114
}
109115

110116
// True asserts that got is true.
111-
func True(tb testing.TB, got bool) {
117+
// Returns true if got is true, false otherwise.
118+
func True(tb testing.TB, got bool) bool {
112119
tb.Helper()
113120
if !got {
114121
tb.Error("got: false; want: true")
115122
}
123+
return got
116124
}
117125

118126
// areEqual checks if a and b are equal.

0 commit comments

Comments
 (0)