Skip to content

viniciusmarson/go-expect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

license

Golang testing

When you use go-expect, you write beautiful assertions as if you were writing a text. When you write assertions in this way, you don't need to remember the order of actual and expected arguments to functions like assert.equal, which helps you write better tests.

Installing:

$ go get github.com/viniciusmarson/go-expect/expect

 

Example:

package main

import (
  "testing"
  "github.com/viniciusmarson/go-expect/expect"
)

func TestToInclude(t *testing.T) {
   expect := expect.New(t)
   expect([]int{10,9,8}).ToInclude(10)
}

func TestBubbleSort(t *testing.T) {
	expect := expect.New(t)
	theExpectedResponse := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	response := []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
	BubbleSort(response)
	expect(response).ToBe(theExpectedResponse)
}

 

Features:

ToExist

expect(value).ToExist()

Asserts the given value is not nil.

expect("something truthy").ToExist()

 

ToNotExist

expect(value).ToNotExist()

Asserts the given value is nil.

expect(nil).ToNotExist()

 

ToBe

expect(value).ToBe(expectedValue)

Asserts that value is strictly equal to expectedValue.

expect([]int{ 1, 2, 3, 4 }).ToBe([]int{ 1, 2, 3, 4 })

expect("test").ToBe("test")

expect(11).ToBe(11)

 

ToNotBe

expect(value).ToNotBe(expectedValue)

Asserts that value is not strictly equal to expectedValue.

expect([]int{ 1, 2, 3, 4 }).ToNotBe([]int{ 4, 3, 2, 1 })

expect("test").ToNotBe("tset")

expect(10).ToNotBe(11)

 

ToBeAn and ToBeA

expect(value).ToBeAn(expectedType)

Asserts that value is of the type expectedType.

expect("test").ToBeA("string")

expect(true).ToBeA("bool")

expect(10).ToBeAn("int")

expect([]int{}).ToBeA("slice")

expect(`some interface{}`).ToBeAn("interface")

expect(`some struct`).ToBeA("struct")

 

ToBeTrue

expect(bool).ToBeTrue()

Asserts that bool is true.

expect(true).ToBeTrue()

 

ToBeFalse

expect(bool).ToBeFalse()

Asserts that bool is false.

expect(false).ToBeFalse()

 

Contains

expect(string).Contains(expectedString)

Asserts that string contains expectedString.

expect("banana").Contains("nana")

 

NotContains

expect(string).NotContains(notExpectedString)

Asserts that string not contains notExpectedString.

expect("banana").NotContains("haha")

 

ToBeLessThan

expect(number).ToBeLessThan(value)

Asserts the given number is less than value.

expect(2).ToBeLessThan(3)

 

ToBeLessThanOrEqualTo

expect(number).ToBeLessThanOrEqualTo(value)

Asserts the given number is less than or equal to value.

expect(2).ToBeLessThanOrEqualTo(3)

 

ToBeGreaterThan

expect(number).ToBeGreaterThan(valu)

Asserts the given number is greater than value.

expect(3).ToBeGreaterThan(2)

 

ToBeGreaterThanOrEqualTo

expect(number).ToBeGreaterThanOrEqualTo(value)

Asserts the given number is greater than or equal to value.

expect(3).ToBeGreaterThanOrEqualTo(2)

 

ToInclude

expect(slice).ToInclude(value)

Asserts the given slice contains the value.

expect([]int{ 10, 9 , 8 }).ToInclude(9)

 

ToExclude

expect(slice).ToExclude(value)

Asserts the given slice not contains the value.

expect([]int{ 10, 9 , 8 }).ToExclude(2)

 

Fail

expect().Fail()

Explicitly forces failure.

expect().Fail()
expect().Fail("Custom message")

 

TODO

Go