diff --git a/Test/include/callPrivateContext.ps1 b/Test/include/callPrivateContext.ps1 new file mode 100644 index 0000000..c9e0ab0 --- /dev/null +++ b/Test/include/callPrivateContext.ps1 @@ -0,0 +1,28 @@ +# CALL PRIVATE FUNCTIONS +# +# This file enables tests to invoke private (non-exported) functions +# defined inside the main module by evaluating a provided scriptblock +# in the module context. +# +# It relies on variables initialized by module.helper.ps1. +# REQUIRED: +# $MODULE_PATH - Full path to the module .psm1 file (or a file inside its folder) +# +# THIS INCLUDE REQUIRES module.helper.ps1 +if(-not $MODULE_PATH){ throw "Missing MODULE_PATH variable initialization. Check for module.helper.ps1 file." } + +function Invoke-PrivateContext { + param ( + [Parameter(Mandatory, Position = 0)] + [scriptblock]$ScriptBlock + ) + + $modulePath = $MODULE_PATH | Split-Path -Parent + $module = Import-Module -Name $modulePath -PassThru + + if ($null -eq $module) { + throw "Failed to import the main module." + } + + & $module $ScriptBlock +} Export-ModuleMember -Function Invoke-PrivateContext diff --git a/Test/public/callPrivateFunction.test.ps1 b/Test/public/callPrivateFunction.test.ps1 new file mode 100644 index 0000000..73bd1d8 --- /dev/null +++ b/Test/public/callPrivateFunction.test.ps1 @@ -0,0 +1,27 @@ +function Test_InvokePrivateFunction{ + + # First check that private function call fails + $errorStr = @" +The term 'Get-PrivateString' is not recognized as a name of a cmdlet, function, script file, or executable program. +Check the spelling of the name, or if a path was included, verify that the path is correct and try again. +"@ + + $hasthrown = $false + try{ + Get-PrivateString -Param1 "TestParam" + } catch { + $hasthrown = $true + Assert-AreEqual -Expected $errorStr -Presented $_.Exception.Message + } + Assert-IsTrue -Condition $hasthrown + + + # Call private function through Invoke-PrivateContext + Invoke-PrivateContext { + + $result = Get-PrivateString -Param1 "TestParam" + + Assert-AreEqual -Expected "Private string [TestParam]" -Presented $result + } + +} \ No newline at end of file