assert
The assert module provides some simple assertion methods for type checks, truthyness tests and guards invalid code paths.
When requiring laxar
, it is available as laxar.assert
.
Contents
Module Members
Types
Module Members
assert( subject, optionalDetails )
Creates and returns a new Assertion
instance for the given subject
.
Note: this function is no member of the module, but the module itself. Thus when using assert
via
laxar, assert
is will be no simple object, but this function having the other functions as
properties.
Example:
define( [ 'laxar' ], function( ax ) { ax.assert( ax.assert ).hasType( Function ); ax.assert.state( typeof ax.assert.codeIsUnreachable === 'function' ); } );
Parameters
Property | Type | Description |
---|---|---|
subject | * |
the object assertions are made for |
optionalDetails | String |
details that should be printed in case no specific details are given when calling an assertion method |
Returns
Type | Description |
---|---|
Assertion |
the assertion instance |
codeIsUnreachable( optionalDetails )
Marks a code path as erroneous by throwing an error when reached.
Parameters
Property | Type | Description |
---|---|---|
optionalDetails | String |
details to append to the error message |
state( expression, optionalDetails )
Throws an error if the given expression is falsy.
Parameters
Property | Type | Description |
---|---|---|
expression | * |
the expression to test for truthyness |
optionalDetails | String |
details to append to the error message |
Types
Assertion
Assertion#isNotNull( optionalDetails )
Throws an error if the subject is null
or undefined
.
Parameters
Property | Type | Description |
---|---|---|
optionalDetails | String |
details to append to the error message |
Returns
Type | Description |
---|---|
Assertion |
this instance |
Assertion#hasType( type, optionalDetails )
Throws an error if the subject is not of the given type. No error is thrown for null
or undefined
.
Parameters
Property | Type | Description |
---|---|---|
type | Function |
the expected type of the subject |
optionalDetails | String |
details to append to the error message |
Returns
Type | Description |
---|---|
Assertion |
this instance |
Assertion#hasProperty( property, optionalDetails )
Throws an error if the subject is no object or the given property is not defined on it.
Parameters
Property | Type | Description |
---|---|---|
property | String |
the property that is expected for the subject |
optionalDetails | String |
details to append to the error message |
Returns
Type | Description |
---|---|
Assertion |
this instance |