Skip to content

Commit

Permalink
new asserts for slice, chan & map
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Jul 29, 2024
1 parent c4d5fe7 commit 76d2364
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,30 @@ func SNil[S ~[]T, T any](s S, a ...any) {
}
}

// CNil asserts that the channel is nil. If it is not it panics/errors
// (default Asserter) the auto-generated (args appended) message.
//
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
// are used to override the auto-generated assert violation message.
func CNil[C ~chan T, T any](c C, a ...any) {
if c != nil {
defMsg := assertionMsg + ": channel shouldn't be nil"
current().reportAssertionFault(defMsg, a)
}
}

// MNil asserts that the map is nil. If it is not it panics/errors (default
// Asserter) the auto-generated (args appended) message.
//
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
// are used to override the auto-generated assert violation message.
func MNil[M ~map[T]U, T comparable, U any](m M, a ...any) {
if m != nil {
defMsg := assertionMsg + ": map should be nil"
current().reportAssertionFault(defMsg, a)
}
}

// SNotNil asserts that the slice is not nil. If it is it panics/errors (default
// Asserter) the auto-generated (args appended) message.
//
Expand Down Expand Up @@ -735,6 +759,24 @@ func Empty(obj string, a ...any) {
}
}

// SEmpty asserts that the slice is empty. If it is NOT, it panics/errors
// (according the current Asserter) with the auto-generated message. You can
// append the generated got-want message by using optional message arguments.
//
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
// are used to override the auto-generated assert violation message.
//
// Note! This is reasonably fast but not as fast as [That] because of lacking
// inlining for the current implementation of Go's type parametric functions.
func SEmpty[S ~[]T, T any](obj S, a ...any) {
l := len(obj)

if l != 0 {
defMsg := assertionMsg + ": slice should be empty"
current().reportAssertionFault(defMsg, a)
}
}

// SNotEmpty asserts that the slice is not empty. If it is, it panics/errors
// (according the current Asserter) with the auto-generated message. You can
// append the generated got-want message by using optional message arguments.
Expand All @@ -753,6 +795,26 @@ func SNotEmpty[S ~[]T, T any](obj S, a ...any) {
}
}

// MEmpty asserts that the map is empty. If it is NOT, it panics/errors
// (according the current Asserter) with the auto-generated message. You can
// append the generated got-want message by using optional message arguments.
// You can append the generated got-want message by using optional message
// arguments.
//
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
// are used to override the auto-generated assert violation message.
//
// Note! This is reasonably fast but not as fast as [That] because of lacking
// inlining for the current implementation of Go's type parametric functions.
func MEmpty[M ~map[T]U, T comparable, U any](obj M, a ...any) {
l := len(obj)

if l != 0 {
defMsg := assertionMsg + ": map should be empty"
current().reportAssertionFault(defMsg, a)
}
}

// MNotEmpty asserts that the map is not empty. If it is, it panics/errors
// (according the current Asserter) with the auto-generated message. You can
// append the generated got-want message by using optional message arguments.
Expand Down

0 comments on commit 76d2364

Please sign in to comment.