lib/Backup/Functions1.Tests.ps1
cls cd $PSScriptRoot . .\Functions1.ps1 Describe Fmt-FixWdt { $wdt = 11,8,7 $qqStr = "Brk-Spc1 ?-rev -> ? ?" $colNy = 's exp exp2' -split ' ' It 'with sqQuote' { Fmt-FixWdt $wdt $colNy $qqStr -sqQuote | Should be "Brk-Spc1 [s] -rev -> [exp] [exp2] " } It 'no sqQuote' { # 12345678901 12345678 1234567 Fmt-FixWdt $wdt $colNy $qqStr | Should be "Brk-Spc1 s -rev -> exp exp2 " } } Describe Get-FixWdtFmtStr { function Tst($wdt,$qqStr,$exp) { It "Should Pass" { Get-FixWdtFmtStr $wdt $qqStr | Should be $exp } } Tst 1,2,3 $null '{0,-1}{1,-2}{2,-3}' Tst 1,2,3 '? -> ? | ?' '{0,-1} -> {1,-2} | {2,-3}' } Describe Repl-QQ { $qqStr = "? ? -> ?" $wdt = 20,20,30 It (Fmt-FixWdt $wdt s,ay,exp $qqStr) {} function Tst($s,$ay,$exp) { $a = Fmt-FixWdt $wdt ($s,"$ay",$exp) $qqStr; It $a { Repl-QQ $s $ay | Should be $exp } } Tst "? ? ?" 1,2,3 "1 2 3" Tst "? ?" 1,2,3 "1 2" Tst "? ?" 1,2,3 "1 2" Tst "? ?" 1,2,3 "1 2" } Describe Get-FstOrAll { $qqStr = "? ? -> ?" $wdt = 15,8,30 function Tst($s,$sep,$exp) { $a = Fmt-FixWdt $wdt ($s,$sep,$exp) $qqStr -sqQuote It $a { Get-FstOrAll $s $sep | Should be $exp } } It (Fmt-FixWdt $wdt s,sep,exp $qqStr) {} Tst "aaa --- bb" "---" "aaa" Tst " aa bb " "---" "aa bb" Tst " aa bb " $null "" } Describe 'Is-AllTrue' { $qqStr = "Is-AllTrue ? -> ?" $wdt = 10,10 function Tst($ay,$exp) { $dta = "$ay",$exp; $a = Fmt-FixWdt $wdt $dta $qqStr; It $a { Is-AllTrue $ay | Should be $exp } } It (Fmt-FixWdt $wdt ay,exp $qqStr) {} Tst 1,2,3 $true Tst 1,2,0 $false } Describe Rmv-Ext { $qqStr = "Rmv-Ext ? -> ?" $wdt = 20,20 function Tst($ffn,$exp) { $dta = $ffn,$exp; It (Fmt-FixWdt $wdt $dta $qqStr) { Rmv-Ext $ffn | Should be $exp } } It (Fmt-FixWdt $wdt `$ffn,`$exp $qqStr) {} Tst "aa.ext" "aa" Tst "c:\bb.cc.ext" "c:\bb.cc" } Describe Instr { function Run($s,$sub,$rev,$exp) { $r=if($rev){"-rev"}; It "'$s' '$sub' $r -> $exp" { $a =if($rev){Instr $s $sub -rev}else{Instr $s $sub}; $a| Should be $exp } } function Tst($s,$sub,$exp) { Run $s $sub $false $exp } function TstR($s,$sub,$exp) { Run $s $sub $true $exp } Tst "aab" "b" 3 Tst "aab" "a" 1 Tst "aab" "z" 0 Tst "a a b a" "a" 1 TstR "a a b a" "a" 7 TstR "aab" "b" 3 TstR "aab" "a" 2 TstR "aab" "z" 0 } Describe Brk-Spc1 { $qqStr = "Brk-Spc1 ? -> ? ?" $wdt = 20,20,20 function Tst($s,$exp1,$exp2) { $a = Fmt-FixWdt $wdt $s,$exp1,$exp2 $qqStr -sqQuote; it $a { $1,$2 = Brk-Spc1 $s; $1 | Should be $exp1; $2 | Should be $exp2 } } It (Fmt-FixWdt $wdt `$s,`$exp[0],`$exp[1] $qqStr) {} Tst "aa bb cc " "aa" "bb cc" Tst " aa bb cc" "aa" "bb cc" } Describe Get-MacroAy { $wdt = 30,20 $qqStr = "Get-MacroAy ? -> ?" function Tst($s,$exp) { $a = Fmt-FixWdt $wdt $s,$exp $qqStr -sqQuote it $a { $act = Get-MacroAy $s $act | Should be $exp } } It (Fmt-FixWdt $wdt s,exp $qqStr) {} Tst 'aaa {aa} {bb} {aa}' aa,bb Tst 'aaa {a a} {b } { b} {aa}' 'a a',b,aa } Describe Get-MacroAy1 { $wdt = 30,20 $qqStr = "Get-MacroAy ? -> ?" function Tst($s,$exp) { $a = Fmt-FixWdt $wdt s,exp $qqStr -sqQuote it $a { $act = Get-MacroAy $s $act | Should be $exp } } Tst $qqStr $t { $s,$exp = $_} { Get-MacroAy1 $s | Should be $exp } It (Fmt-FixWdt $wdt s,exp $qqStr) {} Tst 'aaa {aa} {bb} {aa}' aa,bb Tst 'aaa {a a} {b } { b} {aa}' 'a a',b,aa } function Tst([string]$macroStr, [object[][]]$tstDta, [scriptBlock]$tstScript) { <# .SYNOPSIS Passing $tstDta and $tstScript to test a function. .PARAMETER qqStr Example, 'Mid {s} {pos} {len} -> {exp}' It describes each element in $tstDta is 4-elements-array of varaibles: {s pos len exp} .PARAMETER tstDta It is each element is one test case. Each test case has same number of variables which will be used by {tstScript} .PARAMETER tstScript It should contains {$_} which will be one test case data. Usually, all the beginning data will be the parameters of the function to be test and the last one the expected value of result of the calling function #> } function Get-ItAy([string]$macroStr, [object[][]]$tstDta) { $colNy = Get-MacroAy $macroStr $qqStr = &{ $o=$macroStr; $colNy | foreach { $o = Repl-Sub $o $_ '?' } $o } $wdt = Get-Wdt $tstDta $lblLin = Fmt-FixWdt $wdt $colNy $qqStr # $colNyQ = Quote-Ay $colNy '{}' $itAy = $tstDta|foreach { $dtaEleStrAy = Stringify-Ay $_ # making $_ which is $tstDr as StrAy. Each element in $_ is parameter or expected. # make them as string and save to $dta Fmt-FixWdt $wdt $dtaEleStrAy $qqStr } ,$lblLin + $itAy } <# $macroStr = "Brk-Spc1 {s} -rev -> {exp1} {exp2}" $t = @( ,(ay 'aa bb cc ' 'aa bb' cc) ,(ay ' aa bb cc' 'aa bb' cc) ) $itAy = Get-ItAy $macroStr $t $itAy #> function Tst-Brk-Spc1 { $macroStr = "Brk-Spc1 {s} -rev -> {exp1} {exp2}" $t += ,(ay `$s `$exp[0] `$exp[1]) $t += ,(ay 'aa bb cc ' 'aa bb' cc) $t += ,(ay ' aa bb cc' 'aa bb' cc) $tstScript = { $1,$2 = Brk-Spc1 $s -rev; $1 | Should be $exp1; $2 | Should be $exp2 } Tst $macroStr $t $tstScript } #Tst-Brk-Spc1 Describe Get-Term { function Tst($s,$atMost,$exp) { It "[$s] [$atMost] -> [$exp]" { Get-Term $s $atMost | Should be $exp } } Tst "a b c d" 2 @("a","b c d") Tst "a b c d" 3 @("a","b","c d") Tst "a b c d" 4 @("a","b","c","d") Tst "a b c d" 5 @("a","b","c","d") Tst "a b c d" 100 @("a","b","c","d") } Describe Add-FnSfx { function Tst($fn,$sfx,$exp) { it "Add-FnSfx ""$fn"" ""$sfx"" -> ""$exp""" { (Add-FnSfx $fn $sfx) | should be $exp} } Tst -fn aabb.xls -sfx '(aa)' -exp 'aabb(aa).xls' } Describe Repl-Ext { function Tst($ffn,$newExt,$exp) { it ('{0,-15} {1,-10} -> {2,-10}' -f "[$ffn]","[$newExt]","[$exp]") { Repl-Ext $ffn $newExt | Should be $exp} } Tst "aaa.xlsx" ".txt" "aaa.txt" Tst "aaa" ".txt" "aaa.txt" Tst "aa.bb.wrd" ".txt" "aa.bb.txt" } Describe Rmv-2DotInPth { function run($pth,$exp) { it "[$pth]->[$exp]" { Rmv-2DotInPth $pth | should be $exp } } function runThrow($pth) { it "[$pth]->Throw" { {Rmv-2DotInPth $pth} | Should Throw} } run "c:\..\a" "a\" run "c:\a\b\..\c\" "c:\a\c\" run "c:\a\b\\..\c\" "c:\a\b\c\" run "c:\a\b\\..\c\." "c:\a\b\c\.\" run ".." "\" run "c:\..\.." "\" } Describe Get-Term { function run($s,$exp) { It "should pass" { Get-Term $s | Should Be $exp } } run "aa bb cc dd" ("aa","bb cc dd") } Describe Mid { function run($s,$pos,$len,$exp) { It "Mid '$s' $pos $len -> '$exp'" { Mid $s $pos $len | Should be $exp } } function shouldThrow($s,$pos,$len) { It "Mid '$s' $pos $len -> throw" {{ Mid $s $pos $len} | Should Throw} } run "aa bb cc dd" 1 2 "aa" run "aa bb cc dd" 1 3 "aa " run "aa bb cc dd" 1 0 "" shouldThrow "aa bb cc dd" 0 1 shouldThrow "aa bb cc dd" 0 100 } Describe Is-Empty { function run($obj,$exp,$rmk) { It "is-empty should be [$exp] for 6$rmk" { Is-Empty $obj | Should be $exp } } function runPos($obj,$rmk) { run $obj $true $rmk } function runNeg($obj,$rmk) { run $obj $false $rmk } runPos $null '$null' runPos "" 'empty-string' runPos " " '1-space-string' runPos "`t" 'tab-string' } Describe Rmv-Empty { function run($rmk,$ay,$exp) { It "$rmk" { Rmv-Empty $ay | Should be $exp } } run "[1 $null 2 3] --> [1 2 3]" (1,$null,2,3) (1,2,3) run "[1 '' 2 3] --> [1 2 3]" (1,"",2,3) (1,2,3) run "[1 ' ' 2 3] --> [1 2 3]" (1," ",2,3) (1,2,3) } Describe len { It 'Len @{a=1;b=1} --> 28 # len of non-string will convert' { len (@{aa=1;bb=3}) | Should be 28 } It 'Len $null --> 0' { len $null should be 0 } It 'Len 1334 --> 4' { len 1334 should be 4 } It "using Get-StrLen for 10000" { (1..10000) | foreach { Get-StrLen (@{aa=1})} } It "using len for 10000" { (1..10000) | foreach { len (@{aa=1}) } } } Describe Ens-Pth { It "All-path-segment will be created" { $tmp = Get-TmpPth $tmp += "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-* { $ffn = "c:\aa\bb\cc.txt" It 'Get-FfnPth' { Get-FfnPth $ffn | should be "c:\aa\bb\" } It 'Get-FfnFn ' { Get-FfnFn $ffn | should be "cc.txt" } It 'Get-FfnExt' { Get-FfnExt $ffn | should be ".txt" } } Describe Get-Tmp* { $act = Get-TmpPth It "Get-Tmppth should like [$act] & should exist" { $exp = "C:\Users\user\AppData\Local\Temp\mypowershell\" Test-Path $act | Should Be $true $act | Should Be $exp } $act = Get-TmpFdr abc It "(Get-Tmpfdr abc) should be [$act] & should exist" { Test-Path $act | Should be $true Is-Pfx $act 'C:\Users\user\AppData\Local\Temp\mypowershell\abc\T2017' | Should Match rd $act } } Describe Minus-Ay { function run($ay1,$ay2,$exp) { it "($ay1) - ($ay2) --> ($exp)" {Minus-Ay $ay1 $ay2 | Should be $exp } } run (1,2,3,4) (2,4) (1,3) run (1,2,3,4) ("2",4) (1,3) } |