Tests/Unit/ConvertFrom-ScriptConfigJson.Tests.ps1


BeforeAll {

    $modulePath = Resolve-Path -Path "$PSScriptRoot\..\..\.." | Select-Object -ExpandProperty Path
    $moduleName = Resolve-Path -Path "$PSScriptRoot\..\.." | Get-Item | Select-Object -ExpandProperty BaseName

    Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
    Import-Module -Name "$modulePath\$moduleName" -Force
}

Describe 'ConvertFrom-ScriptConfigJson' {

    It 'should be able to convert the example config file' {

        InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"

            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config | Should -Not -BeNullOrEmpty
        }
    }

    It 'should be able to parse a string' {

        InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"

            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config.MyString           | Should -Be 'This is a test JSON config file!'
            $config.MyString.GetType() | Should -Be ([System.String])
        }
    }

    It 'should be able to parse an integer' {

        InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"


            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config.MyIntegerPositive           | Should -Be 42
            $config.MyIntegerPositive.GetType() | Should -BeIn ([System.Int32], [System.Int64])   # PowerShell 5.1: Int32, PowerShell 7+: Int64
            $config.MyIntegerNegative           | Should -Be -153
            $config.MyIntegerNegative.GetType() | Should -BeIn ([System.Int32], [System.Int64])   # PowerShell 5.1: Int32, PowerShell 7+: Int64
        }
    }

    It 'should be able to parse an boolean' {

            InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"

            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config.MyBooleanTrue            | Should -BeTrue
            $config.MyBooleanTrue.GetType()  | Should -Be ([System.Boolean])
            $config.MyBooleanFalse           | Should -BeFalse
            $config.MyBooleanFalse.GetType() | Should -Be ([System.Boolean])
        }
    }

    It 'should be able to parse an array' {

        InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"
            $expectedArray = @( 'Lorem', 'Ipsum' )

            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config.MyArray           | Should -Not -BeNullOrEmpty
            $config.MyArray           | Should -Be $expectedArray
            $config.MyArray.GetType() | Should -Be ([System.Object[]])
        }
    }

    It 'should be able to parse an hashtable' {

        InModuleScope $moduleName {

            # Arrange
            $content = Get-Content -Path "$PSScriptRoot\TestData\config.json"
            $expectedHashtable = @{ Foo = 'Bar'; Hello = 'World' }

            # Act
            $config = ConvertFrom-ScriptConfigJson -Content $content

            # Assert
            $config.MyHashtable                       | Should -Not -BeNullOrEmpty
            $config.MyHashtable.Keys -as [string[]]   | Should -Be ($expectedHashtable.Keys -as [string[]])
            $config.MyHashtable.Values -as [string[]] | Should -Be ($expectedHashtable.Values -as [string[]])
            $config.MyHashtable.GetType()             | Should -Be ([System.Collections.Hashtable])
        }
    }
}