lib/t/Ay.Tests.ps1
cls cd $PSScriptRoot . ..\Ay.ps1 <# function Ay {$args} function Ay-Idx ($ay,$ele) {$ay.IndexOf($ele)} function Ay-PSCusObj ([Array]$prpVal,[string]$prpNmStr) { $o=@{}; $ny = Split-Spc $prpNmStr; $ub=ub $ny; (0..$ub) | % {$o.Add($ny.$_,$prpVal.$_)}; [PSCustomObject]$o } function Is-AllFalse ($ay){$allF=$true ;foreach($_ in $ay){if( $_){$allF=$false;break}};$allF} function Is-AllTrue ($ay){$allT=$true ;foreach($_ in $ay){if(!$_){$allT=$false;break}};$allT} function Is-SomFalse ($ay){$somF=$false;foreach($_ in $ay){if(!$_){$somF=$true ;break}};$somF} function Is-SomTrue ($ay){$somT=$false;foreach($_ in $ay){if( $_){$somT=$true ;break}};$somT} function Max-Ele ($ay){$o=$null;$ay|%{if($o -lt $_){$o=$_}};$o} function Obj-Ay ($obj,$prpNmStr) {(Split-Spc $prpNmStr)|%{$obj.$_} } function Push-NoDup ($ay,$s) {if((Is-Empty $s)-or($ay-contains $s)){$ay}else{$ay+(,$s)}} function Push-NonEmpty($ay,$s) {if(Is-Empty $s){$ay}else{$ay+(,$s)} } function Quote-Ay ($ay,$quote) {$1,$2=Brk-Quote $quote;$ay|%{$1+$_+$2} } function ReSz ([ref]$ay,$newUb) {[Array]::Resize($ay,$newUb+1);$ay.Value} function Rmv-Dup ($ay){$o=@(); $ay|foreach{if(-not($o -ccontains $_)){$o=$o+$_}}; $o} function Rmv-EmptyEle ($ay){$ay|where{Is-NonEmpty $_}} function Stringify-Ay ($ay){$ay|%{"$_"}} function Sum-Ay ($ay){[double]$o=0;$ay|%{$o+=$_};$o} function Sz ($ay){$ay.Count } function Ub ($ay) { (Sz $ay) - 1 } #> function Tst($script){It $script.toString() $script} Describe Ay { Tst {Ay 1 2 3 | Should be 1,2,3} Tst {Ay a b c | Should be a,b,c} } Describe Ay-Idx { Tst {Ay-Idx 1,2,3 1 | Should be 0 } Tst {Ay-Idx 1,2,3 2 | Should be 1 } Tst {Ay-Idx 1,2,3 3 | Should be 2 } Tst {Ay-Idx 1,2,3 4 | Should be -1} Tst {Ay-Idx 1,2,3 0 | Should be -1} Tst {Ay-Idx a,b,c a | Should be 0 } Tst {Ay-Idx a,b,c b | Should be 1 } Tst {Ay-Idx a,b,c c | Should be 2 } } Describe Rpl-QQ { Tst {Rpl-QQ '? ? ?' 1,2,3 | Should Be '1 2 3' } Tst {Rpl-QQ '? ? ' 1,2,3 | Should Be '1 2 ' } Tst {Rpl-QQ '? ? ? ?' 1,2,3 | Should Be '1 2 3 ?'} } Describe Is-AllTrue { Tst {Is-AllTrue 1,2,3 | should be $true } Tst {Is-AllTrue 1,2,3,0 | should be $false} } Describe Rmv-Ext { Tst {Rmv-Ext aa.ext | should be aa } Tst {Rmv-Ext c:\aa.ext | should be c:\aa} Tst {Rmv-Ext aa | should be aa } } Describe Macro-Ay { Tst {Macro-Ay 'aaa {aa} {bb} {aa}' | Should be aa,bb} Tst {Macro-Ay 'aaa {aa} {bb} {aa}' -inclBkt | Should be '{aa}','{bb}'} } Describe Get-Term { Tst {Get-Term 'a b c d' |Should Be (Ay a 'b c d' )} Tst {Get-Term 'a b c d' 3|Should Be (Ay a b 'c d' )} Tst {Get-Term 'a b c d' 4|Should Be (Ay a b c d )} Tst {Get-Term 'a b c d' 5|Should Be (Ay a b c d )} Tst {Get-Term 'a b c d' 6|Should Be (Ay a b c d )} Tst {Get-Term ' a b c d' |Should Be (Ay a 'b c d')} Tst {Get-Term ' a b c d' 3|Should Be (Ay a b 'c d' )} Tst {Get-Term ' a b c d' 4|Should Be (Ay a b c d )} Tst {Get-Term ' a b c d' 5|Should Be (Ay a b c d )} Tst {Get-Term ' a b c d' 6|Should Be (Ay a b c d )} } Describe Add-FnSfx { Tst {Add-FnSfx aabb.xls "(aa)" | Should be "aabb(aa).xls"} } Describe Rpl-Ext { Tst {Rpl-Ext aaa.xlsx .txt | Should be aaa.txt } Tst {Rpl-Ext aaa .txt | Should be aaa.txt } Tst {Rpl-Ext aa.bb.wrd .txt | Should be aa.bb.txt} } Describe Rmv-2DotInPth { Tst {Rmv-2DotInPth 'c:\..\a' | Should be 'a' } Tst {Rmv-2DotInPth 'c:\a\b\..\c\' | Should be 'c:\a\c' } Tst {Rmv-2DotInPth 'c:\a\b\\..\c\' | Should be 'c:\a\c' } Tst {Rmv-2DotInPth 'c:\a\b\\..\c\.' | Should be 'c:\a\c\.' } Tst {{Rmv-2DotInPth '..'} | Should throw } Tst {{Rmv-2DotInPth 'c:\..\..'} | Should throw } } Describe Mid { Tst {Mid 'aa bb cc dd' 1 2 | Should be aa } Tst {Mid 'aa bb cc dd' 1 3 | Should be 'aa ' } Tst {Mid 'aa bb cc dd' 1 100 | Should be 'aa bb cc dd'} Tst {Mid 'aa bb cc dd' 1 0 | Should be '' } } Describe Is-Empty { Tst { Is-Empty "`r`n`t " | Should Be $true } Tst { Is-Empty $null | Should Be $true } Tst { Is-Empty '' | Should Be $true } Tst { Is-Empty ' ' | Should Be $true } Tst { Is-Empty ' ' | Should Be $true } Tst { Is-Empty "`t" | Should Be $true } Tst { Is-Empty "`n" | Should Be $true } Tst { Is-Empty "`r" | Should Be $true } Tst { Is-Empty '0' | Should be $false} } Describe Rmv-EmptyEle { Tst {Rmv-EmptyEle 1,$null,2,3 | Should be (Ay 1 2 3)} Tst {Rmv-EmptyEle 1,"",2,3 | Should be (Ay 1 2 3)} Tst {Rmv-EmptyEle 1," ",2,3 | Should be (Ay 1 2 3)} } Describe Len { Tst {Len @{a=1;b=1} | Should be (Len([string]@{a=1;b=1})) <# Len <Obj> should be Len [string]<obj> #> } Tst {Len $null | Should be 0 } Tst {Len 1334 | Should be 4 } } Describe Ens-Pth { Tst { $tmp = (Tmp-Pth)+'aa\bb\cc\dd' Test-Path $tmp | Should be $false Ens-Pth $tmp Test-Path $tmp | Should be $true rd $tmp Test-Path $tmp | Should be $false } } Describe Ffn-* { Tst { Ffn-Pth "c:\aa\bb\cc.txt" | should be "c:\aa\bb\" } Tst { Ffn-Fn "c:\aa\bb\cc.txt" | should be "cc.txt" } Tst { Ffn-Ext "c:\aa\bb\cc.txt" | should be ".txt" } } Describe Tmp-Pth { $act = Tmp-Pth Tst {Test-Path(Tmp-Pth) | Should Be $true } Tst { Tmp-Pth | Should BeLike "$env:tmp\PowerShell\*" } } Describe Tmp-Fdr { Tst { $act = Tmp-Fdr abc Test-Path $act | Should be $true $act | Should BeLike 'C:\Users\user\AppData\Local\Temp\powershell\abc\T2017*' rd $act } } Describe Minus-Ay { Tst {Minus-Ay(1,2,3,4)(2,4) | Should be (Ay 1 3)} Tst {Minus-Ay(1,2,3,4)("2",4) | Should be (Ay 1 3)} } |