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:
"You know that using conversions like .ToUpper()
to do case insensitive comparisons can be
problematic?"
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"
_"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
"I admit, 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:
1function Test-ToUpper {
2 [CmdletBinding()]
3 param (
4 [string]$Text
5 )
6 $Text.ToUpper()
7}
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):
1$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
3. "$here\$sut"
4
5Describe 'Test-ToUpper' {
6 It 'Converts to Upper Case' {
7 Test-ToUpper -Text 'SQLEngine' | Should BeExactly 'SQLENGINE'
8 }
9 It 'Converts to Upper Case using Turkish Culture' {
10 Use-Culture -Culture tr-TR -ScriptBlock {
11 Test-ToUpper -Text 'SQLEngine'
12 } | Should BeExactly 'SQLENGINE'
13 }
14}
The results show that using the ToUpper()
method does indeed have the "Turkish I" problem as it's
referred to:
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.
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."
µ