|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Assert keyword |
| 4 | +.DESCRIPTION |
| 5 | + Assert is a common keyword in many programming languages. |
| 6 | + |
| 7 | + In PipeScript, Asset will take a condition and an optional action. |
| 8 | +
|
| 9 | + The condtion may be contained in either parenthesis or a [ScriptBlock]. |
| 10 | +
|
| 11 | + If there is no action, the assertion will throw an exception containing the condition. |
| 12 | +
|
| 13 | + If the action is a string, the assertion will throw that error as a string. |
| 14 | +
|
| 15 | + If the action is a ScriptBlock, it will be run if the assertion is false. |
| 16 | +
|
| 17 | + Assertions will not be transpiled or included if -Verbose or -Debug has not been set. |
| 18 | +
|
| 19 | + Additionally, while running, Assertions will be ignored if -Verbose or -Debug has not been set. |
| 20 | +.EXAMPLE |
| 21 | + # With no second argument, assert will throw an error with the condition of the assertion. |
| 22 | + Invoke-PipeScript { |
| 23 | + assert (1 -eq 1) |
| 24 | + } -Debug |
| 25 | +.EXAMPLE |
| 26 | + # With a second argument of a string, assert will throw an error |
| 27 | + Invoke-PipeScript { |
| 28 | + assert ($true) "It's true" |
| 29 | + } -Debug |
| 30 | +.EXAMPLE |
| 31 | + # Conditions can also be written as a ScriptBlock |
| 32 | + Invoke-PipeScript { |
| 33 | + assert {$true} "Process id '$pid' Asserted" |
| 34 | + } -Verbose |
| 35 | +.EXAMPLE |
| 36 | + # If the assertion action was a ScriptBlock, no exception is automatically thrown |
| 37 | + Invoke-PipeScript { |
| 38 | + assert ($true) { Write-Information "Assertion was true"} |
| 39 | + } -Verbose |
| 40 | +#> |
| 41 | +[ValidateScript({ |
| 42 | + # This transpiler should run if the command is literally 'assert' |
| 43 | + $commandAst = $_ |
| 44 | + return ($commandAst -and $CommandAst.CommandElements[0].Value -eq 'assert') |
| 45 | +})] |
| 46 | +param( |
| 47 | +# The CommandAst |
| 48 | +[Parameter(Mandatory,ValueFromPipeline,ParameterSetName='CommandAst')] |
| 49 | +[Management.Automation.Language.CommandAst] |
| 50 | +$CommandAst |
| 51 | +) |
| 52 | + |
| 53 | +process { |
| 54 | + $CommandName, $CommandArgs = $commandAst.CommandElements |
| 55 | + $firstArg, $secondArg = $CommandArgs |
| 56 | + |
| 57 | + # If the first arg can be a condition in simple or complex form |
| 58 | + if (-not $firstArg -or $firstArg.GetType().Name -notin |
| 59 | + 'ParenExpressionAst', |
| 60 | + 'ScriptBlockExpressionAst', |
| 61 | + 'VariableExpressionAst', |
| 62 | + 'MemberExpressionAst', |
| 63 | + 'StringConstantExpressionAst', |
| 64 | + 'ExpandableStringExpressionAst') { |
| 65 | + # If it was the wrong type, let them know. |
| 66 | + Write-Error "Assert must be followed by one of the following expressions: |
| 67 | +* Variable |
| 68 | +* Member |
| 69 | +* String |
| 70 | +* Parenthesis |
| 71 | +* ScriptBlock |
| 72 | +" |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + # If there was a second argument, it must be a string or ScriptBlock. |
| 77 | + if ($secondArg -and $secondArg.GetType().Name -notin |
| 78 | + 'ScriptBlockExpressionAst', |
| 79 | + 'StringConstantExpressionAst', |
| 80 | + 'ExpandableStringExpressionAst') { |
| 81 | + Write-Error "Assert must be followed by a ScriptBlock or string" |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + # We need to create a [ScriptBlock] for the condition so we can transpile it. |
| 86 | + $firstArgTypeName = $firstArg.GetType().Name |
| 87 | + # The condition will always check for -DebugPreference or -VerbosePreference. |
| 88 | + $checkDebugPreference = '($debugPreference,$verbosePreference -ne ''silentlyContinue'')' |
| 89 | + |
| 90 | + $condition = |
| 91 | + [ScriptBlock]::Create("($checkDebugPreference -and $( |
| 92 | + # If the condition is already in parenthesis, |
| 93 | + if ($firstArgTypeName -eq 'ParenExpressionAst') { |
| 94 | + "$FirstArg" # leave it alone. |
| 95 | + } |
| 96 | + # If the condition is a ScriptBlockExpression, |
| 97 | + elseif ($firstArgTypeName -eq 'ScriptBlockExpressionAst') |
| 98 | + { |
| 99 | + # put it in parenthesis. |
| 100 | + "($($FirstArg -replace '^\{' -replace '\}$'))" |
| 101 | + } |
| 102 | + # Otherwise |
| 103 | + else |
| 104 | + { |
| 105 | + "($FirstArg)" # embed the condition in parenthesis. |
| 106 | + } |
| 107 | + ))") |
| 108 | + |
| 109 | + # Transpile the condition. |
| 110 | + $condition = $condition | .>Pipescript |
| 111 | + |
| 112 | + # Now we create the entire assertion script |
| 113 | + $newScript = |
| 114 | + # If there was no second argument |
| 115 | + if (-not $secondArg) { |
| 116 | + # Rethrow the condition |
| 117 | + "if $condition { throw '{$($firstArg -replace "'", "''")}' } " |
| 118 | + } elseif ($secondArg.GetType().Name -eq 'ScriptBlockExpressionAst') { |
| 119 | + # If the second argument was a script, transpile and embed it. |
| 120 | + "if $condition {$([ScriptBlock]::Create( |
| 121 | + ($secondArg -replace '^\{' -replace '\}$') |
| 122 | + ) | .>Pipescript)}" |
| 123 | + } else { |
| 124 | + # Otherwise, throw the second argument. |
| 125 | + "if $condition { throw $secondArg } " |
| 126 | + } |
| 127 | + |
| 128 | + if ($DebugPreference, $VerbosePreference -ne 'silentlyContinue') { |
| 129 | + [scriptblock]::Create($newScript) |
| 130 | + } else { |
| 131 | + {} |
| 132 | + } |
| 133 | +} |
0 commit comments