expect

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, 2021 License: MIT Imports: 4 Imported by: 0

README

expect test

A (very) simple expectation, and diff, package for use in test suites.

Prerequisites

You will need the following things properly installed on your computer.

Installation

With Go module support (Go 1.11+), simply add the following import

import "github.com/crumbandbase/expect"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, to install the expect package, run the following command:

$ go get -u github.com/crumbandbase/expect

Usage

In the simple case where two like values need to be tested for equality the expect.Equal function can be used.

package main_test

import (
  "testing"

  "github.com/crumbandbase/expect"
)

func TestGreeting(t *testing.T) {
	t.Run("succeeds when the greetings are equal", func(t *testing.T) {
		expected := "Hello, Picard"
		got := greeting("Picard")

		expect.Equal(t, got, expected)
	})

	t.Run("fails when the greeting are not equal", func(t *testing.T) {
		expected := "Bonjour, Picard"
		got := greeting("Picard")

		expect.Equal(t, got, expected)
	})
}

In the above the second test will fail and will produce a diff describing the differences. This is handled by the brilliant go-cmp package. For example the output that will be printed for this test suite is:

    string(
  -       "Hello, Picard",
  +       "Bonjour, Picard",
    )

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t *testing.T, got, expected interface{}, opts ...cmp.Option)

Equal asserts that two types are identical. If the types are not identical then their differences are printed and the current spec is marked to have failed.

func StreamEqual

func StreamEqual(t *testing.T, d decoder, expected interface{}, opts ...cmp.Option)

StreamEqual does a deep comparison of a data stream against an expected value. This function uses reflection to determine the type of the expected value in order to know how to properly decode the stream.

Types

This section is empty.

Jump to

Keyboard shortcuts

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