xycond

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2022 License: MIT Imports: 4 Imported by: 0

README

Introduction

Xycond supports to check many types of condition and panic if the condition fails.

It makes source code to be shorter and more readable by using inline assertion commands.

Features

This package has only one struct, Condition, a type alias of bool.

The package defines the following methods for this struct:

// Panic without message if the Condition fails.
func (Condition) JustAssert()

// Panic with a formatted messsage if the Condition fails.
func (Condition) Assert(msg string, a ...any)
// Test will call t.Error if condition is false.
func (c Condition) Test(t tester, args ...any)

// Testf will call t.Errorf if condition is false.
func (c Condition) Testf(t tester, format string, args ...any)

There are many functions to create Condition instances. Example:

// Check condition directly.
func MustTrue(bool) Condition
func MustFalse(bool) Condition

// Assert a number to be zero.
func MustZero(number) Condition

// Assert an object to be nil.
func MustNil(any) Condition

// Assert a slice or array to contain the element.
func MustContainA(a any, e any) Condition

// Assert a map to contain the key.
func MustContainM(m map, k any) Condition

// Assert a snippet of code to cause a panic.
func MustPanic(f func()) Condition

Besides, Xycond also has two functions to panic without using a Condition, they are Panic and JustPanic.

Visit pkg.go.dev for more details.

Example

// Assert 1 == 2
xycond.MustFalse(1 == 2).Assert("weird")

// Assert x is 0
var x int
xycond.MustZero(x).Assert("%d is not initialized with zero", x)

// Assert string is empty, panic without any message if the condition fails.
xycond.MustEmpty("").JustAssert()

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func JustPanic

func JustPanic()

JustPanic panics immediately.

func Panic

func Panic(msg string, a ...any)

Panic panics with a formatted string.

Types

type Condition

type Condition bool

Condition is a type of bool which later you must call JustAssert or Assert. If the condition is false, the program will be panicked.

Example
package main

import (
	"github.com/xybor/xyplatform/xycond"
)

func main() {
	// Assert 1 == 2
	xycond.MustFalse(1 == 2).Assert("weird")

	// Assert x is 0
	var x int
	xycond.MustZero(x).Assert("%d is not initialized with zero", x)

	// Assert string is empty, panic without any message if the condition fails.
	xycond.MustEmpty("").JustAssert()
}
Output:

func MustBe added in v0.0.2

func MustBe(v any, kinds ...reflect.Kind) Condition

MustBe returns true if value belongs to one of basic types.

func MustBeElemType added in v0.0.2

func MustBeElemType(a any) Condition

MustBeElemType returns true if a is LengthType or Pointer.

func MustBeLenghtType added in v0.0.2

func MustBeLenghtType(a any) Condition

MustBeLengthType returns true if a is a string, slice, array, or chan.

func MustContainA added in v0.0.2

func MustContainA(a any, e any) Condition

MustContainA returns true if array or slice contains the element.

func MustContainM added in v0.0.2

func MustContainM[kt comparable, vt any](m map[kt]vt, k kt) Condition

MustContainM returns true if map contains the key.

func MustEmpty added in v0.0.2

func MustEmpty(a any) Condition

MustEmpty returns true if a is an empty string, slice, array, or channel.

func MustEqual added in v0.0.2

func MustEqual(a, b any) Condition

MustEqual returns true if two values are the same.

func MustFalse added in v0.0.2

func MustFalse(b bool) Condition

MustFalse checks if b is false.

func MustNil added in v0.0.2

func MustNil(a any) Condition

MustNil returns true if a is nil.

func MustNotBe added in v0.0.2

func MustNotBe(v any, kinds ...reflect.Kind) Condition

MustNotBe returns true if value doesn't belong to all of basic types.

func MustNotContainA added in v0.0.2

func MustNotContainA(a any, e any) Condition

MustNotContainA returns true if array doesn't contains the element.

func MustNotContainM added in v0.0.2

func MustNotContainM[kt comparable, vt any](m map[kt]vt, k kt) Condition

MustNotContainM returns true if map doesn't contain the key.

func MustNotEmpty added in v0.0.2

func MustNotEmpty(a any) Condition

MustNotEmpty returns true if a is a not empty string, slice, array, or channel.

func MustNotEqual added in v0.0.2

func MustNotEqual(a, b any) Condition

MustNotEqual returns true if two values are not the same.

func MustNotNil added in v0.0.2

func MustNotNil(a any) Condition

MustNotNil returns true if a is not nil.

func MustNotPanic added in v0.0.2

func MustNotPanic(f func()) (c Condition)

func MustNotZero added in v0.0.2

func MustNotZero[T number](a T) Condition

MustNotZero returns true if a is not zero. MustNotZero only accepts number parameter.

func MustPanic added in v0.0.2

func MustPanic(f func()) (c Condition)

func MustReadableChan added in v0.0.2

func MustReadableChan(c any) Condition

MustReadableChan returns true if channel is readable.

func MustSameType added in v0.0.2

func MustSameType(v ...any) Condition

MustSameType returns true if values are the same type.

func MustTrue added in v0.0.2

func MustTrue(b bool) Condition

MustTrue checks if b is true.

func MustWritableChan added in v0.0.2

func MustWritableChan(c any) Condition

MustWritableChan returns true if channel is writable.

func MustZero added in v0.0.2

func MustZero[T number](a T) Condition

MustZero returns true if a is zero. MustZero only accepts number parameter.

func (Condition) Assert

func (c Condition) Assert(format string, args ...any)

Assert prints a formatted message and panics the program if the condition fails.

func (Condition) JustAssert

func (c Condition) JustAssert()

JustAssert panics the program without a message if condition fails.

func (Condition) Test added in v0.0.2

func (c Condition) Test(t tester, args ...any)

Test will call t.Error if condition is false.

func (Condition) Testf added in v0.0.2

func (c Condition) Testf(t tester, format string, args ...any)

Testf will call t.Errorf if condition is false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL