The very first PSUnit Pilot is testing the PowerShell |e| Tie-Fighter aka Get-Enumerator Cmdlet

by Klaus Graefensteiner 31. July 2009 08:09

I was working on my PSUnit PowerShell Unit Testing framework the last few weeks. This open source project has its home on Codeplex at http://www.codeplex.com/PSUnit. The first release is feature complete and I am now testing the framework by applying it to some real world scripts that I worked on in the past and some that I am currently working on. So far, I must confess,  I am quite pleased with the results. Here is the first unit test suite for PSUnit.

PSUnit Logo

Figure 1: PSUnit PowerShell Unit Testing Framework Logo

Introduction

A wile ago I wrote the Get-Enumerator Cmdlet that makes the use of any type of collection transparent. With the help of this Cmdlet an array enumerates the same way as a Hashtable does. Here are some references to the article that provides this solution and another article that describes the problem it solves:

Script under test

The script that I wrote unit test against has three switch parameters that can influence the results significantly. There are also a couple of alias definitions that need some verification. Here is the script:

   1: function Get-Enumerator 
   2: {
   3:  
   4:     [CmdletBinding()]
   5:     PARAM(
   6:     
   7:         [Switch] $Force, 
   8:         
   9:         [Switch] $Strict,
  10:         
  11:         [Switch] $PreserveString,
  12:     
  13:         [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
  14:         [Alias("Input","Hashtable")]
  15:         [Object] $Enumerable
  16:     
  17:     
  18:     )
  19:     Process{
  20:     
  21:         if( $Enumerable -is "System.Collections.Hashtable")
  22:         {
  23:             return $Enumerable.GetEnumerator()
  24:         }
  25:         
  26:         if ( $Enumerable -is "System.String" -and $PreserveString -and $Force)
  27:         {
  28:             return $Enumerable
  29:         }
  30:         
  31:         if( $Enumerable -is "System.Collections.IEnumerable" -and $Force )
  32:         {
  33:             return $Enumerable.GetEnumerator()
  34:         }
  35:         
  36:         if($Force -and -not $Strict)
  37:         {
  38:             return $Enumerable
  39:         }
  40:             
  41:         if ($Force -and $Strict)
  42:         {
  43:             throw("Pipeline input is not an IEnumerable")
  44:         }
  45:         
  46:         throw("Pipeline input is not a HashTable")
  47:     
  48:     }# End Process
  49: }
  50:  
  51: set-alias e Get-Enumerator
  52: set-alias so Sort-Object

Test script

The test script covers four different collections: Arrays, Hashtable, ArrayList and SortedList. It checks for expected exceptions and verifies that the elements of the collections are in the expected order after the execution of Get-Enumerator. The unit tests follow the AAA convention (Arrange, Act and Assert).

Here are the unit tests:

   1: CLS
   2: . PSUnit.ps1 
   3: . C:\SCHREIBTISCH\SVN\PS\DarthEnumerator\Darth-Enumerator.ps1
   4:  
   5: Function Test.GetEnumerator_ReturnsHelp_IfItIsUsedWithGet-HelpCmdlet([switch] $Category_Help)
   6: {
   7:     #Arrange
   8:     #Act
   9:     $Actual = Get-Help "Get-Enumerator" -Full | Out-String
  10:     
  11:     #Assert
  12:     Write-Debug $($Actual.GetType().FullName)
  13:     Assert-That -ActualValue $Actual -Constraint {$ActualValue.Length -gt 0}
  14: }
  15:  
  16: Function Test.GetEnumerator_TeamIsHashtable([switch] $Category_HashTable)
  17: {   #Arrange
  18:     #Act
  19:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  20:     
  21:     #Assert
  22:     Assert-That -ActualValue $Team -Constraint {$ActualValue -is "System.collections.HashTable"}
  23: }
  24:  
  25: Function Test.GetEnumerator_Sort-ObjectOnTeamReturnsAHashtable([switch] $Category_HashTable)
  26: {
  27:     #Arrange
  28:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  29:     $Actual = $Team | Sort-Object
  30:     Write-Debug $($Actual.GetType().FullName)
  31:     Assert-That -ActualValue $Actual -Constraint {$ActualValue -is "System.collections.HashTable"}
  32: }
  33:  
  34: Function Test.GetEnumerator_OnTeamReturnsAnArrayOfDictionaryEntries([switch] $Category_HashTable)
  35: {
  36:     #Arrange
  37:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  38:     
  39:     #Act
  40:     $Actual = $Team | Get-Enumerator
  41:     
  42:     #Assert
  43:     Write-Debug $($Actual.GetType().FullName)
  44:     Assert-That -ActualValue $Actual -Constraint {$ActualValue[0] -is "System.Collections.DictionaryEntry"}
  45: }
  46:  
  47: Function Test.GetEnumerator_CallingGetEnumeratorFunctionAndSortObjectOnKeyReturnsSortedObjects([switch] $Category_HashTable)
  48: {
  49:     #Arrange
  50:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  51:     
  52:     #Act
  53:     $Actual = $Team.GetEnumerator() | Sort-Object -Property "Key" | Foreach-Object{ $_.key}
  54:     
  55:     #Assert
  56:     Write-Debug $($Actual.GetType().FullName)
  57:     $ExpectedOrder = 2,4,12
  58:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
  59: }
  60:  
  61: Function Test.GetEnumerator_SortObjectOnKeyReturnsSortedObjects([switch] $Category_HashTable)
  62: {
  63:     #Arrange
  64:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  65:     
  66:     #Act
  67:     $Actual = $Team | Get-Enumerator | Sort-Object -Property "Key" | Foreach-Object{ $_.key}
  68:     
  69:     #Assert
  70:     Write-Debug $($Actual.GetType().FullName)
  71:     $ExpectedOrder = 2,4,12
  72:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
  73: }
  74:  
  75: Function Test.GetEnumerator_UsingAliasEAndSOOnKeyReturnsSortedObjects([switch] $Category_HashTable)
  76: {
  77:     #Arrange
  78:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  79:     
  80:     #Act
  81:     $Actual = $Team |e| so Key | Foreach-Object{ $_.key}
  82:     
  83:     #Assert
  84:     Write-Debug $($Actual.GetType().FullName)
  85:     $ExpectedOrder = 2,4,12
  86:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
  87: }
  88:  
  89: Function Test.GetEnumerator_UsingForceSwitchWithHashTableDoesntChangeOutcome([switch] $Category_HashTable)
  90: {
  91:     #Arrange
  92:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
  93:     
  94:     #Act
  95:     $Actual = $Team | Get-Enumerator -Force | Sort-Object -Property "Key" | Foreach-Object{ $_.key}
  96:     
  97:     #Assert
  98:     Write-Debug $($Actual.GetType().FullName)
  99:     $ExpectedOrder = 2,4,12
 100:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 101: }
 102:  
 103: Function Test.GetEnumerator_UsingForceSwitchWithHashTableInAliasNotationDoesntChangeOutcome([switch] $Category_HashTable)
 104: {   
 105:     #Arrange
 106:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
 107:     
 108:     #Act
 109:     $Actual = $Team |e -f| so Key | Foreach-Object{ $_.key}
 110:     
 111:     #Assert
 112:     Write-Debug $($Actual.GetType().FullName)
 113:     $ExpectedOrder = 2,4,12
 114:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 115: }
 116:  
 117: Function Test.GetEnumerator_UsingForceSwitchAndStrictSwitchWithHashTableDoesntChangeOutcome([switch] $Category_HashTable)
 118: {
 119:     #Arrange
 120:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
 121:     
 122:     #Act
 123:     $Actual = $Team | Get-Enumerator -Force -Strict | Sort-Object -Property "Key"| Foreach-Object{ $_.key}
 124:     
 125:     #Assert
 126:     Write-Debug $($Actual.GetType().FullName)
 127:     $ExpectedOrder = 2,4,12
 128:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 129: }
 130:  
 131: Function Test.GetEnumerator_UsingForceSwitchAndStrictSwitchInAliasNotationWithHashTableDoesntChangeOutcome([switch] $Category_HashTable)
 132: {
 133:     #Arrange
 134:     $Team = @{4 = "Joe"; 2 = "Steve"; 12 = "Tom"}
 135:     
 136:     #Act
 137:     $Actual = $Team |e -f -s| so Key| Foreach-Object{ $_.key}
 138:     
 139:     #Assert
 140:     Write-Debug $($Actual.GetType().FullName)
 141:     $ExpectedOrder = 2,4,12
 142:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 143: }
 144:  
 145: Function Test.GetEnumerator_UsingStrictSwitchAndForceSwitchWithArrayInAliasNotationThrowsRuntimeException([switch] $Category_Array, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 146: {
 147:     #Arrange
 148:     $RosterNumbers = 4,2,12
 149:     
 150:     #Act
 151:     $Actual = $RosterNumbers |e -f -s| so
 152:     
 153:     #Assert
 154: }
 155:  
 156: Function Test.GetEnumerator_NotUsingForceSwitchWithStringArrayInAliasNotationThrowsRuntimeException([switch] $Category_Array, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 157: {
 158:     #Arrange
 159:     $RosterNames = "Joe", "Steve", "Tom"
 160:     
 161:     #Act
 162:     $Actual = $RosterNames |e| so
 163:     
 164:     #Assert
 165: }
 166:  
 167: Function Test.GetEnumerator_NotUsingForceSwitchWithIntegerArrayInAliasNotationThrowsRuntimeException([switch] $Category_Array, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 168: {
 169:     #Arrange
 170:     $RosterNumbers = 4,2,12
 171:     $Actual = $RosterNumbers |e| so 
 172: }
 173:  
 174: Function Test.GetEnumerator_UsingForceSwitchWithArrayOfIntegersReturnsSortedArrayOfIntegers([switch] $Category_Array)
 175: {
 176:     #Arrange
 177:     $RosterNumbers = 4,2,12
 178:     
 179:     #Act
 180:     $Actual = $RosterNumbers | Get-Enumerator -Force | Sort-Object
 181:     
 182:     #Assert
 183:     Write-Debug $($Actual.GetType().FullName)
 184:     $ExpectedOrder = 2,4,12
 185:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 186: }
 187:  
 188: Function Test.GetEnumerator_UsingForceSwitchInAliasNotationWithArrayOfIntegersReturnsSortedArrayOfIntegers([switch] $Category_Array)
 189: {
 190:     #Arrange
 191:     $RosterNumbers = 4,2,12
 192:     
 193:     #Act
 194:     $Actual = $RosterNumbers | e -f | so
 195:     
 196:     #Assert
 197:     Write-Debug $($Actual.GetType().FullName)
 198:     $ExpectedOrder = 2,4,12
 199:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 200: }
 201:  
 202: Function Test.GetEnumerator_UsingForceSwitchWithArrayOfStringsReturnsSortedArrayOfCharacters([switch] $Category_Array)
 203: {
 204:     #Arrange
 205:     $RosterNames = "Joe", "Steve", "Tom"
 206:     
 207:     #Act
 208:     $Actual = $RosterNames | Get-Enumerator -Force | Sort-Object
 209:     
 210:     #Assert
 211:     Write-Debug $($Actual.GetType().FullName)
 212:     $ExpectedOrder = 'J', 'S', 'T', 'e', 'e', 'e', 'm', 'o', 'o', 't', 'v'
 213:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 11}
 214: }
 215:  
 216: Function Test.GetEnumerator_UsingForceSwitchInAliasNotationWithArrayOfStringsReturnsSortedArrayOfCharacters([switch] $Category_Array)
 217: {
 218:     #Arrange
 219:     $RosterNames = "Joe", "Steve", "Tom"
 220:     
 221:     #Act
 222:     $Actual = $RosterNames | e -f | so
 223:     
 224:     #Assert
 225:     Write-Debug $($Actual.GetType().FullName)
 226:     $ExpectedOrder = 'J', 'S', 'T', 'e', 'e', 'e', 'm', 'o', 'o', 't', 'v'
 227:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 11}
 228: }
 229:  
 230: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchWithArrayOfStringsReturnsSortedArrayOfStrings([switch] $Category_Array)
 231: {
 232:     #Arrange
 233:     $RosterNames = "Joe", "Steve", "Tom"
 234:     
 235:     #Act
 236:     $Actual = $RosterNames | Get-Enumerator -Force -PreserveString | Sort-Object
 237:     
 238:     #Assert
 239:     Write-Debug $($Actual.GetType().FullName)
 240:     $ExpectedOrder = "Joe", "Steve", "Tom"
 241:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 242: }
 243:  
 244: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchWithArrayOfStringsReturnsSortedArrayOfStrings([switch] $Category_Array)
 245: {
 246:     #Arrange
 247:     $RosterNames = "Joe", "Steve", "Tom"
 248:     
 249:     #Act
 250:     $Actual = $RosterNames | Get-Enumerator -Force -PreserveString -Strict | Sort-Object
 251:     
 252:     #Assert
 253:     Write-Debug $($Actual.GetType().FullName)
 254:     $ExpectedOrder = "Joe", "Steve", "Tom"
 255:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 256: }
 257:  
 258:  
 259: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchInAliasNotationWithArrayOfStringsReturnsSortedArrayOfStrings([switch] $Category_Array)
 260: {
 261:     #Arrange
 262:     $RosterNames = "Joe", "Steve", "Tom"
 263:     
 264:     #Act
 265:     $Actual = $RosterNames | e -f -p | so
 266:     
 267:     #Assert
 268:     Write-Debug $($Actual.GetType().FullName)
 269:     $ExpectedOrder = "Joe", "Steve", "Tom"
 270:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 271: }
 272:  
 273: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchInAliasNotationWithArrayOfStringsReturnsSortedArrayOfStrings([switch] $Category_Array)
 274: {
 275:     #Arrange
 276:     $RosterNames = "Joe", "Steve", "Tom"
 277:     
 278:     #Act
 279:     $Actual = $RosterNames | e -f -p -s| so
 280:     
 281:     #Assert
 282:     Write-Debug $($Actual.GetType().FullName)
 283:     $ExpectedOrder = "Joe", "Steve", "Tom"
 284:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 285: }
 286:  
 287:  
 288: Function Test.GetEnumerator_UsingForceSwitchWithArrayListOfIntegersInAliasNotationReturnsSortedArrayOfIntegers([switch] $Category_ArrayList)
 289: {
 290:     #Arrange
 291:     $RosterNumbers = 4,2,12
 292:     $RosterNumberList = New-Object -TypeName "System.Collections.ArrayList"
 293:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_)} | Out-Null
 294:     
 295:     #Act
 296:     $Actual = $RosterNumberList |e -f| so
 297:     
 298:     #Assert
 299:     Write-Debug $($Actual.GetType().FullName)
 300:     $ExpectedOrder = 2,4,12
 301:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 302: }
 303:  
 304: Function Test.GetEnumerator_NotUsingForceSwitchAndStrictSwitchWithArrayListOfIntegersInAliasNotationThrowsRuntimeException([switch] $Category_ArrayList, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 305: {
 306:     #Arrange
 307:     $RosterNumbers = 4,2,12
 308:     $RosterNumberList = New-Object -TypeName "System.Collections.ArrayList"
 309:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_)} | Out-Null
 310:     
 311:     #Act
 312:     $Actual = $RosterNumberList |e -f -s| so #ArrayList gets implicitly converted to an array (ToArray()) and treated as such
 313:     
 314:     #Assert
 315: }
 316:  
 317: Function Test.GetEnumerator_NotUsingForceSwitchWithArrayListOfIntegersInAliasNotationThrowsRuntimeException([switch] $Category_ArrayList, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 318: {
 319:     #Arrange
 320:     $RosterNumbers = 4,2,12
 321:     $RosterNumberList = New-Object -TypeName "System.Collections.ArrayList"
 322:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_)} | Out-Null
 323:     
 324:     #Act
 325:     $Actual = $RosterNumberList |e | so
 326:     
 327:     #Assert
 328: }
 329:  
 330: Function Test.GetEnumerator_NotUsingForceSwitchWithArrayListOfStringsInAliasNotationThrowsRuntimeException([switch] $Category_ArrayList, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 331: {
 332:     #Arrange
 333:     $RosterNames = "Joe", "Steve", "Tom"
 334:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 335:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 336:     
 337:     #Act
 338:     $Actual = $RosterNameList |e | so
 339:     
 340:     #Assert
 341: }
 342:  
 343:  
 344:  
 345: Function Test.GetEnumerator_UsingForceSwitchWithArrayListOfIntegersReturnsSortedArrayOfIntegers([switch] $Category_ArrayList)
 346: {
 347:     #Arrange
 348:     $RosterNumbers = 4,2,12
 349:     $RosterNumberList = New-Object -TypeName "System.Collections.ArrayList"
 350:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_)} | Out-Null
 351:     
 352:     #Act
 353:     $Actual = $RosterNumberList | Get-Enumerator -Force | Sort-Object
 354:     
 355:     #Assert
 356:     Write-Debug $($Actual.GetType().FullName)
 357:     $ExpectedOrder = 2,4,12
 358:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 359: }
 360:  
 361: Function Test.GetEnumerator_UsingForceSwitchWithArrayListOfStringsReturnsSortedArrayOfCharacters([switch] $Category_ArrayList)
 362: {
 363:     #Arrange
 364:     $RosterNames = "Joe", "Steve", "Tom"
 365:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 366:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 367:     
 368:     #Act
 369:     $Actual = $RosterNameList | Get-Enumerator -Force | Sort-Object
 370:     
 371:     #Assert
 372:     Write-Debug $($Actual.GetType().FullName)
 373:     $ExpectedOrder = 'J', 'S', 'T', 'e', 'e', 'e', 'm', 'o', 'o', 't', 'v'
 374:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 11}
 375: }
 376:  
 377: Function Test.GetEnumerator_UsingForceSwitchWithArrayListOfStringsInAliasNotationReturnsSortedArrayOfCharacters([switch] $Category_ArrayList)
 378: {
 379:     #Arrange
 380:     $RosterNames = "Joe", "Steve", "Tom"
 381:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 382:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 383:     
 384:     #Act
 385:     $Actual = $RosterNameList | e -f | so
 386:     
 387:     #Assert
 388:     Write-Debug $($Actual.GetType().FullName)
 389:     $ExpectedOrder = 'J', 'S', 'T', 'e', 'e', 'e', 'm', 'o', 'o', 't', 'v'
 390:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 11}
 391: }
 392:  
 393: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchWithArrayListOfStringsReturnsSortedArrayOfStrings([switch] $Category_ArrayList)
 394: {
 395:     #Arrange
 396:     $RosterNames = "Joe", "Steve", "Tom"
 397:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 398:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 399:     
 400:     #Act
 401:     $Actual = $RosterNameList | e -Force -PreserveString| so
 402:     
 403:     #Assert
 404:     Write-Debug $($Actual.GetType().FullName)
 405:     $ExpectedOrder = "Joe", "Steve", "Tom"
 406:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 407: }
 408:  
 409: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchWithArrayListOfStringsInAliasNotationReturnsSortedArrayOfStrings([switch] $Category_ArrayList)
 410: {
 411:     #Arrange
 412:     $RosterNames = "Joe", "Steve", "Tom"
 413:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 414:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 415:     
 416:     #Act
 417:     $Actual = $RosterNameList | e -f -p | so
 418:  
 419:     #Assert
 420:     Write-Debug $($Actual.GetType().FullName)
 421:     $ExpectedOrder = "Joe", "Steve", "Tom"
 422:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 423: }
 424:  
 425: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchWithArrayListOfStringsInAliasNotationReturnsSortedArrayOfStrings([switch] $Category_ArrayList)
 426: {
 427:     #Arrange
 428:     $RosterNames = "Joe", "Steve", "Tom"
 429:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 430:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 431:     
 432:     #Act
 433:     $Actual = $RosterNameList | e -f -p -s| so
 434:  
 435:     #Assert
 436:     Write-Debug $($Actual.GetType().FullName)
 437:     $ExpectedOrder = "Joe", "Steve", "Tom"
 438:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 439: }
 440:  
 441: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchWithArrayListOfStringsReturnsSortedArrayOfStrings([switch] $Category_ArrayList)
 442: {
 443:     #Arrange
 444:     $RosterNames = "Joe", "Steve", "Tom"
 445:     $RosterNameList = New-Object -TypeName "System.Collections.ArrayList"
 446:     $RosterNames | foreach-object {$RosterNameList.Add($_)} | Out-Null
 447:     
 448:     #Act
 449:     $Actual = $RosterNameList | e -Force -PreserveString -Strict| so
 450:  
 451:     #Assert
 452:     Write-Debug $($Actual.GetType().FullName)
 453:     $ExpectedOrder = "Joe", "Steve", "Tom"
 454:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 455: }
 456:  
 457: Function Test.GetEnumerator_UsingForceSwitchWithSortedListOfIntegersReturnsSortedListOfIntegers([switch] $Category_SortedList)
 458: {
 459:     #Arrange
 460:     $RosterNumbers = 4,2,12
 461:     $RosterNumberList = New-Object -TypeName "System.Collections.SortedList"
 462:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_, $_)} | Out-Null
 463:     
 464:     #Act
 465:     $Actual = $RosterNumberList |e -f| %{$_.Value}
 466:  
 467:     #Assert
 468:     Write-Debug $($Actual.GetType().FullName)
 469:     $ExpectedOrder = 2,4,12
 470:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 471: }
 472:  
 473: Function Test.GetEnumerator_UsingForceSwitchAndStrictSwitchWithSortedListOfIntegersReturnsSortedListOfIntegers([switch] $Category_SortedList)
 474: {
 475:     #Arrange
 476:     $RosterNumbers = 4,2,12
 477:     $RosterNumberList = New-Object -TypeName "System.Collections.SortedList"
 478:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_, $_)} | Out-Null
 479:     
 480:     #Act
 481:     $Actual = $RosterNumberList |e -f -s| %{$_.Value}
 482:  
 483:     #Assert
 484:     Write-Debug $($Actual.GetType().FullName)
 485:     $ExpectedOrder = 2,4,12
 486:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -SyncWindow 0).Count -eq 3}
 487: }
 488:  
 489: Function Test.GetEnumerator_NotUsingForceSwitchWithSortedListOfIntegersInAliasNotationThrowsRuntimeException([switch] $Category_SortedList, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 490: {
 491:     #Arrange
 492:     $RosterNumbers = 4,2,12
 493:     $RosterNumberList = New-Object -TypeName "System.Collections.SortedList"
 494:     $RosterNumbers | foreach-object {$RosterNumberList.Add($_, $_)} | Out-Null
 495:     
 496:     #Act
 497:     $Actual = $RosterNumberList |e| %{$_.Value}
 498:     
 499:     #Assert
 500:  
 501: }
 502:  
 503: Function Test.GetEnumerator_NotUsingForceSwitchWithSortedListOfStringsInAliasNotationThrowsRuntimeException([switch] $Category_SortedList, [System.Management.Automation.RuntimeException] $ExpectedException = $(New-Object -TypeName "System.Management.Automation.RuntimeException"))
 504: {
 505:     #Arrange
 506:     $RosterNames = "Joe", "Steve", "Tom"
 507:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 508:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 509:     
 510:     #Act
 511:     $Actual = $RosterNameList |e| %{$_.Value}
 512:     
 513:     #Assert
 514: }
 515:  
 516: Function Test.GetEnumerator_UsingForceSwitchWithSortedListOfStringsReturnsSortedListOfStrings([switch] $Category_SortedList)
 517: {
 518:     #Arrange
 519:     $RosterNames = "Joe", "Steve", "Tom"
 520:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 521:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 522:     
 523:     #Act
 524:     $Actual = $RosterNameList | e -f | %{$_.Value}
 525:     
 526:     #Assert
 527:     Write-Debug $($Actual.GetType().FullName)
 528:     $ExpectedOrder = "Joe", "Steve", "Tom"
 529:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 530: }
 531:  
 532: Function Test.GetEnumerator_UsingForceSwitchAndStrictSwitchWithSortedListOfStringsInAliasNotationReturnsSortedListOfStrings([switch] $Category_SortedList)
 533: {
 534:     #Arrange
 535:     $RosterNames = "Joe", "Steve", "Tom"
 536:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 537:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 538:     
 539:     #Act
 540:     $Actual = $RosterNameList | e -f -s | %{$_.Value}
 541:     
 542:     #Assert
 543:     Write-Debug $($Actual.GetType().FullName)
 544:     $ExpectedOrder = "Joe", "Steve", "Tom"
 545:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 546: }
 547:  
 548: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchWithSortedListOfStringsReturnsSortedListOfStrings([switch] $Category_SortedList)
 549: {
 550:     #Arrange
 551:     $RosterNames = "Joe", "Steve", "Tom"
 552:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 553:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 554:     
 555:     #Act
 556:     $Actual = $RosterNameList | e -Force -PreserveString| %{$_.Value}
 557:     
 558:     #Assert
 559:     Write-Debug $($Actual.GetType().FullName)
 560:     $ExpectedOrder = "Joe", "Steve", "Tom"
 561:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 562: }
 563:  
 564: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchWithSortedListOfStringsReturnsSortedListOfStrings([switch] $Category_SortedList)
 565: {
 566:     #Arrange
 567:     $RosterNames = "Joe", "Steve", "Tom"
 568:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 569:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 570:     
 571:     #Act
 572:     $Actual = $RosterNameList | e -Force -PreserveString -Strict| %{$_.Value}
 573:     
 574:     #Assert
 575:     Write-Debug $($Actual.GetType().FullName)
 576:     $ExpectedOrder = "Joe", "Steve", "Tom"
 577:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 578: }
 579:  
 580: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchWithSortedListOfStringsInAliasNotationReturnsSortedListOfStrings([switch] $Category_SortedList)
 581: {
 582:     #Arrange
 583:     $RosterNames = "Joe", "Steve", "Tom"
 584:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 585:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 586:     
 587:     #Act
 588:     $Actual = $RosterNameList | e -f -p | %{$_.Value}
 589:     
 590:     #Assert
 591:     Write-Debug $($Actual.GetType().FullName)
 592:     $ExpectedOrder = "Joe", "Steve", "Tom"
 593:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 594: }
 595:  
 596: Function Test.GetEnumerator_UsingForceSwitchAndPreserveStringSwitchAndStrictSwitchWithSortedListOfStringsInAliasNotationReturnsSortedListOfStrings([switch] $Category_SortedList)
 597: {
 598:     #Arrange
 599:     $RosterNames = "Joe", "Steve", "Tom"
 600:     $RosterNameList = New-Object -TypeName "System.Collections.SortedList"
 601:     $RosterNames | foreach-object {$RosterNameList.Add($_, $_)} | Out-Null
 602:     
 603:     #Act
 604:     $Actual = $RosterNameList | e -f -p -s| %{$_.Value}
 605:     
 606:     #Assert
 607:     Write-Debug $($Actual.GetType().FullName)
 608:     $ExpectedOrder = "Joe", "Steve", "Tom"
 609:     Assert-That -ActualValue $Actual -Constraint {(Compare-Object -ReferenceObject $ExpectedOrder -DifferenceObject $ActualValue -IncludeEqual -syncwindow 0).Count -eq 3}
 610: }

Test result

PSUnit reports test progress and results in two ways:

1.The test results are sent to the console output window

2. A HTML report gets generated at the end of each test run

HTML report

Here is a screenshot of an HTML report:

PSUnitHTMLReport

Figure 2: PSUnit HTML Report

PowerShell ISE

Here is a screen shot of the test run in the PowerShell ISE:

PSUnitTestInPowerShellISE

Figure 3: PSUnit test run in PowerShell ISE

PowerShell.exe

Here is a screen shot of the test run in PowerShell.exe:

PSUnitTestInPowerShellEXE

Figure 4: PSUnit test run in PowerShell.exe

Download

The script files can be downloaded here: TheFirstPSUnitPilot.zip

Ausblick

There are two more scripts that I need to test with PSUnit before the first release of the framework. The first shipment will be PSUnit 2.0 Beta and is scheduled for August 6th 2009. For the latest updates you can follow the PSUnit Twitter handle at: http://www.twitter.com/psunit.

Tags: , ,

PowerShell | PSUnit | Test Automation

Comments are closed

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2014, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2014 by Klaus Graefensteiner

Rendertime:
Page rendered at 10/23/2014 5:21:31 AM (PST Pacific Standard Time UTC DST -7)