gospec

package module
v0.0.0-...-43f5604 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2013 License: Apache-2.0 Imports: 2 Imported by: 0

README

gospec

Spec style syntax for writing Go tests.

Documentation

Overview

Package gospec provides a spec like syntax for writing Go tests.

Use Verify() and Equals() to build simple equality tests.

    func TestUser_Fullname(t testing.T) {
        user := User.new("John", "Cinnamond")
 	    if err := gospec.Verify(user.Fullname()).Equals("John Cinnamond"); err != nil {
	  	  t.Error(err)
	    }
    }

You can also add DoesNot() to the middle of the call to check for inequality.

    func TestCount(t testing.T) {
        c = counter.New()
        c.Inc()
 	    if err := gospec.Verify(c.NextVal()).DoesNot().Equal(0); err != nil {
	  	  t.Error(err)
	    }
    }
Example
// This should return an error as 1 != 2
if err := Verify(1).Equals(2); err != nil {
	fmt.Println(err)
}

// This should return nil
if err := Verify(1).Equals(1); err != nil {
	fmt.Println(err)
}
Output:

expected 2, got 1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExpectationError

type ExpectationError struct {
	// contains filtered or unexported fields
}

An ExpectationError is returned when an expectation fails. It will yield a helpful description of the failed expectation.

func (*ExpectationError) Error

func (e *ExpectationError) Error() string

type Spec

type Spec struct {
	// contains filtered or unexported fields
}

func Verify

func Verify(actual interface{}) *Spec

Verify sets up a spec and stores the subject to test. Use this with an expectation matcher (e.g., `Equals') to check behaviour.

Example
a := 4 / 2
if err := Verify(a).Equals(2); err != nil {
	fmt.Println(err)
}
Output:

func (*Spec) DoesNot

func (spec *Spec) DoesNot() *Spec

DoesNot inverts the sense of an expectation.

Example
// This should return nil
if err := Verify(1).DoesNot().Equal(2); err != nil {
	fmt.Println(err)
}

// This should return an error as 1 = 1
if err := Verify(1).DoesNot().Equal(1); err != nil {
	fmt.Println(err)
}
Output:

expected not 1, got 1

func (*Spec) Equal

func (spec *Spec) Equal(expected interface{}) error

Alias for `Equals'.

func (*Spec) EqualWithPrecision

func (spec *Spec) EqualWithPrecision(expected float64, precision int) error

Alias for `EqualsWithPrecision'

func (*Spec) Equals

func (spec *Spec) Equals(expected interface{}) (err error)

Equals is the simplest expectation matcher, returning an error if expected value does not match the subject passed to `Verify'.

Example
if err := Verify(1).Equals(2); err != nil {
	fmt.Println(err)
}

if err := Verify(1).Equals(2.3); err != nil {
	fmt.Println(err)
}

if err := Verify("baz").Equals("bar"); err != nil {
	fmt.Println(err)
}
Output:

expected 2, got 1
expected 2.3, got 1
expected bar, got baz

func (*Spec) EqualsWithPrecision

func (spec *Spec) EqualsWithPrecision(expected float64, precision int) error

EqualsWithPrecision allows the comparison of two floats to a specified number of decimal places.

Example
// This should return an error as 1.123 != 1.124
if err := Verify(1.12311).EqualsWithPrecision(1.12411, 3); err != nil {
	fmt.Println(err)
}

// This should return an error as 2.0 != 3.0
if err := Verify(2.0).EqualsWithPrecision(3.0, 2); err != nil {
	fmt.Println(err)
}

// This should return nil
if err := Verify(1.12311).EqualsWithPrecision(1.12411, 2); err != nil {
	fmt.Println(err)
}
Output:

expected 1.124, got 1.123
expected 3.00, got 2.00
Example (Inverted)
// This should return an error as 1.123 = 1.123
if err := Verify(1.1236).DoesNot().EqualWithPrecision(1.124, 3); err != nil {
	fmt.Println(err)
}

// This should return nil
if err := Verify(1.1231).DoesNot().EqualWithPrecision(1.124, 3); err != nil {
	fmt.Println(err)
}
Output:

expected not 1.124, got 1.124

Jump to

Keyboard shortcuts

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