|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +Tests converting bytes to largest possible units, to improve readability. |
| 4 | +#> |
| 5 | + |
| 6 | +$basename = "$(($MyInvocation.MyCommand.Name -split '\.',2)[0])." |
| 7 | +$skip = !(Test-Path .changes -Type Leaf) ? $false : |
| 8 | + !@(Get-Content .changes |Get-Item |Select-Object -ExpandProperty Name |Where-Object {$_.StartsWith($basename)}) |
| 9 | +if($skip) {Write-Information "No changes to $basename" -infa Continue} |
| 10 | +Describe 'Format-ByteUnits' -Tag Format-ByteUnits -Skip:$skip { |
| 11 | + BeforeAll { |
| 12 | + $scriptsdir,$sep = (Split-Path $PSScriptRoot),[io.path]::PathSeparator |
| 13 | + if($scriptsdir -notin ($env:Path -split $sep)) {$env:Path += "$sep$scriptsdir"} |
| 14 | + } |
| 15 | + Context 'Converts bytes to largest possible units, to improve readability' -Tag FormatByteUnits,Format,ByteUnits { |
| 16 | + It "Formatting '<Bytes>', up to '<Precision>' digits after the decimal returns '<Result>', or '<SIResult> for SI'" -TestCases @( |
| 17 | + @{ Bytes = 0; Precision = 16; Result = '0'; SIResult = '0' } |
| 18 | + @{ Bytes = 1; Precision = 16; Result = '1'; SIResult = '1 B' } |
| 19 | + @{ Bytes = 999; Precision = 16; Result = '999'; SIResult = '999 B' } |
| 20 | + @{ Bytes = 0KB; Precision = 16; Result = '0'; SIResult = '0' } |
| 21 | + @{ Bytes = 1KB; Precision = 16; Result = '1KB'; SIResult = '1 KiB' } |
| 22 | + @{ Bytes = 999KB; Precision = 16; Result = '999KB'; SIResult = '999 KiB' } |
| 23 | + @{ Bytes = 1MB; Precision = 16; Result = '1MB'; SIResult = '1 MiB' } |
| 24 | + @{ Bytes = 999MB; Precision = 16; Result = '999MB'; SIResult = '999 MiB' } |
| 25 | + @{ Bytes = 1GB; Precision = 16; Result = '1GB'; SIResult = '1 GiB' } |
| 26 | + @{ Bytes = 999GB; Precision = 16; Result = '999GB'; SIResult = '999 GiB' } |
| 27 | + @{ Bytes = 1TB; Precision = 16; Result = '1TB'; SIResult = '1 TiB' } |
| 28 | + @{ Bytes = 999TB; Precision = 16; Result = '999TB'; SIResult = '999 TiB' } |
| 29 | + @{ Bytes = 1PB; Precision = 16; Result = '1PB'; SIResult = '1 PiB' } |
| 30 | + @{ Bytes = 999PB; Precision = 16; Result = '999PB'; SIResult = '999 PiB' } |
| 31 | + @{ Bytes = 999999999999999999N; Precision = 2; Result = '888.18PB'; SIResult = '888.18 PiB' } |
| 32 | + @{ Bytes = 1234567890L; Precision = 2; Result = '1.15GB'; SIResult = '1.15 GiB' } |
| 33 | + @{ Bytes = 1234567890L; Precision = 1; Result = '1.1GB'; SIResult = '1.1 GiB' } |
| 34 | + @{ Bytes = 1234567890L; Precision = 0; Result = '1GB'; SIResult = '1 GiB' } |
| 35 | + @{ Bytes = 987654321000; Precision = 16; Result = '919.824765063822GB'; SIResult = '919.824765063822 GiB' } |
| 36 | + @{ Bytes = 987654321000; Precision = 12; Result = '919.824765063822GB'; SIResult = '919.824765063822 GiB' } |
| 37 | + @{ Bytes = 987654321000; Precision = 11; Result = '919.82476506382GB'; SIResult = '919.82476506382 GiB' } |
| 38 | + @{ Bytes = 987654321000; Precision = 10; Result = '919.8247650638GB'; SIResult = '919.8247650638 GiB' } |
| 39 | + @{ Bytes = 987654321000; Precision = 9; Result = '919.824765064GB'; SIResult = '919.824765064 GiB' } |
| 40 | + @{ Bytes = 987654321000; Precision = 8; Result = '919.82476506GB'; SIResult = '919.82476506 GiB' } |
| 41 | + @{ Bytes = 987654321000; Precision = 7; Result = '919.8247651GB'; SIResult = '919.8247651 GiB' } |
| 42 | + @{ Bytes = 987654321000; Precision = 6; Result = '919.824765GB'; SIResult = '919.824765 GiB' } |
| 43 | + @{ Bytes = 987654321000; Precision = 5; Result = '919.82477GB'; SIResult = '919.82477 GiB' } |
| 44 | + @{ Bytes = 987654321000; Precision = 4; Result = '919.8248GB'; SIResult = '919.8248 GiB' } |
| 45 | + @{ Bytes = 987654321000; Precision = 3; Result = '919.825GB'; SIResult = '919.825 GiB' } |
| 46 | + @{ Bytes = 987654321000; Precision = 2; Result = '919.82GB'; SIResult = '919.82 GiB' } |
| 47 | + @{ Bytes = 987654321000; Precision = 1; Result = '919.8GB'; SIResult = '919.8 GiB' } |
| 48 | + @{ Bytes = 987654321000; Precision = 0; Result = '920GB'; SIResult = '920 GiB' } |
| 49 | + @{ Bytes = 9685059; Precision = 1; Result = '9.2MB'; SIResult = '9.2 MiB' } |
| 50 | + ) { |
| 51 | + Param([bigint] $Bytes, [byte] $Precision, [string] $Result, [string] $SIResult) |
| 52 | + Format-ByteUnits.ps1 -Bytes $Bytes -Precision $Precision | |
| 53 | + Should -BeExactly $Result -Because 'parameter should work' |
| 54 | + $Bytes |Format-ByteUnits.ps1 -Precision $Precision | |
| 55 | + Should -BeExactly $Result -Because 'pipeline should work' |
| 56 | + Format-ByteUnits.ps1 -Bytes $Bytes -Precision $Precision -UseSI | |
| 57 | + Should -BeExactly $SIResult -Because 'SI parameter should work' |
| 58 | + $Bytes |Format-ByteUnits.ps1 -Precision $Precision -UseSI | |
| 59 | + Should -BeExactly $SIResult -Because 'SI pipeline should work' |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments