gospec: github.com/jcinnamond/gospec Index | Examples | Files

package gospec

import "github.com/jcinnamond/gospec"

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)
	    }
    }

Code:

// 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

Package Files

doc.go gospec.go

type ExpectationError Uses

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 Uses

func (e *ExpectationError) Error() string

type Spec Uses

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

func Verify Uses

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.

Code:

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

func (*Spec) DoesNot Uses

func (spec *Spec) DoesNot() *Spec

DoesNot inverts the sense of an expectation.

Code:

// 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 Uses

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

Alias for `Equals'.

func (*Spec) EqualWithPrecision Uses

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

Alias for `EqualsWithPrecision'

func (*Spec) Equals Uses

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'.

Code:

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 Uses

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

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

Code:

// 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

Code:

// 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

Package gospec imports 2 packages (graph). Updated 2016-07-28. Refresh now. Tools for package owners.