Skip to main content

Assert

This page was generated

Contributions are welcome in Psake-repo.

SYNOPSIS​

Helper function for "Design by Contract" assertion checking.

SYNTAX​

__AllParameterSets​

Assert [-ConditionToCheck] <Object> [-FailureMessage] <string> [<CommonParameters>]

DESCRIPTION​

This is a helper function that makes the code less noisy by eliminating many of the "if" statements that are normally required to verify assumptions in the code.

EXAMPLES​

EXAMPLE 1​

Assert $false "This always throws an exception"

Example of an assertion that will always fail.

EXAMPLE 2​

Assert ( ($i % 2) -eq 0 ) "$i is not an even number"

This example may throw an exception if $i is not an even number

It might be necessary to wrap the condition with parentheses to force PowerShell to evaluate the condition so that a boolean value is calculated and passed into the 'ConditionToCheck' parameter.

EXAMPLE 3​

Assert 1 -eq 2 "1 doesn't equal 2"

PowerShell will pass 1 into the ConditionToCheck variable and PS will look for a parameter called "eq" and throw an exception with the following message "A parameter cannot be found that matches parameter name 'eq'"

The solution is to wrap the condition in () so that PowerShell will evaluate it first.

Assert (1 -eq 2) "1 doesn't equal 2"

PARAMETERS​

-ConditionToCheck​

The boolean condition to evaluate

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 0
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-FailureMessage​

The error message used for the exception if the ConditionToCheck parameter is false

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 1
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters​

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS​

OUTPUTS​

NOTES​

VERSION​

This page was generated using comment-based help in Psake 5.0.4.