Using Pester to Test PowerShell Code with Other Cultures

I recently published a blog article on unexpected case sensitivity in PowerShell. An example in that blog article uses the contains method which is indeed case sensitive. One of the workarounds that I demonstrated was to convert whatever the user entered to upper case using the ToUpper() method.

One of the reasons I like blogging is that many times there are things that I may not have considered and sometimes things that I wasn’t even aware of so in addition to sharing my knowledge with others, I often times learn in the process based on the feedback I receive about my blog articles and this was one of those times.

I received a response to my tweet about that blog article from @Oleschri on Twitter:

turkey-test1a.jpg

“You know that using conversions like .ToUpper() to do case insensitive comparisons can be problematic?"

turkey-test2a.jpg

No one knows everything so always be willing to listen to others. My response: “I haven’t had any problems with .ToUpper(), but if it can be problematic, I would like to hear more about that & see some examples”

turkey-test3a.jpg

_“Sure. I present to you the The Turkey Test. Yeah, really, they call it that. https://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

turkey-test4a.jpg

“I admin, nobody should expect i18n problems in service names. But I never give advice to use such workarounds for that reason."

More info about i18n.

After reading the previously referenced Turkey Test blog article, I created a simple Test-ToUpper function in PowerShell to test with:

function Test-ToUpper {
    [CmdletBinding()]
    param (
        [string]$Text
    )
    $Text.ToUpper()
}

I created a Pester test to not only test using my culture, but also with the Turkish culture via the Use-Culture function that’s part of the PowerShellCookbook module (the PowerShell Cookbook and module was written by Lee Holmes):

$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
. "$here\$sut"

Describe 'Test-ToUpper' {
    It 'Converts to Upper Case' {
        Test-ToUpper -Text 'SQLEngine' | Should BeExactly 'SQLENGINE'
    }
    It 'Converts to Upper Case using Turkish Culture' {
        Use-Culture -Culture tr-TR -ScriptBlock {
            Test-ToUpper -Text 'SQLEngine'
        } | Should BeExactly 'SQLENGINE'
    }
}

The results show that using the ToUpper() method does indeed have the “Turkish I” problem as it’s referred to:

turkey-test6a.jpg

I’m sure you can see how simple it really is to test your code and make sure it works internationally and not just with your localization.

turkey-test5a.jpg

Give credit to others and thank them for their assistance. This will make them more likely to provide feedback to you and to others in the future. My response: “You’re right. It has the “Turkish I” problem. Thanks for the info. That gives me some additional tests to perform on my code."

ยต