contract

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package contract provides support for runtime "Programming by Contract" checks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Conditions

func Conditions(result bool, results ...bool)

Conditions checks the expression(s) given to see that they are all true. If any fail a suitable error message is logged and the program is halted if os.Exit(1).

This method should typically not be called near the start or end of a method, however that is not a requirement. (In fact Preconditions, Conditions, and Postconditions all do exactly the same thing except for a slight difference in the message that is logged. The three methods are provided instead of one simply as a means of documenting their intent.)

func Postconditions

func Postconditions(result bool, results ...bool)

Postconditions checks the expression(s) given to see that they are all true. If any fail a suitable error message is logged and the program is halted if os.Exit(1).

This method should typically be called near the end of a method, possibly right after the parameter checking. However, that is not a requirement. (In fact Preconditions, Conditions, and Postconditions all do exactly the same thing except for a slight difference in the message that is logged. The three methods are provided instead of one simply as a means of documenting their intent.)

Typically this is called to check the post-conditions of a successful execution of the function. As such it is generally not run when errors cause an early exit. However, if you wish it to run in such cases, you can add defer before the call.

func Preconditions

func Preconditions(result bool, results ...bool)

Preconditions checks the expression(s) given to see that they are all true. If any fail a suitable error message is logged and the program is halted if os.Exit(1).

This method should typically be called near the start of a method, possibly right after the parameter checking. However, that is not a requirement. (In fact Preconditions, Conditions, and Postconditions all do exactly the same thing except for a slight difference in the message that is logged. The three methods are provided instead of one simply as a means of documenting their intent.)

Types

type Invariant

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

Invariant marks that a given item must not change within the method.

func NewInvariant

func NewInvariant(obj interface{}) Invariant

NewInvariant constructs an Invariant wrapping a given object. Note that obj needs to be a pointer to a struct. This code does not work with types such as integers or strings as they end up being copied hence any changes are not seen.

func (*Invariant) Check

func (inv *Invariant) Check()

Checks that the invariant has not changed. If it has changed this will log an error message and halt the program via os.Exit(1). (I.e. It acts like a failed condition.)

Jump to

Keyboard shortcuts

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