Skip to content

Commit 825bc61

Browse files
committed
Refactoring
added Test-ByteArrayType. Test-StreamType
1 parent fe68939 commit 825bc61

File tree

6 files changed

+64
-12
lines changed

6 files changed

+64
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Test-ByteArrayType {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
$Object
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet Test-ByteArrayType - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet Test-ByteArrayType - Process"
14+
$Object.PSTypeNames -contains [System.Byte[]].ToString()
15+
}
16+
17+
end {
18+
Write-Verbose "Cmdlet Test-ByteArrayType - End"
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Test-StreamType {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory = $true, Position = 0 )]
5+
$Object
6+
)
7+
8+
begin {
9+
Write-Verbose "Cmdlet Test-StreamType - Begin"
10+
}
11+
12+
process {
13+
Write-Verbose "Cmdlet Test-StreamType - Process"
14+
$Object.PSTypeNames -contains [System.IO.Stream]
15+
}
16+
17+
end {
18+
Write-Verbose "Cmdlet Test-StreamType - End"
19+
}
20+
}

Photo.Shell/Public/Compress-Image.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function Compress-Image {
1515

1616
process {
1717
Write-Verbose "Cmdlet Compress-Image - Process"
18-
$isByte = $Image.PSTypeNames -contains [System.Byte[]].ToString()
19-
$isStream = $Image.PSTypeNames -contains [System.IO.Stream]
18+
$isByte = Test-ByteArrayType $Image
19+
$isStream = Test-StreamType $Image
2020

2121
if (!$isByte -and !$isStream ) {
2222
throw [System.ArgumentException]::new("Invalid input. Accepted types are: [System.IO.Stream] and [System.Byte[]]")

Photo.Shell/Public/Get-ImageCodecInfo.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
function Get-ImageCodecInfo {
44
[CmdletBinding()]
55
param (
6-
# [byte[]] or [System.IO.Stream]
76
[Parameter(Mandatory = $true, Position = 0 )]
87
$Image
98
)
@@ -15,7 +14,14 @@ function Get-ImageCodecInfo {
1514
process {
1615
Write-Verbose "Cmdlet Get-ImageCodecInfo - Process"
1716

18-
if ($Image.PSTypeNames -contains [System.IO.Stream]) {
17+
$isByte = Test-ByteArrayType $Image
18+
$isStream = Test-StreamType $Image
19+
20+
if (!$isByte -and !$isStream ) {
21+
throw [System.ArgumentException]::new("Invalid input. Accepted types are: [System.IO.Stream] and [System.Byte[]]")
22+
}
23+
24+
if ($isStream) {
1925
$Image = $Image.ToArray()
2026
}
2127

Photo.Shell/Public/Resize-Image.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ function Resize-Image {
2020
process {
2121
Write-Verbose "Cmdlet Resize-Image - Process"
2222

23-
if ($Image.PSTypeNames -contains [System.Byte[]].ToString()) {
23+
$isByte = Test-ByteArrayType $Image
24+
$isStream = Test-StreamType $Image
25+
26+
if (!$isByte -and !$isStream ) {
27+
throw [System.ArgumentException]::new("Invalid input. Accepted types are: [System.IO.Stream] and [System.Byte[]]")
28+
}
29+
30+
if ($isByte) {
2431
$stream = [System.IO.MemoryStream]::new($Image)
2532
}
2633
else {

Tests/Photo.Shell.Tests.ps1

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Describe 'Photo.Shell.Tests' {
5050
$img = Get-TestImage -Format $Format
5151
Get-ImageCodecInfo $img | Select-Object -ExpandProperty MimeType | Should -BeExactly $Expected
5252
}
53+
54+
It "shouldn't accept" {
55+
{ Get-ImageCodecInfo $false } | Should -Throw -ExceptionType ([System.ArgumentException])
56+
}
5357
}
5458

5559
Context "Resize-Image" {
@@ -103,9 +107,7 @@ Describe 'Photo.Shell.Tests' {
103107
}
104108

105109
It "shouldn't accept" {
106-
$stream = [System.IO.MemoryStream]::new($testImage)
107-
$img = [System.Drawing.Image]::FromStream($stream)
108-
{ Resize-Image -Image $img -Ratio 1 } | Should -Throw
110+
{ Resize-Image -Image $false -Ratio 1 } | Should -Throw -ExceptionType ([System.ArgumentException])
109111
}
110112
}
111113

@@ -133,10 +135,7 @@ Describe 'Photo.Shell.Tests' {
133135
}
134136

135137
It "shouldn't accept" {
136-
$type = [System.Drawing.Imaging.ImageFormat]::Png
137-
$img_before = Get-TestImage -Format $type
138-
$img_before = Convert-BytesToImage $img_before
139-
{ Compress-Image $img_before -Format $type -Compression 10L } | Should -Throw -ExceptionType ([System.ArgumentException])
138+
{ Compress-Image $false -Format $type -Compression 10L } | Should -Throw -ExceptionType ([System.ArgumentException])
140139
}
141140
}
142141
}

0 commit comments

Comments
 (0)