Skip to content

Commit 76d2364

Browse files
committed
new asserts for slice, chan & map
1 parent c4d5fe7 commit 76d2364

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

assert/assert.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,30 @@ func SNil[S ~[]T, T any](s S, a ...any) {
367367
}
368368
}
369369

370+
// CNil asserts that the channel is nil. If it is not it panics/errors
371+
// (default Asserter) the auto-generated (args appended) message.
372+
//
373+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
374+
// are used to override the auto-generated assert violation message.
375+
func CNil[C ~chan T, T any](c C, a ...any) {
376+
if c != nil {
377+
defMsg := assertionMsg + ": channel shouldn't be nil"
378+
current().reportAssertionFault(defMsg, a)
379+
}
380+
}
381+
382+
// MNil asserts that the map is nil. If it is not it panics/errors (default
383+
// Asserter) the auto-generated (args appended) message.
384+
//
385+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
386+
// are used to override the auto-generated assert violation message.
387+
func MNil[M ~map[T]U, T comparable, U any](m M, a ...any) {
388+
if m != nil {
389+
defMsg := assertionMsg + ": map should be nil"
390+
current().reportAssertionFault(defMsg, a)
391+
}
392+
}
393+
370394
// SNotNil asserts that the slice is not nil. If it is it panics/errors (default
371395
// Asserter) the auto-generated (args appended) message.
372396
//
@@ -735,6 +759,24 @@ func Empty(obj string, a ...any) {
735759
}
736760
}
737761

762+
// SEmpty asserts that the slice is empty. If it is NOT, it panics/errors
763+
// (according the current Asserter) with the auto-generated message. You can
764+
// append the generated got-want message by using optional message arguments.
765+
//
766+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
767+
// are used to override the auto-generated assert violation message.
768+
//
769+
// Note! This is reasonably fast but not as fast as [That] because of lacking
770+
// inlining for the current implementation of Go's type parametric functions.
771+
func SEmpty[S ~[]T, T any](obj S, a ...any) {
772+
l := len(obj)
773+
774+
if l != 0 {
775+
defMsg := assertionMsg + ": slice should be empty"
776+
current().reportAssertionFault(defMsg, a)
777+
}
778+
}
779+
738780
// SNotEmpty asserts that the slice is not empty. If it is, it panics/errors
739781
// (according the current Asserter) with the auto-generated message. You can
740782
// append the generated got-want message by using optional message arguments.
@@ -753,6 +795,26 @@ func SNotEmpty[S ~[]T, T any](obj S, a ...any) {
753795
}
754796
}
755797

798+
// MEmpty asserts that the map is empty. If it is NOT, it panics/errors
799+
// (according the current Asserter) with the auto-generated message. You can
800+
// append the generated got-want message by using optional message arguments.
801+
// You can append the generated got-want message by using optional message
802+
// arguments.
803+
//
804+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
805+
// are used to override the auto-generated assert violation message.
806+
//
807+
// Note! This is reasonably fast but not as fast as [That] because of lacking
808+
// inlining for the current implementation of Go's type parametric functions.
809+
func MEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
810+
l := len(obj)
811+
812+
if l != 0 {
813+
defMsg := assertionMsg + ": map should be empty"
814+
current().reportAssertionFault(defMsg, a)
815+
}
816+
}
817+
756818
// MNotEmpty asserts that the map is not empty. If it is, it panics/errors
757819
// (according the current Asserter) with the auto-generated message. You can
758820
// append the generated got-want message by using optional message arguments.

0 commit comments

Comments
 (0)