Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active August 29, 2015 14:20
Show Gist options
  • Save nohwnd/ef4f38fbf35aaa012fab to your computer and use it in GitHub Desktop.
Save nohwnd/ef4f38fbf35aaa012fab to your computer and use it in GitHub Desktop.
Likeness concept for Pester
function Assert-ValueObjectAreAlike ($ReferenceObject, $DifferenceObject, $ExcludeProperty)
{
[string[]] $properties = ($ReferenceObject.psObject.Properties.Name + $DifferenceObject.psObject.Properties.Name ) |
sort -Unique |
where { $_ -notlike $ExcludeProperty }
-not ( Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -Property $properties )
}
#usual use case
$a= [pscustomobject]@{
a = 1
b = 2
c = 3
}
$b = [pscustomobject]@{
a = 1
b = 2
c = 3
d = 4 # not present on the $a
}
Assert-ValueObjectAreAlike -ReferenceObject $a -DifferenceObject $b #false
Assert-ValueObjectAreAlike -ReferenceObject $a -DifferenceObject $b -ExcludeProperty 'd' #true
#not so usual use case
$ps = ps | select -First 2
Assert-ValueObjectAreAlike -ReferenceObject $ps[0] -DifferenceObject $ps[1] #writes few errors then returns false
$b = [pscustomobject]@{
a = 1
b = 2
c = 3
d = 4
}
$d = [pscustomobject]@{
a = 1
b = 2
c = 3
d = 5
}
$a= [pscustomobject]@{
a = 1
b = $b
}
$c = [pscustomobject]@{
a=1
b=$d
}
Assert-ValueObjectAreAlike $a $c
#result working with reference types makes this very complex problem,
#psobjects references (in properties) are considered equal by the compare-object
#related sources
#https://github.com/AutoFixture/AutoFixture/issues/48
#http://www.nuget.org/packages/DeepEqual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment