Elethoughts Go Assertion Library
![Build Status](https://travis-ci.org/elethoughts-code/goasserts.svg?branch=master)
Elethoughts Go Assertion library is what its name says : A simple assertion library for Go unit tests.
Installation
go get github.com/elethoughts-code/goasserts
Usage
For detailed example, you can check the unit tests from the repository. Next are some code samples.
func Test_Common_expectations(t *testing.T) {
// Given
assert := assertion.New(t)
// When / Then
assert.That(nil).IsNil()
assert.That([]string{" "}).Not().IsNil()
assert.That("123").Not().IsNil()
assert.That([]string{""}).Not().IsEmpty()
assert.That(map[int]string{0: "", 1: "", 2: ""}).HasLen(3)
}
func Test_Map_expectations(t *testing.T) {
// Given
assert := assertion.New(t)
// When / Then
assert.That(map[int]string{0: "a", 1: "b", 2: "c"}).ContainsValue("b")
assert.That(map[int]string{0: "a", 1: "b", 2: "c"}).Not().ContainsValue("d")
assert.That(map[int]string{}).Not().ContainsValue("d")
assert.That(map[int]struct{ a string }{0: {"a"}, 1: {"b"}, 2: {"c"}}).ContainsValue(struct{ a string }{"b"})
}
func Test_Http_record_transformation(t *testing.T) {
// Given
assert := assertion.New(t)
handler := func(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{Name: "my-cookie", Value: "654321", Domain: "my-domain"})
w.Header().Add("X-SOME-HEADER", "123456")
w.WriteHeader(201)
_, _ = io.WriteString(w, `["a", "b", "c"]`)
}
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
handler(w, req)
// When / Then
assert.That(w).Status().IsEq(201)
assert.That(w).Status().Not().IsEq(200)
assert.That(w).BodyToString().IsEq(`["a", "b", "c"]`)
assert.That(w).JSONBodyToSlice().IsDeepEq([]interface{}{"a", "b", "c"})
assert.That(w).Headers().HasLen(2)
assert.That(w).Cookies().HasLen(1)
assert.That(w).Header("X-SOME-HEADER").IsEq("123456")
assert.That(w).Cookie("my-cookie").Attr("Name").IsEq("my-cookie")
assert.That(w).Cookie("my-cookie").Attr("Value").IsEq("654321")
assert.That(w).Cookie("my-cookie-2").IsNil()
}
License
Elethoughts Go Assertion Library is licensed under the terms of the MIT license.