From 5e1d194f0925bc3bdb50dea2a0a609b907d245b0 Mon Sep 17 00:00:00 2001 From: Josh <9366888+hollanjs@users.noreply.github.com> Date: Sat, 17 Dec 2022 07:33:38 -0500 Subject: [PATCH] Add Public Function Test-IsNumericType (#88) --- CHANGELOG.md | 10 ++- README.md | 36 ++++++++ source/Public/Test-IsNumericType.ps1 | 53 +++++++++++ .../Unit/Public/Assert-ElevatedUser.Tests.ps1 | 2 +- .../Unit/Public/Test-IsNumericType.Tests.ps1 | 89 +++++++++++++++++++ 5 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 source/Public/Test-IsNumericType.ps1 create mode 100644 tests/Unit/Public/Test-IsNumericType.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 48959de..a4e7272 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` @@ -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 diff --git a/README.md b/README.md index 96f631a..8278e63 100644 --- a/README.md +++ b/README.md @@ -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 + + +```plaintext +Test-IsNumericType [[-Object] ] [] +``` + + +#### Outputs + +**System.Boolean** + +#### Example + +```PowerShell +Test-IsNumericType -Object ([System.UInt32] 3) +``` +```PowerShell +([System.String] 'a') | Test-IsNumericType +``` diff --git a/source/Public/Test-IsNumericType.ps1 b/source/Public/Test-IsNumericType.ps1 new file mode 100644 index 0000000..50e5c50 --- /dev/null +++ b/source/Public/Test-IsNumericType.ps1 @@ -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 + } +} diff --git a/tests/Unit/Public/Assert-ElevatedUser.Tests.ps1 b/tests/Unit/Public/Assert-ElevatedUser.Tests.ps1 index c714688..cde42c5 100644 --- a/tests/Unit/Public/Assert-ElevatedUser.Tests.ps1 +++ b/tests/Unit/Public/Assert-ElevatedUser.Tests.ps1 @@ -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 diff --git a/tests/Unit/Public/Test-IsNumericType.Tests.ps1 b/tests/Unit/Public/Test-IsNumericType.Tests.ps1 new file mode 100644 index 0000000..bd0fc43 --- /dev/null +++ b/tests/Unit/Public/Test-IsNumericType.Tests.ps1 @@ -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 + } + } + } + } +}