#Requires -Version 5.0 # Offline tests: load only the helper to avoid Tools.ps1 setup and logging. $ErrorActionPreference = 'Stop' $BooleanToolsPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'Tools.ps1' $BooleanTokens = $null $BooleanParseErrors = $null $BooleanAst = [Management.Automation.Language.Parser]::ParseFile($BooleanToolsPath, [ref]$BooleanTokens, [ref]$BooleanParseErrors) If ($BooleanParseErrors.Count) { Throw ($BooleanParseErrors | Out-String) } $BooleanDefinition = $BooleanAst.Find({ param($Node) $Node -is [Management.Automation.Language.FunctionDefinitionAst] -and $Node.Name -eq 'ConvertTo-RmmBoolean' }, $true) If (!$BooleanDefinition) { Throw 'Missing helper: ConvertTo-RmmBoolean' } . ([scriptblock]::Create($BooleanDefinition.Extent.Text)) # Tools.ps1 overrides Write-Output with a string parameter. Boolean results must # bypass that wrapper so $false remains a Boolean, without log text or side effects. Function Write-Output { Throw 'Conversion must not use the string-based logging wrapper.' } $BooleanCases = @( @{Values=@('true', '$true', 'yes', 'y', '1', $true, 1); Expected=$true} @{Values=@('false', '$false', 'no', 'n', '0', '', $null, $false, 0); Expected=$false} ) Foreach ($Case in $BooleanCases) { Foreach ($Value in $Case.Values) { $Variants = @($Value) If ($Value -is [string]) { $Variants += " `t$($Value.ToUpperInvariant())`r`n " } Foreach ($Variant in $Variants) { $Result = @(ConvertTo-RmmBoolean -Value $Variant) If ($Result.Count -ne 1 -or $Result[0] -isnot [bool] -or $Result[0] -ne $Case.Expected) { Throw "Input '$Variant' must return exactly one Boolean with value $($Case.Expected)." } } } } Write-Host 'PASS: accepted text, case, whitespace, null, native booleans, and numeric 0/1 return one Boolean.' $RmmInput = ' no ' $Result = ConvertTo-RmmBoolean $RmmInput If ($Result -isnot [bool] -or $Result -ne $false -or $RmmInput -cne ' no ') { Throw 'Positional conversion must return false without changing the original input.' } $Result = @(' yes ', 'false', '', $null, '$TRUE', '0' | ConvertTo-RmmBoolean) $Expected = @($true, $false, $false, $false, $true, $false) If ($Result.Count -ne $Expected.Count) { Throw 'Pipeline conversion must return one value for every input.' } For ($Index = 0; $Index -lt $Expected.Count; $Index++) { If ($Result[$Index] -isnot [bool] -or $Result[$Index] -ne $Expected[$Index]) { Throw "Pipeline conversion returned the wrong type or value at index $Index." } } Write-Host 'PASS: positional input, preserved caller value, and multiple pipeline inputs.' # RMM scripts may use Continue and omit -ErrorAction. Invalid input must still stop. $ErrorActionPreference = 'Continue' Foreach ($Invalid in @('maybe', 'on', 'off', '2', 'true false', '$null')) { Foreach ($Parameters in @(@{}, @{VariableName='Restart'})) { $Caught = $null $Returned = @() Try { ConvertTo-RmmBoolean -Value $Invalid @Parameters | ForEach-Object { $Returned += $_ } } Catch { $Caught = $_ } $Name = If ($Parameters.ContainsKey('VariableName')) { 'Restart' } Else { 'Value' } If ($null -eq $Caught -or !$Caught.Exception.Message.StartsWith(('Invalid ${0} value.' -f $Name))) { Throw "Invalid input '$Invalid' must throw an error identifying $Name." } If ($Returned.Count -ne 0) { Throw 'Invalid input must not return a value.' } } } Write-Host 'PASS: invalid input throws with default or caller-supplied variable name.' Write-Host "All ConvertTo-RmmBoolean tests passed on PowerShell $($PSVersionTable.PSVersion), $([IntPtr]::Size * 8)-bit."