Skip to content

Instantly share code, notes, and snippets.

@Brad-Christie
Created December 28, 2018 17:34
Show Gist options
  • Save Brad-Christie/8f0f3d21c008d46046beba2b956a8f53 to your computer and use it in GitHub Desktop.
Save Brad-Christie/8f0f3d21c008d46046beba2b956a8f53 to your computer and use it in GitHub Desktop.
Test if a property exists on object
<#
.SYNOPSIS
Tests if a property exists on the provided object.
.DESCRIPTION
Often times you may want to check (esp. when dealing with JSON/XML)
if a proeprty exists on an object. By accessing a missing property
(specifically, in strict mode) you're receive back an error:
The property 'Foo' cannot be found on this object. Verify that the
property exists.
This helper is designed to avoid this.
.PARAMETER InputObject
The object to examine.
.PARAMETER PropertyName
The name of the property to test for.
#>
Function Test-Property {
[CmdletBinding()]
[OutputType([boolean])]
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[psobject]$InputObject
,
[Parameter(Position = 1, Mandatory)]
[ValidateNotNullOrEmpty()]
[Alias("Name")]
[string]$PropertyName
,
[Parameter()]
[type]$OfType = $null
)
Process {
If ($InputObject -is [System.Collections.Hashtable]) {
$result = ([System.Collections.Hashtable]$InputObject).ContainsKey($PropertyName)
} ElseIf ($InputObject -is [System.Collections.IDictionary]) {
$result = ([System.Collections.IDictionary]$InputObject).Contains($PropertyName)
} Else {
$result = $PropertyName -in ($InputObject | Get-Member -MemberType NoteProperty).Name
}
If ($result -and $OfType -ne $null) {
$result = $InputObject.$PropertyName -is $OfType
}
Return $result
}
}
. "${PSScriptRoot}\Test-Property.ps1"
Describe "Test-Property" {
Context "Basic Usage" {
BeforeAll {
$obj = @{ Foo = "Bar" }
}
It "Should accept named parameters" {
Test-Property -InputObject $obj -PropertyName "Foo" | Should -Be $true
}
It "Should accept ordered parameters" {
Test-Property $obj "Foo" | Should -Be $true
}
It "Should accept pipeline parameters" {
$obj | Test-Property -PropertyName "Foo" | Should -Be $true
}
}
Context "IDictionary" {
BeforeAll {
$obj = New-Object "System.Collections.Generic.Dictionary[string,object]"
$obj.Add("Make", "Ford")
$obj.Add("Model", "Mustang")
$obj.Add("Year", 2008)
}
It "Should have <PropertyName> [of type <OfType>]" -TestCases @(
@{ PropertyName = "Make"; OfType = [string] },
@{ PropertyName = "Model"; OfType = [string] },
@{ PropertyName = "Year"; OfType = [int] }
) {
Param([string]$PropertyName,[type]$OfType)
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
Test-Property $obj -PropertyName $PropertyName -OfType $OfType | Should -Be $true
}
It "Shouldn't have <PropertyName>" -TestCases @(
@{ PropertyName = "VIN" },
@{ PropertyName = "Mileage" }
) {
Param([string]$PropertyName)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
}
It "Should acknowledge <PropertyName> was added" -TestCases @(
@{ PropertyName = "VIN"; PropertyValue = "1HGBH41JXMN109186" },
@{ PropertyName = "Mileage"; PropertyValue = 12345 }
) {
Param([string]$PropertyName,[psobject]$PropertyValue)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
$obj.Add($PropertyName, $PropertyValue)
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
}
}
Context "HashTable" {
BeforeEach {
$obj = @{
Make = "Ford"
Model = "Mustang"
Year = 2008
}
}
It "Should have <PropertyName> [of type <OfType>]" -TestCases @(
@{ PropertyName = "Make"; OfType = [string] },
@{ PropertyName = "Model"; OfType = [string] },
@{ PropertyName = "Year"; OfType = [int] }
) {
Param([string]$PropertyName,[type]$OfType)
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
Test-Property $obj -PropertyName $PropertyName -OfType $OfType | Should -Be $true
}
It "Shouldn't have <PropertyName>" -TestCases @(
@{ PropertyName = "VIN" },
@{ PropertyName = "Mileage" }
) {
Param([string]$PropertyName)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
}
It "Should acknowledge <PropertyName> was added" -TestCases @(
@{ PropertyName = "VIN"; PropertyValue = "1HGBH41JXMN109186" },
@{ PropertyName = "Mileage"; PropertyValue = 12345 }
) {
Param([string]$PropertyName,[psobject]$PropertyValue)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
$obj.Add($PropertyName, $PropertyValue)
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
}
}
Context "PSCustomObject" {
BeforeEach {
$obj = "{`"Make`":`"Ford`",`"Model`":`"Mustang`",`"Year`":2008}" | ConvertFrom-Json
}
It "Should have <PropertyName> [of type <OfType>]" -TestCases @(
@{ PropertyName = "Make"; OfType = [string] },
@{ PropertyName = "Model"; OfType = [string] },
@{ PropertyName = "Year"; OfType = [int] }
) {
Param([string]$PropertyName,[type]$OfType)
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
Test-Property $obj -PropertyName $PropertyName -OfType $OfType | Should -Be $true
}
It "Shouldn't have <PropertyName>" -TestCases @(
@{ PropertyName = "VIN" },
@{ PropertyName = "Mileage" }
) {
Param([string]$PropertyName)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
}
It "Should acknowledge <PropertyName> was added" -TestCases @(
@{ PropertyName = "VIN"; PropertyValue = "1HGBH41JXMN109186" },
@{ PropertyName = "Mileage"; PropertyValue = 12345 }
) {
Param([string]$PropertyName,[psobject]$PropertyValue)
Test-Property $obj -PropertyName $PropertyName | Should -Be $false
$obj | Add-Member -NotePropertyName $PropertyName -NotePropertyValue $PropertyValue
Test-Property $obj -PropertyName $PropertyName | Should -Be $true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment