/xycond

A package supports to assert or expect many conditions in Golang

Primary LanguageGoMIT LicenseMIT

Xybor founder Go Reference GitHub Repo stars GitHub top language GitHub go.mod Go version GitHub release (release name instead of tag name) Codacy Badge Codacy Badge Go Report

Introduction

Package xycond supports to assert or expect many conditions.

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

Features

This package has the following features:

  • Assert a condition, panic in case condition is false.
  • Expect a condition to occur and perform actions on this expectation.
  • Panic with an assertion error.

Benchmark

ExpectIn

op time per op
large-map 293ns
small-map 209ns
large-array 196507ns
small-array 375ns
large-string-string 115002ns
small-string-string 455ns
large-string-rune 194ns
small-string-rune 192ns

Example

  1. Assert conditions
xycond.AssertFalse(1 == 2)

var x int
xycond.AssertZero(x)

xycond.ExpectFalse(true).Assert("this is a custom assertion message")
  1. Testing
// Test a condition with *testing.T or *testing.B.
func TestSomething(t *testing.T) {
    xycond.ExpectEmpty("").Test(t)
}
  1. Perform actions on expectation
// Perform actions on an expectation.
xycond.ExpectEqual(1, 2).
    True(func() {
        fmt.Printf("1 == 2")
    }).
    False(func() {
        fmt.Printf("1 != 2")
    })

// Output:
// 1 != 2
  1. Panic with formatted string
func foo() {
    xycond.Panicf("foo %s", "bar")
}

func bar() int {
    return xycond.Panic("buzzz").(int)
}