Skip to content

Commit

Permalink
Add Public Function Test-IsNumericType (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
hollanjs authored Dec 17, 2022
1 parent 05e5294 commit 5e1d194
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 3 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added public function `Test-IsNumericType` that returns whether the specified
object is of a numeric type - [Issue #87](https://github.com/dsccommunity/DscResource.Common/issues/87)
- Related to SqlServerDsc [Issue #1795](https://github.com/dsccommunity/SqlServerDsc/issues/1795).

### Changed

- `Assert-ElevatedUser`
Expand All @@ -18,8 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added public function `Assert-ElevatedUser` that asserts the user has elevated
the PowerShell session. [issue #82](https://github.com/dsccommunity/DscResource.Common/issues/71)
- Related to SqlServerDsc [issue #1797](https://github.com/dsccommunity/SqlServerDsc/issues/1797).
the PowerShell session. [Issue #82](https://github.com/dsccommunity/DscResource.Common/issues/82)
- Related to SqlServerDsc [Issue #1797](https://github.com/dsccommunity/SqlServerDsc/issues/1797).

## [0.11.1] - 2022-08-18

Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -943,4 +943,40 @@ if ((Test-IsNanoServer)) {
'Nano server'
}
```

### `Test-IsNumericType`

Returns whether the specified object is of a numeric type:
- [System.Byte]
- [System.Int16]
- [System.Int32]
- [System.Int64]
- [System.SByte]
- [System.UInt16]
- [System.UInt32]
- [System.UInt64]
- [System.Decimal]
- [System.Double]
- [System.Single]

#### Syntax

<!-- markdownlint-disable MD013 - Line length -->
```plaintext
Test-IsNumericType [[-Object] <Object>] [<CommonParameters>]
```
<!-- markdownlint-enable MD013 - Line length -->

#### Outputs

**System.Boolean**

#### Example

```PowerShell
Test-IsNumericType -Object ([System.UInt32] 3)
```
```PowerShell
([System.String] 'a') | Test-IsNumericType
```
<!-- markdownlint-enable MD036 - Emphasis used instead of a heading -->
53 changes: 53 additions & 0 deletions source/Public/Test-IsNumericType.ps1
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
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Public/Assert-ElevatedUser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ AfterAll {
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
}

Describe 'Assert-ElevatedUser' -Tag 'Private' {
Describe 'Assert-ElevatedUser' -Tag 'Public' {
BeforeDiscovery {
<#
Since it is not possible to elevated (or un-elevate) a user during testing
Expand Down
89 changes: 89 additions & 0 deletions tests/Unit/Public/Test-IsNumericType.Tests.ps1
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
}
}
}
}
}

0 comments on commit 5e1d194

Please sign in to comment.