File size: 8,096 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Testing of script internationalization' -Tags 'CI' {
    BeforeAll {
        $testCultures = @(
            @{ UICulture = 'en-US' }
            @{ UICulture = 'fr-FR' }
        )

        $currentCulture = $PSUICulture
        [System.Globalization.CultureInfo]::CurrentUICulture = 'en-US'

        $defaultParams = @{
            BindingVariable = 'data'
        }
    }

    BeforeEach {
        Get-Variable -Name data -Scope Local -ErrorAction Ignore | Remove-Variable
    }

    AfterAll {
        [System.Globalization.CultureInfo]::CurrentUICulture = $currentCulture
    }

    Context 'Data section' {
        It 'ConvertFrom-StringData is permitted in a Data section' {
            data dataVariable
            {
                ConvertFrom-StringData @'
string1=string1
string2=string2
'@
            }

            $dataVariable.string1 | Should -BeExactly 'string1'
            $dataVariable.string2 | Should -BeExactly 'string2'
        }

        It 'Throws an error if the data section contains a command which is not allowed' -TestCases @(
            @{ Script = 'data d { @{ x=$(Get-Command)} }'; }
            @{ Script = 'data d { if ($(Get-Command)) {} }' }
            @{ Script = 'data d { @(Get-Command) }' }
        ) {
            param ( $Script )

            { Invoke-Expression $Script } | Should -Throw -ErrorId 'CmdletNotInAllowedListForDataSection,Microsoft.PowerShell.Commands.InvokeExpressionCommand'
        }
    }

    Context 'BindingVariable parameter' {
        It 'BindingVariable binds positionally' {
            Import-LocalizedData data

            $data.string1 | Should -BeExactly 'string1 en-US'
            $data.string2 | Should -BeExactly 'string2 en-US'
        }

        It 'Imports data into the BindingVariable based on the current UICulture' {
            Import-LocalizedData @defaultParams

            $data.string1 | Should -BeExactly 'string1 en-US'
            $data.string2 | Should -BeExactly 'string2 en-US'
        }

        It 'Does not clobber existing variables in a parent scope' {
            $data = data { 456 }
            & {
                 Import-LocalizedData @defaultParams
             }
             $data | Should -Be 456
        }

        It 'Replaces a BindingVariable in a parent scope if a scope modifier is specified' {
            $Script:bindingVariable = data { 456 }

            $Script:bindingVariable | Should -Be 456

            & {
                Import-LocalizedData -BindingVariable Script:bindingVariable
            }

            $Script:bindingVariable.string1 | Should -BeExactly 'string1 en-US'
        }
    }

    Context 'UICulture parameter' {
        It 'Imports specific culture (<UICulture>) defined by the UICulture parameter' -TestCases $testCultures {
            param ( $UICulture )

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            Import-LocalizedData @defaultParams -UICulture $UICulture

            $data.string1 | Should -BeExactly ('string1 {0}' -f $UICulture)
            $data.string2 | Should -BeExactly ('string2 {0}' -f $UICulture)
        }

        It 'Throws an error if the specified UICulture does not exist' -Skip:(-not $IsWindows) {
            { Import-LocalizedData @defaultParams -UICulture none-none -ErrorAction Stop } |
                Should -Throw -ExceptionType 'System.Management.Automation.PSArgumentException'
        }
    }

    Context 'UICulture fallback' {
        It 'When the UICulture parameter is specified, searches parent culture and current directory' -TestCases @(
            @{ UICulture = 'en-US'; ExpectedString = 'en-US' }
            @{ UICulture = 'en-GB'; ExpectedString = 'en' }
            @{ UICulture = 'no-NL'; ExpectedString = 'fallback' }
        ) {
            param ( $UICulture, $ExpectedString )

            if ($UICulture -eq 'no-NL' -and (Test-IsWinServer2012R2))
            {
                Set-ItResult -Skipped -Because 'no-NL culture is not available on Windows Server 2012 R2'
                return
            }

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            $data = Import-LocalizedData -UICulture $UICulture

            $data.string1 | Should -Be ('string1 {0}' -f $ExpectedString)
            $data.string2 | Should -Be ('string2 {0}' -f $ExpectedString)
        }

        It 'When the UICulture parameter is not specified and no files exist falls back on en-US and parent cultures' -TestCases @(
            @{ UICulture = 'no-NL'; ExpectedString = 'en-US' }
        ) {
            param ( $UICulture, $ExpectedString )

            if ($UICulture -eq 'no-NL' -and (Test-IsWinServer2012R2))
            {
                Set-ItResult -Skipped -Because 'no-NL culture is not available on Windows Server 2012 R2'
                return
            }

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            $data = Import-LocalizedData -FileName 'I18n_altfilename'

            $data.string1 | Should -Be ('string1 {0} I18n_altfilename' -f $ExpectedString)
            $data.string2 | Should -Be ('string2 {0} I18n_altfilename' -f $ExpectedString)
        }
    }

    Context 'FileName and BaseDirectory parameters' {
        BeforeAll {
            $fileName = @{ FileName = 'I18n_altfilename' }
            $baseDirectory = @{ BaseDirectory = Join-Path -Path $PSScriptRoot -ChildPath 'I18n_altbase' }
        }

        It 'Imports from the "foo" file name when then FileName parameter is specified (<UICulture>)' -TestCases $testCultures {
            param ( $UICulture )

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            Import-LocalizedData @defaultParams @fileName -UICulture $UICulture

            $data.string1 | Should -BeExactly ('string1 {0} I18n_altfilename' -f $UICulture)
            $data.string2 | Should -BeExactly ('string2 {0} I18n_altfilename' -f $UICulture)
        }

        It 'Imports from the "I18n_altfilename" directory when the BaseDirectory parameter is specified (<UICulture>)' -TestCases $testCultures {
            param ( $UICulture )

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            Import-LocalizedData @defaultParams @baseDirectory -UICulture $UICulture

            $data.string1 | Should -BeExactly ('string1 {0} I18n_altbase' -f $UICulture)
            $data.string2 | Should -BeExactly ('string2 {0} I18n_altbase' -f $UICulture)
        }

        It 'Imports from the "I18n_altfilename" file name and "I18n_altbase" directory when both FileName and BaseDirectory are specified (<UICulture>)' -TestCases $testCultures {
            param ( $UICulture )

            [System.Globalization.CultureInfo]::CurrentUICulture = $UICulture

            Import-LocalizedData @defaultParams @fileName @baseDirectory -UICulture $UICulture

            $data.string1 | Should -BeExactly ('string1 {0} I18n_altbase I18n_altfilename' -f $UICulture)
            $data.string2 | Should -BeExactly ('string2 {0} I18n_altbase I18n_altfilename' -f $UICulture)
        }

        It 'Throws an error if the specified FileName does not exist in any path' {
            { Import-LocalizedData @defaultParams -FileName doesNotExist -ErrorAction Stop } |
                Should -Throw -ExceptionType 'System.Management.Automation.PSInvalidOperationException'
        }

        It 'Throws an error if the specified BaseDirectory does not exist' {
            { Import-LocalizedData @defaultParams -BaseDirectory "$PSScriptRoot\doesNotExist" -ErrorAction Stop } |
                Should -Throw -ExceptionType 'System.Management.Automation.PSInvalidOperationException'
        }
    }

    Context 'SupportedCommand parameter' {
        It 'Allows non-standard commands to be used in a data file' {
            $data = Import-LocalizedData -FileName I18n_supportedcommands -SupportedCommand Get-Command

            $data.Name | Should -Be 'Import-LocalizedData'
        }
    }
}