-
Notifications
You must be signed in to change notification settings - Fork 2
/
comparable.go
47 lines (40 loc) · 1.1 KB
/
comparable.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
package verify
import "fmt"
// FluentObj encapsulates assertions for comparable object.
type FluentObj[T comparable] struct {
FluentAny[T]
}
// Obj is used for testing a comparable object.
func Obj[T comparable](got T) FluentObj[T] {
return FluentObj[T]{FluentAny[T]{got}}
}
// Equal tests the objects using == operator.
func (x FluentObj[T]) Equal(want T) FailureMessage {
if x.Got == want {
return ""
}
return FailureMessage(fmt.Sprintf("the objects are not equal\ngot: %+v\nwant: %+v", x.Got, want))
}
// NotEqual tests the objects using != operator.
func (x FluentObj[T]) NotEqual(obj T) FailureMessage {
if x.Got != obj {
return ""
}
return "the objects are equal"
}
// Zero tests if the object is a zero value.
func (x FluentObj[T]) Zero() FailureMessage {
var want T
if want == x.Got {
return ""
}
return FailureMessage(fmt.Sprintf("not a zero value\ngot: %+v", x.Got))
}
// NonZero tests if the object is a non-zero value.
func (x FluentObj[T]) NonZero() FailureMessage {
var want T
if want != x.Got {
return ""
}
return FailureMessage(fmt.Sprintf("not a zero value\ngot: %+v", x.Got))
}