-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Public Function Test-IsNumericType (#88)
- Loading branch information
Showing
5 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<# | ||
.SYNOPSIS | ||
Returns whether the specified object is of a numeric type. | ||
.DESCRIPTION | ||
Returns whether the specified object is of a numeric type. | ||
.PARAMETER Object | ||
The object to test if it is a numeric type. | ||
.EXAMPLE | ||
Test-IsNumericType -Object ([System.UInt32] 1) | ||
Returns $true since the object passed is of a numeric type. | ||
.OUTPUTS | ||
[System.Boolean] | ||
#> | ||
function Test-IsNumericType | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[Parameter(ValueFromPipeline = $true)] | ||
[System.Object] | ||
$Object | ||
) | ||
|
||
process | ||
{ | ||
$isNumeric = $false | ||
|
||
if ( | ||
$Object -is [System.Byte] -or | ||
$Object -is [System.Int16] -or | ||
$Object -is [System.Int32] -or | ||
$Object -is [System.Int64] -or | ||
$Object -is [System.SByte] -or | ||
$Object -is [System.UInt16] -or | ||
$Object -is [System.UInt32] -or | ||
$Object -is [System.UInt64] -or | ||
$Object -is [System.Decimal] -or | ||
$Object -is [System.Double] -or | ||
$Object -is [System.Single] | ||
) | ||
{ | ||
$isNumeric = $true | ||
} | ||
|
||
return $isNumeric | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] | ||
param () | ||
|
||
BeforeDiscovery { | ||
try | ||
{ | ||
if (-not (Get-Module -Name 'DscResource.Test')) | ||
{ | ||
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task. | ||
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) | ||
{ | ||
# Redirect all streams to $null, except the error stream (stream 2) | ||
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null | ||
} | ||
|
||
# If the dependencies has not been resolved, this will throw an error. | ||
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' | ||
} | ||
} | ||
catch [System.IO.FileNotFoundException] | ||
{ | ||
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.' | ||
} | ||
} | ||
|
||
BeforeAll { | ||
$script:dscModuleName = 'DscResource.Common' | ||
|
||
Import-Module -Name $script:dscModuleName | ||
|
||
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName | ||
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName | ||
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName | ||
} | ||
|
||
AfterAll { | ||
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName') | ||
$PSDefaultParameterValues.Remove('Mock:ModuleName') | ||
$PSDefaultParameterValues.Remove('Should:ModuleName') | ||
|
||
# Unload the module being tested so that it doesn't impact any other tests. | ||
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force | ||
} | ||
|
||
Describe 'Test-IsNumericType' -Tag 'Public' { | ||
Context 'When passing value with named parameter' { | ||
Context 'When type is numeric' { | ||
It 'Should return the correct value' { | ||
InModuleScope -ScriptBlock { | ||
$result = Test-IsNumericType -Object ([System.UInt32] 3) | ||
|
||
$result | Should -BeTrue | ||
} | ||
} | ||
} | ||
|
||
Context 'When type is not numeric' { | ||
It 'Should return the correct value' { | ||
InModuleScope -ScriptBlock { | ||
$result = Test-IsNumericType -Object ([System.String] 'a') | ||
|
||
$result | Should -BeFalse | ||
} | ||
} | ||
} | ||
} | ||
|
||
Context 'When passing value in pipeline' { | ||
Context 'When type is numeric' { | ||
It 'Should return the correct value' { | ||
InModuleScope -ScriptBlock { | ||
$result = ([System.UInt32] 3) | Test-IsNumericType | ||
|
||
$result | Should -BeTrue | ||
} | ||
} | ||
} | ||
|
||
Context 'When type is not numeric' { | ||
It 'Should return the correct value' { | ||
InModuleScope -ScriptBlock { | ||
$result = ([System.String] 'a') | Test-IsNumericType | ||
|
||
$result | Should -BeFalse | ||
} | ||
} | ||
} | ||
} | ||
} |