Skip to content

Instantly share code, notes, and snippets.

@gaelcolas
Created August 18, 2016 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaelcolas/4f1c5b60ad497a04fab3fddd3bc02c2e to your computer and use it in GitHub Desktop.
Save gaelcolas/4f1c5b60ad497a04fab3fddd3bc02c2e to your computer and use it in GitHub Desktop.
Function Export-StructuralUnitTestFromCommand {
param(
[parameter(Mandatory=$true,ValueFromPipelineByPropertyName = $true,ValueFromPipeline = $true)]
[System.Management.Automation.CommandInfo[]]
$command
)
begin {
$stringBuilder = New-object -TypeName System.Text.StringBuilder
$BuiltInParameters = ([Management.Automation.PSCmdlet]::CommonParameters + [Management.Automation.PSCmdlet]::OptionalCommonParameters)
}
process {
foreach ($cmd in $command)
{
$null = $stringBuilder.Clear()
$cmdName = $cmd.Name
$cmdDefaultParameterSet = $cmd.DefaultParameterSet
$null = $stringBuilder.AppendLine(@"
Describe '$cmdName' {
#getting Command Metadata
`$command = (Get-Command '$cmdName')
It 'has $cmdDefaultParameterSet as Default parameterSet' {
`$command.defaultParameterSet | Should be '$cmdDefaultParameterSet'
}
"@)
$outputTypes = $cmd.OutputType
foreach ($outputType in $outputTypes)
{
$outputTypeName = $outputType.Name
#It 'Output the Type $outputType'
$null = $stringBuilder.AppendLine(@"
It 'contains an outputType of Type $outputTypeName' {
`$command.OutputType.Type -contains [$outputTypeName] | should be `$true
}
"@)
}
$parameterSets = $cmd.ParameterSets
foreach ($ParameterSet in $parameterSets)
{
$ParameterSetName = $ParameterSet.Name
$null = $stringBuilder.AppendLine(@"
Context 'ParameterSetName $ParameterSetName' {
It 'has a parameter Set of Name $ParameterSetName' {
`$command.ParameterSets.Name -contains '$ParameterSetName' | Should be $true
}
`$ParameterSet = `$command.ParameterSets | Where-Object { `$_.'Name' -eq '$ParameterSetName' }
"@)
$parameters = $ParameterSet.Parameters | Where-Object { $_.Name -notin $BuiltInParameters}
foreach ($parameter in $parameters)
{
$ParameterName = $parameter.Name
$TypeName = $parameter.ParameterType.ToString()
$isMandatory = $parameter.isMandatory
$ValueFromPipeline = $parameter.ValueFromPipeline
$ValueFromPipelineByPropertyName = $parameter.ValueFromPipelineByPropertyName
$ValueFromRemainingArguments = $parameter.ValueFromRemainingArguments
$Position = $parameter.Position
$null = $stringBuilder.AppendLine(@"
`$Parameter = `$ParameterSet.Parameters | Where-Object { `$_.'Name' -eq '$ParameterName' }
It 'has compatible parameter $ParameterName' {
`$Parameter | Should Not BeNullOrEmpty
`$Parameter.ParameterType.ToString() | Should be $TypeName
`$Parameter.IsMandatory | Should be `$$([bool]$isMandatory)
`$Parameter.ValueFromPipeline | Should be `$$([bool]$ValueFromPipeline)
`$Parameter.ValueFromPipelineByPropertyName | Should be `$$([bool]$ValueFromPipelineByPropertyName)
`$Parameter.ValueFromRemainingArguments | Should be `$$([bool]$ValueFromRemainingArguments)
`$Parameter.Position | Should be $Position
}
"@)
}
#Closing Context block
$null = $stringBuilder.AppendLine(' }')
}
#Closing Describe Statement
$null = $stringBuilder.AppendLine('}')
Write-Output $stringBuilder.ToString()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment