MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Compare-Object" -Tags "CI" {4 BeforeAll {5 $nl = [Environment]::NewLine6 7 $content1 = "line 1" + $nl + "line 2"8 $content2 = "line 1" + $nl + "line 2.1"9 $content3 = "line 1" + $nl + "line 2" + $nl + "line 3"10 $content4 = "line 1" + $nl + "line 2.1" + $nl + "Line 3"11 12 $file1 = Join-Path -Path $TestDrive -ChildPath "test1.txt"13 $file2 = Join-Path -Path $TestDrive -ChildPath "test2.txt"14 $file3 = Join-Path -Path $TestDrive -ChildPath "test3.txt"15 $file4 = Join-Path -Path $TestDrive -ChildPath "test4.txt"16 17 $null = New-Item $file1 -ItemType file -Value $content1 -Force18 $null = New-Item $file2 -ItemType file -Value $content2 -Force19 $null = New-Item $file3 -ItemType file -Value $content3 -Force20 $null = New-Item $file4 -ItemType file -Value $content4 -Force21 }22 23 It "Should be able to compare the same object using the referenceObject and differenceObject switches" {24 { Compare-Object -ReferenceObject $(Get-Content $file1) -DifferenceObject $(Get-Content $file2) } | Should -Not -Throw25 }26 27 It "Should not throw when referenceobject switch is not used" {28 { Compare-Object $(Get-Content $file1) -DifferenceObject $(Get-Content $file2) } | Should -Not -Throw29 }30 31 It "Should not throw when differenceobject switch is not used" {32 { Compare-Object -ReferenceObject $(Get-Content $file1) $(Get-Content $file2) } | Should -Not -Throw33 }34 35 It "Should indicate data that exists only in the reference dataset" {36 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)37 38 $actualOutput[1].SideIndicator | Should -BeExactly "<="39 }40 41 It "Should indicate data that exists only in the difference dataset" {42 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)43 44 $actualOutput[1].SideIndicator | Should -BeExactly "<="45 }46 47 It "Should indicate data that exists in both datasets when the includeEqual switch is used" {48 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -IncludeEqual49 50 $actualOutput.Length | Should -Be 451 $actualOutput[0].SideIndicator | Should -BeExactly "=="52 $actualOutput[1].SideIndicator | Should -BeExactly "=="53 }54 55 It "Should be able to use the casesensitive switch" {56 { Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -CaseSensitive } | Should -Not -Throw57 }58 59 It "Should correctly indicate that different cases are different when the casesensitive switch is used" {60 $caOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -CaseSensitive61 $ncaOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4)62 63 $caOutput.Length | Should -Be 464 65 $ncaOutput[1].SideIndicator | Should -Not -Be $caOutput[1].SideIndicator66 $ncaOutput[2].SideIndicator | Should -Not -Be $caOutput[2].SideIndicator67 $ncaOutput[3].SideIndicator | Should -Not -Be $caOutput[3].SideIndicator68 69 }70 71 It "Should throw when reference set is null" {72 { Compare-Object -ReferenceObject $anonexistentvariable -DifferenceObject $(Get-Content $file4) } | Should -Throw73 }74 75 It "Should throw when difference set is null" {76 { Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $anonexistentvariable } | Should -Throw77 }78 79 It "Should only display equal lines when excludeDifferent switch is used without the includeequal switch" {80 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -ExcludeDifferent81 82 $actualOutput.Length | Should -Be 283 }84 85 It "Should only display equal lines when excludeDifferent switch is used alongside the includeequal switch" {86 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -IncludeEqual -ExcludeDifferent87 88 $actualOutput.Length | Should -Be 289 }90 91 It "Should give a 0 array when using excludedifferent switch when also setting the includeequal switch to false" {92 $actualOutput = Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -ExcludeDifferent -IncludeEqual:$false93 94 $actualOutput.Length | Should -Be 095 }96 97 It "Should be able to pass objects to pipeline using the passthru switch" {98 { Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -PassThru | Format-Wide } | Should -Not -Throw99 }100 101 It "Should be able to specify the property of two objects to compare" {102 $actualOutput = Compare-Object -ReferenceObject $file3 -DifferenceObject $TestDrive -Property Length103 $actualOutput[0].Length | Should -BeNullOrEmpty104 $actualOutput[1].Length | Should -BeGreaterThan 0105 $actualOutput[0].Length | Should -Not -Be $actualOutput[1].Length106 }107 108 It "Should be able to specify the syncwindow without error" {109 { Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -SyncWindow 5 } | Should -Not -Throw110 { Compare-Object -ReferenceObject $(Get-Content $file3) -DifferenceObject $(Get-Content $file4) -SyncWindow 8 } | Should -Not -Throw111 }112 113 It "Should have the expected output when changing the syncwindow" {114 $var1 = 1..15115 $var2 = 15..1116 117 $actualOutput = Compare-Object -ReferenceObject $var1 -DifferenceObject $var2 -SyncWindow 6118 119 $actualOutput[0].InputObject | Should -Be 15120 $actualOutput[1].InputObject | Should -Be 1121 $actualOutput[2].InputObject | Should -Be 1122 $actualOutput[3].InputObject | Should -Be 15123 124 $actualOutput[0].SideIndicator | Should -BeExactly "=>"125 $actualOutput[1].SideIndicator | Should -BeExactly "<="126 $actualOutput[2].SideIndicator | Should -BeExactly "=>"127 $actualOutput[3].SideIndicator | Should -BeExactly "<="128 }129}130 131Describe "Compare-Object DRT basic functionality" -Tags "CI" {132 if(-not ([System.Management.Automation.PSTypeName]'Employee').Type)133 {134 Add-Type -TypeDefinition @"135 public class Employee136 {137 public Employee(){}138 public Employee(string firstName, string lastName, int yearsInMS)139 {140 FirstName = firstName;141 LastName = lastName;142 YearsInMS = yearsInMS;143 }144 public string FirstName;145 public string LastName;146 public int YearsInMS;147 }148 public class EmployeeComparable : Employee, System.IComparable149 {150 public EmployeeComparable(151 string firstName, string lastName, int yearsInMS)152 : base(firstName, lastName, yearsInMS)153 {}154 155 public int CompareTo(object obj)156 {157 EmployeeComparable ec = obj as EmployeeComparable;158 if (null == ec)159 return -1;160 if (FirstName != ec.FirstName)161 return -1;162 if (LastName != ec.LastName)163 return -1;164 if (YearsInMS != ec.YearsInMS)165 return -1;166 return 0;167 }168 }169 170 public class EmployeeDefinesSideIndicator : EmployeeComparable171 {172 public EmployeeDefinesSideIndicator(173 string firstName, string lastName, int yearsInMS)174 : base(firstName, lastName, yearsInMS)175 {}176 177 public string SideIndicator178 {179 get { throw new System.ArgumentException("get_SideIndicator"); }180 set { throw new System.ArgumentException("get_SideIndicator"); }181 }182 }183"@184 }185 else186 {187 Add-Type -TypeDefinition @"188 public class EmployeeComparable : Employee, System.IComparable189 {190 public EmployeeComparable(191 string firstName, string lastName, int yearsInMS)192 : base(firstName, lastName, yearsInMS)193 {}194 195 public int CompareTo(object obj)196 {197 EmployeeComparable ec = obj as EmployeeComparable;198 if (null == ec)199 return -1;200 if (FirstName != ec.FirstName)201 return -1;202 if (LastName != ec.LastName)203 return -1;204 if (YearsInMS != ec.YearsInMS)205 return -1;206 return 0;207 }208 }209 210 public class EmployeeDefinesSideIndicator : EmployeeComparable211 {212 public EmployeeDefinesSideIndicator(213 string firstName, string lastName, int yearsInMS)214 : base(firstName, lastName, yearsInMS)215 {}216 217 public string SideIndicator218 {219 get { throw new System.ArgumentException("get_SideIndicator"); }220 set { throw new System.ArgumentException("get_SideIndicator"); }221 }222 }223"@224 }225 It "Compare-Object with 1 referenceObject and 1 differenceObject should work"{226 $empsReference = @([EmployeeComparable]::New("john","smith",5))227 $empsDifference = @([EmployeeComparable]::New("mary","jane",5))228 $boolvalues=@($true,$false)229 foreach($recordEqual in $boolvalues)230 {231 foreach($excludeDifferent in $boolvalues)232 {233 foreach($passthru in $boolvalues)234 {235 $result = Compare-Object -ReferenceObject $empsReference -IncludeEqual:$recordEqual -ExcludeDifferent:$excludeDifferent -PassThru:$passthru -DifferenceObject $empsDifference236 237 if(!$excludeDifferent)238 {239 $result.Count | Should -Be 2240 if($passthru)241 {242 $result[0] | Should -Be $empsDifference243 $result[1] | Should -Be $empsReference244 }245 else246 {247 $result[0].InputObject | Should -Be $empsDifference248 $result[1].InputObject | Should -Be $empsReference249 $result[0].SideIndicator | Should -BeExactly "=>"250 $result[1].SideIndicator | Should -BeExactly "<="251 }252 }253 else254 {255 $result.Count | Should -Be 0256 }257 }258 }259 }260 }261 262 It "Compare-Object with 2 referenceObjects and 1 differenceObject should work"{263 $empsReference = @([EmployeeComparable]::New("john","smith",5),[EmployeeComparable]::New("mary","jane",3))264 $empsDifference = @([EmployeeComparable]::New("john","smith",5))265 $boolvalues=@($true,$false)266 foreach($recordEqual in $boolvalues)267 {268 foreach($excludeDifferent in $boolvalues)269 {270 foreach($passthru in $boolvalues)271 {272 $result = Compare-Object -ReferenceObject $empsReference -IncludeEqual:$recordEqual -ExcludeDifferent:$excludeDifferent -PassThru:$passthru -DifferenceObject $empsDifference273 if($recordEqual)274 {275 if(!$excludeDifferent)276 {277 $result.Count | Should -Be 2278 if($passthru)279 {280 $result[0] | Should -Be $empsReference[0]281 $result[1] | Should -Be $empsReference[1]282 }283 else284 {285 $result[0].InputObject | Should -Be $empsReference[0]286 $result[1].InputObject | Should -Be $empsReference[1]287 $result[0].SideIndicator | Should -BeExactly "=="288 $result[1].SideIndicator | Should -BeExactly "<="289 }290 }291 else292 {293 if($passthru)294 {295 $result | Should -Be $empsReference[0]296 }297 else298 {299 $result.InputObject | Should -Be $empsReference[0]300 $result.SideIndicator | Should -BeExactly "=="301 }302 }303 }304 else305 {306 if(!$excludeDifferent)307 {308 if($passthru)309 {310 $result | Should -Be $empsReference[1]311 }312 else313 {314 $result.InputObject | Should -Be $empsReference[1]315 $result.SideIndicator | Should -BeExactly "<="316 }317 }318 else319 {320 $result.Count | Should -Be 0321 }322 }323 }324 }325 }326 }327 328 It "Compare-Object with 0 SyncWindow should work"{329 $empsReference = @([EmployeeComparable]::New("john","smith",5))330 $empsDifference = @([EmployeeComparable]::New("john","smith",5),[EmployeeComparable]::New("mary","jane",3))331 $boolvalues=@($true,$false)332 foreach($recordEqual in $boolvalues)333 {334 foreach($excludeDifferent in $boolvalues)335 {336 foreach($passthru in $boolvalues)337 {338 $result = Compare-Object -ReferenceObject $empsReference -IncludeEqual:$recordEqual -ExcludeDifferent:$excludeDifferent -PassThru:$passthru -DifferenceObject $empsDifference -SyncWindow:0339 if($recordEqual)340 {341 if(!$excludeDifferent)342 {343 $result.Count | Should -Be 2344 if($passthru)345 {346 $result[0] | Should -Be $empsReference347 $result[1] | Should -Be $empsDifference[1]348 }349 else350 {351 $result[0].InputObject | Should -Be $empsReference352 $result[1].InputObject | Should -Be $empsDifference[1]353 $result[0].SideIndicator | Should -BeExactly "=="354 $result[1].SideIndicator | Should -BeExactly "=>"355 }356 }357 else358 {359 if($passthru)360 {361 $result | Should -Be $empsReference362 }363 else364 {365 $result.InputObject | Should -Be $empsReference366 $result.SideIndicator | Should -BeExactly "=="367 }368 }369 }370 else371 {372 if(!$excludeDifferent)373 {374 if($passthru)375 {376 $result | Should -Be $empsDifference[1]377 }378 else379 {380 $result.InputObject | Should -Be $empsDifference[1]381 $result.SideIndicator | Should -BeExactly "=>"382 }383 }384 else385 {386 $result.Count | Should -Be 0387 }388 }389 }390 }391 }392 }393 394 It "Compare-Object with SyncWindow should work"{395 $empsReference = @([EmployeeComparable]::New("mary","jane",3),[EmployeeComparable]::New("john","smith",5),[EmployeeComparable]::New("jack", "black", 15),[EmployeeComparable]::New("jim", "bob", 1))396 $empsDifference = @([EmployeeComparable]::New("jack", "black", 15),[EmployeeComparable]::New("jim", "bob", 1),[EmployeeComparable]::New("mary","jane",3),[EmployeeComparable]::New("john","smith",5))397 $boolvalues=@($true,$false)398 foreach($recordEqual in $boolvalues)399 {400 foreach($excludeDifferent in $boolvalues)401 {402 foreach($passthru in $boolvalues)403 {404 $result = Compare-Object -ReferenceObject $empsReference -IncludeEqual:$recordEqual -ExcludeDifferent:$excludeDifferent -PassThru:$passthru -DifferenceObject $empsDifference -SyncWindow:2405 406 if($recordEqual)407 {408 $result.Count | Should -Be 4409 if($passthru)410 {411 $result[0] | Should -Be $empsReference[0]412 $result[1] | Should -Be $empsReference[2]413 $result[2] | Should -Be $empsReference[1]414 $result[3] | Should -Be $empsReference[3]415 }416 else417 {418 $result[0].InputObject | Should -Be $empsReference[0]419 $result[0].SideIndicator | Should -BeExactly "=="420 $result[1].InputObject | Should -Be $empsReference[2]421 $result[1].SideIndicator | Should -BeExactly "=="422 $result[2].InputObject | Should -Be $empsReference[1]423 $result[2].SideIndicator | Should -BeExactly "=="424 $result[3].InputObject | Should -Be $empsReference[3]425 $result[3].SideIndicator | Should -BeExactly "=="426 }427 }428 else429 {430 $result.Count | Should -Be 0431 }432 }433 }434 }435 }436 437 It "Compare-Object with Script Block Property Parameter should work"{438 $a = [version]"1.2.3.4"439 $b = [version]"5.6.7.8"440 $result = Compare-Object $a $b -IncludeEqual -Property {$_.Major},{$_.Minor}441 $result[0] | Select-Object -ExpandProperty "*Major" | Should -Be 5442 $result[0] | Select-Object -ExpandProperty "*Minor" | Should -Be 6443 $result[0].SideIndicator | Should -BeExactly "=>"444 $result[1] | Select-Object -ExpandProperty "*Major" | Should -Be 1445 $result[1] | Select-Object -ExpandProperty "*Minor" | Should -Be 2446 $result[1].SideIndicator | Should -BeExactly "<="447 }448 449 It "Compare-Object with no ReferenceObject nor DifferenceObject: output nothing, no error and should work"{450 $result = Compare-Object @() @()451 $result | Should -BeNullOrEmpty452 }453 454 It "Compare-Object with no DifferenceObject should work"{455 $result = Compare-Object @() @("diffObject")456 $result.InputObject | Should -BeExactly "diffObject"457 $result.SideIndicator | Should -BeExactly "=>"458 }459 460 It "Compare-Object with no ReferenceObject should work"{461 $result = Compare-Object @("refObject") @()462 $result.InputObject | Should -BeExactly "refObject"463 $result.SideIndicator | Should -BeExactly "<="464 }465}466 