asserter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2019 License: MIT Imports: 4 Imported by: 0

README

Build Status codecov

asserter - Go package oneline assertions

Quick start

go get github.com/gregoryv/asserter

In your tests

func Test_something(t *testing.T) {
   assert := asserter.New(t)
   got, err := something()
   assert(err == nil).Fatal(err)
   assert(got == exp).Errorf("%v, expected %v", got, exp)
   // or the shorter version when checking got vs. expected
   assert().Equals(got, exp)
   // and with optional case message
   assert().Equals(got, exp, "with no arguments")
   
   assert().Contains(got, "text")
   assert().Contains(got, 1)


   // Check readers content 
   resp, err := http.Get("http://example.com")
   assert(err == nil).Fatal(err)
   assert().Contains(resp, "<title>")
}

Documentation

Overview

package defines AssertFunc wrapper of testing.T

Online assertions are done by wrapping the T in a test

func TestSomething(t *testing.T) {
    assert := asserter.New(t)
    got, err := Something()
    t.Logf("%v, %v := Something()", got, err)
    assert(err == nil).Fail()
    // Special case used very often is check equality
    assert().Equals(got, 1)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A interface {
	T
	Equals(got, exp interface{}) T
	Contains(body, exp interface{}) T
}

type AssertFunc

type AssertFunc func(expr ...bool) A

func New

func New(t T) AssertFunc

Assert returns an asserter for online assertions.

Example
package main

import (
	"testing"

	"github.com/gregoryv/asserter"
)

var t = &testing.T{}

func main() {
	assert := asserter.New(t)
	assert(1 != 2).Errorf("...")
	got, exp := 1, 1
	assert(got == exp).Fail()
	assert().Equals(got, exp)
}
Output:

type T

type T interface {
	Helper()
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fail()
	FailNow()
	Log(...interface{})
	Logf(string, ...interface{})
}

Jump to

Keyboard shortcuts

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