Skip to content

Commit

Permalink
Add unit tests for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brianbunke committed Dec 31, 2017
1 parent 006afcd commit 6ed9998
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 4 deletions.
109 changes: 109 additions & 0 deletions Tests/Add-ArrayObject.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
Remove-Module ArrayList -ErrorAction SilentlyContinue -Force
Import-Module $PSScriptRoot\..\src\ArrayList.psd1

# TODO: How to best assert actions on PSCustomObjects?

Describe 'Add-ArrayObject unit tests' -Tags 'unit' {
$ArrayList = New-ArrayList
$intList = New-ArrayList -Type int
$objList = New-ArrayList -Type PSCustomObject

# Make $0-$4 objects to populate $objList
for ($i = 0; $i -lt 5; $i++) {
[PSCustomObject]@{ID = $i} | New-Variable $i
}

It 'Adds a single item' {
Add-ArrayObject -Array $ArrayList -InputObject 0
$ArrayList | Should -Contain 0

Add-ArrayObject -Array $intList -InputObject 0
$intList | Should -Contain 0

Add-ArrayObject -Array $objList -InputObject $0
}

It 'Pipes a single item into an array' {
1 | Add-ArrayObject -Array $ArrayList
$ArrayList | Should -Contain 1

1 | Add-ArrayObject -Array $intList
$intList | Should -Contain 1

$1 | Add-ArrayObject -Array $objList
}

It 'Adds multiple items at once' {
2,3 | Add-ArrayObject -Array $ArrayList
$ArrayList | Should -Contain 2
$ArrayList | Should -Contain 3

2,3 | Add-ArrayObject -Array $intList
$intList | Should -Contain 2
$intList | Should -Contain 3

$2,$3 | Add-ArrayObject -Array $objList
}

It 'Accepts positional parameter input' {
Add-ArrayObject $ArrayList 4
$ArrayList | Should -Contain 4

Add-ArrayObject $intList 4
$intList | Should -Contain 4

Add-ArrayObject $objList $4
}

It 'Adds duplicate objects' {
Add-ArrayObject $ArrayList 4
$ArrayList | Should -Contain 4

Add-ArrayObject $intList 4
$intList | Should -Contain 4

Add-ArrayObject $objList $4
}

It 'Adds a second object type to the array' {
'test' | Add-ArrayObject -Array $ArrayList
$ArrayList | Should -Contain 'test'
}

It 'Retains all added values' {
$ArrayList | Should -Be @(0, 1, 2, 3, 4, 4, 'test')

$intList | Should -Be @(0, 1, 2, 3, 4, 4)

$objList | Should -Be @($0, $1, $2, $3, $4, $4)
}

It 'Adds nested objects to the array' {
[PSCustomObject]@{letter = 'a'; number = 1} | Add-ArrayObject $ArrayList
}

It 'Fails to add objects that do not match the list type' {
{$intList | Add-ArrayObject $1} | Should -Throw

{$objList | Add-ArrayObject 1} | Should -Throw
}

It 'Retains the expected collection type' {
$ArrayList.GetType().FullName | Should -Be 'System.Collections.ArrayList'

[string]$intList.GetType().UnderlyingSystemType |
Should -Be 'System.Collections.Generic.List[int]'

[string]$objList.GetType().UnderlyingSystemType |
Should -Be 'System.Collections.Generic.List[psobject]'
}

It 'Displays the correct object count' {
# Verify that member objects are counted, instead of just one ArrayList
$ArrayList.Count | Should -Be 8

$intList.Count | Should -Be 6

$objList.Count | Should -Be 6
}
}
34 changes: 34 additions & 0 deletions Tests/New-ArrayList.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Remove-Module ArrayList -ErrorAction SilentlyContinue -Force
Import-Module $PSScriptRoot\..\src\ArrayList.psd1

Describe 'New-ArrayList unit tests' -Tags 'unit' {
## ArrayList

$ArrayList = New-ArrayList

It 'Outputs a generic ArrayList' {
$ArrayList | Should -BeNullOrEmpty

$ArrayList.GetType().FullName | Should -Be 'System.Collections.ArrayList'
}

## List<T>

# Test int, string, and PSCustomObject types
$Types = 'int', 'string', 'PSCustomObject'

$Types | ForEach-Object {
It "Outputs a type-safe Generic.List of type [$_]" {
$List = New-ArrayList -Type $_
$ListType = [string]$List.GetType().UnderlyingSystemType

$List | Should -BeNullOrEmpty

If ($_ -eq 'PSCustomObject') {
$ListType | Should -Be "System.Collections.Generic.List[psobject]"
} Else {
$ListType | Should -Be "System.Collections.Generic.List[$_]"
}
}
} #ForEach
}
125 changes: 125 additions & 0 deletions Tests/Remove-ArrayObject.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
Remove-Module ArrayList -ErrorAction SilentlyContinue -Force
Import-Module $PSScriptRoot\..\src\ArrayList.psd1

