Skip to content

Commit

Permalink
Test-IsNumericType: Now handles arrays correctly (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Dec 18, 2022
1 parent 9b2abdb commit 161abe2
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- DscResource.Common
- Now builds the module into a separate folder `output/builtModule`.

### Fixed

- `Test-IsNumericType`
- Now handles arrays correctly.

## [0.13.0] - 2022-12-17

### Added

- Added public function `Test-IsNumericType` that returns whether the specified
- 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).

Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ the parameters `Parameter1` and `Parameter2`.

Assert that the user has elevated the PowerShell session.

`Assert-ElevatedUser` will throw a statement-terminating error if the
`Assert-ElevatedUser` will throw a statement-terminating error if the
script is not run from an elevated session.

#### Syntax
Expand All @@ -100,7 +100,7 @@ None.
`Assert-ElevatedUser -ErrorAction 'Stop'`
```

This example stops the entire script if it is not run from an
This example stops the entire script if it is not run from an
elevated PowerShell session.

### `Assert-IPAddress`
Expand Down Expand Up @@ -201,7 +201,7 @@ Compare-DscParameterState [-CurrentValues] <Object> [-DesiredValues] <Object>

#### Outputs

Returns an array containing a psobject with metadata for each property
Returns an array containing a PSObject with metadata for each property
that was evaluated.

Metadata Name | Type | Description
Expand All @@ -216,6 +216,7 @@ ActualValue | `[System.PsObject]` | Return the value of current object.
#### Example

##### Example 1

```powershell
$currentValues = @{
String = 'This is a string'
Expand Down Expand Up @@ -947,6 +948,7 @@ if ((Test-IsNanoServer)) {
### `Test-IsNumericType`

Returns whether the specified object is of a numeric type:

- [System.Byte]
- [System.Int16]
- [System.Int32]
Expand All @@ -973,10 +975,22 @@ Test-IsNumericType [[-Object] <Object>] [<CommonParameters>]

#### Example

```PowerShell
```powershell
Test-IsNumericType -Object ([System.UInt32] 3)
```
```PowerShell

Returns `$true` since the value is a numeric type.

```powershell
([System.String] 'a') | Test-IsNumericType
```

Returns `$false` since the value is not a numeric type.

```powershell
('a', 2, 'b') | Test-IsNumericType
```

Returns `$true` since one of the values passed in the pipeline is of a
numeric type.
<!-- markdownlint-enable MD036 - Emphasis used instead of a heading -->
1 change: 1 addition & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ prefix: prefix.ps1
suffix: suffix.ps1
Encoding: UTF8
VersionedOutputDirectory: true
BuiltModuleSubdirectory: builtModule

####################################################
# Sampler Pipeline Configuration #
Expand Down
17 changes: 16 additions & 1 deletion source/Public/Test-IsNumericType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@
Returns $true since the object passed is of a numeric type.
.EXAMPLE
('a', 2, 'b') | Test-IsNumericType
Returns $true since one of the values in the array is of a numeric type.
.OUTPUTS
[System.Boolean]
.NOTES
When passing in an array of values from the pipeline, the command will return
$true if any of the values in the array is numeric.
#>
function Test-IsNumericType
{
Expand All @@ -27,10 +36,13 @@ function Test-IsNumericType
$Object
)

process
begin
{
$isNumeric = $false
}

process
{
if (
$Object -is [System.Byte] -or
$Object -is [System.Int16] -or
Expand All @@ -47,7 +59,10 @@ function Test-IsNumericType
{
$isNumeric = $true
}
}

end
{
return $isNumeric
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Public/Test-IsNumericType.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,25 @@ Describe 'Test-IsNumericType' -Tag 'Public' {
}
}
}

Context 'When type is an array with no numeric values' {
It 'Should return the correct value' {
InModuleScope -ScriptBlock {
$result = ('a', 'b') | Test-IsNumericType

$result | Should -BeFalse
}
}
}

Context 'When type is an array with a numeric value' {
It 'Should return the correct value' {
InModuleScope -ScriptBlock {
$result = ('a', 1, 'b') | Test-IsNumericType

$result | Should -BeTrue
}
}
}
}
}

0 comments on commit 161abe2

Please sign in to comment.