.InvariantsCoreinvariant

function defined in module InvariantsCore


			invariant(fn, title; kwargs...)

Create an invariant with name title that checks an input against fn which returns either nothing when the invariant is satisfied, or an error message when the invariant is violated.


			invariant(inv; title=title(inv), kwargs...)

Wrap an invariant inv , replacing some of its attributes.


			invariant(title, invariants, all; kwargs...)
invariant(title, invariants, any; kwargs...)

Combine multiple invariants invs logically. The third argument defines how they are combined. If it is all , the resulting invariant requires all invariants to pass, while a value of any results in an invariant where only one of the invariants has to be satisfied.

Keyword arguments

Every method additionally accepts the following keyword arguments:

  • description::String = "" : A description of the invariant that gives explanation and points to related resources.

  • inputfn = identity : A function that is applied to an input before checking. Useful when composing multiple invariants.

Examples

Basic usage:


			
			
			
			using
			
			
			 
			
	
		
			Invariants
			
			

			
			

			
			
			inv
			
			 
			
			=
			
			 
			
			
			
	
			invariant
			
			(
			
			"
			
			Is negative
			
			"
			
			)
			
			 
			
			do
			
			
			
			 
			
			n
			
			
			
    
			
			
			
			n
			
			 
			
			<
			
			 
			
			0
			
			 
			
			?
			
			 
			
			nothing
			
			 
			
			:
			
			 
			
			"
			
			`n` is not negative!
			
			"
			
			

			
			end

			Invariant("Is negative")

Successful check:


			
			
			
	
		
			check
			
			(
			
			inv
			
			,
			
			 
			
			-1
			
			)

			✔ Invariant satisfied: Is negative

Failing check:


			
			
			
	
		
			check
			
			(
			
			inv
			
			,
			
			 
			
			1
			
			)

			⨯ Invariant not satisfied: Is negative

`n` is not negative!

Or just get a Bool:


			
			
			
			
	
		
			check
			
			(
			
			Bool
			
			,
			
			 
			
			inv
			
			,
			
			 
			
			-1
			
			)
			
			,
			
			 
			
			
	
		
			check
			
			(
			
			Bool
			
			,
			
			 
			
			inv
			
			,
			
			 
			
			1
			
			)

			(true, false)

Throw an error when an invariant is not satisfied:


			
			
			
	
		
			check_throw
			
			(
			
			inv
			
			,
			
			 
			
			1
			
			)

			LoadError("string", 1, InvariantsCore.InvariantException{InvariantsCore.Invariant, String}(Invariant("Is negative")
, "`n` is not negative!"))
Methods

There are 6 methods for invariant:

invariant(inv::InvariantsCore.Invariant; title, inputfn, description, format)
highlevel.jl:111
invariant(inv::InvariantsCore.AbstractInvariant; title, inputfn, description, format)
highlevel.jl:99
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}; kwargs...)
highlevel.jl:84
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}, ::typeof(all; kwargs...)
highlevel.jl:87
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}, ::typeof(any; kwargs...)
highlevel.jl:91
invariant(fn, title::String; kwargs...)
highlevel.jl:79