Describe 'Remove-ArrayObject unit tests' -Tags 'unit' {
## Arrange ArrayList
$ArrayList = New-ArrayList

0..8 | Add-ArrayObject $ArrayList
'remove', 'keep' | Add-ArrayObject $ArrayList

$a1 = [PSCustomObject]@{letter = 'a'; int = 1}
$a1 | Add-ArrayObject $ArrayList

$b2 = [PSCustomObject]@{letter = 'b'; int = 2}
$c3 = [PSCustomObject]@{letter = 'c'; int = 3}
$d4 = [PSCustomObject]@{letter = 'd'; int = 4}

1..2 | ForEach-Object {
# Add all of these twice
Add-ArrayObject $ArrayList 9
Add-ArrayObject $ArrayList 'double'
Add-ArrayObject $ArrayList $b2
Add-ArrayObject $ArrayList $c3
Add-ArrayObject $ArrayList $d4
}

## Arrange List<T>
$intList = New-ArrayList -Type int

0..11 | Add-ArrayObject $intList
# Add 8-10 again to test duplicates
8..10 | Add-ArrayObject $intList

It 'Removes a single item' {
Remove-ArrayObject -Array $ArrayList -InputObject 0
$ArrayList | Should -Not -Contain 0
Remove-ArrayObject -Array $ArrayList -InputObject 'remove'
$ArrayList | Should -Not -Contain 'remove'
Remove-ArrayObject -Array $ArrayList -InputObject $a1
# TODO: How to test this?

Remove-ArrayObject -Array $intList -InputObject 0
$intList | Should -Not -Contain 0
}

It 'Removes a single item via the pipeline' {
1 | Remove-ArrayObject -Array $ArrayList
$ArrayList | Should -Not -Contain 1

1 | Remove-ArrayObject -Array $intList
$intList | Should -Not -Contain 1
}

It 'Removes multiple items at once' {
Remove-ArrayObject -Array $ArrayList -InputObject 2,3
$ArrayList | Should -Not -Contain 2
$ArrayList | Should -Not -Contain 3

Remove-ArrayObject -Array $intList -InputObject 2,3
$intList | Should -Not -Contain 2
$intList | Should -Not -Contain 3
}

It 'Removes multiple items via the pipeline' {
4,5 | Remove-ArrayObject -Array $ArrayList
$ArrayList | Should -Not -Contain 4
$ArrayList | Should -Not -Contain 5

4,5 | Remove-ArrayObject -Array $intList
$intList | Should -Not -Contain 4
$intList | Should -Not -Contain 5
}

It 'Supports expected positional parameters' {
Remove-ArrayObject $ArrayList 6
$ArrayList | Should -Not -Contain 6
7 | Remove-ArrayObject $ArrayList
$ArrayList | Should -Not -Contain 7

Remove-ArrayObject $intList 6
$intList | Should -Not -Contain 6
7 | Remove-ArrayObject $intList
$intList | Should -Not -Contain 7
}

It 'Removes duplicate simple objects' {
9 | Remove-ArrayObject $ArrayList
$ArrayList | Should -Not -Contain 9
Remove-ArrayObject $ArrayList 'double'
$ArrayList | Should -Not -Contain 'double'

8 | Remove-ArrayObject $intList
$intList | Should -Not -Contain 8
Remove-ArrayObject $intList 9
$intList | Should -Not -Contain 9
}

It 'Removes duplicate nested objects' {
# Removes both $b2 instances
Remove-ArrayObject $ArrayList $b2
# Removes both $c3 instances
$c3 | Remove-ArrayObject $ArrayList
# Removes both $d4 instances
$ArrayList | Where-Object letter -eq 'd' | Remove-ArrayObject $ArrayList
}

It 'Retains the expected collection type' {
$ArrayList.GetType().FullName | Should Be 'System.Collections.ArrayList'

[string]$intList.GetType().UnderlyingSystemType |
Should -Be 'System.Collections.Generic.List[int]'
}

It 'Retains expected objects' {
$ArrayList | Should -Contain 8
$ArrayList | Should -Contain 'keep'
# Verify that member objects are counted, instead of just one ArrayList
$ArrayList.Count | Should Be 2

$intList | Should -Contain 10
$intList | Should -Contain 11
# Verify that member objects are counted, instead of just one ArrayList
$intList.Count | Should Be 3
}
}
8 changes: 4 additions & 4 deletions src/ArrayList.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'ArrayList.psm1'

# Version number of this module.
ModuleVersion = '0.2.0'
ModuleVersion = '0.2.1'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -99,13 +99,13 @@ PrivateData = @{
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()
Tags = @('array', 'list', 'collection')

# A URL to the license for this module.
# LicenseUri = ''
LicenseUri = 'https://github.com/brianbunke/ArrayList/blob/master/LICENSE.md'

# A URL to the main website for this project.
ProjectUri = 'https://github.com/brianbunke/ArrayList'
ProjectUri = 'https://github.com/brianbunke/ArrayList'

# A URL to an icon representing this module.
# IconUri = ''
Expand Down

0 comments on commit 6ed9998

Please sign in to comment.