|
2 | 2 |
|
3 | 3 | Describe 'config' -Tag 'Scoop' { |
4 | 4 | BeforeAll { |
5 | | - $json = '{ "one": 1, "two": [ { "a": "a" }, "b", 2 ], "three": { "four": 4 }, "five": true, "six": false, "seven": "\/Date(1529917395805)\/", "eight": "2019-03-18T15:22:09.3930000+00:00" }' |
| 5 | + $configFile = "$env:TEMP\ScoopTestFixtures\config.json" |
| 6 | + if (Test-Path $configFile) { |
| 7 | + Remove-Item -Path $configFile -Force |
| 8 | + } |
| 9 | + $unicode = [Regex]::Unescape('\u4f60\u597d\u3053\u3093\u306b\u3061\u306f') # 你好こんにちは |
6 | 10 | } |
7 | 11 |
|
8 | | - It 'converts JSON to PSObject' { |
9 | | - $obj = ConvertFrom-Json $json |
10 | | - |
11 | | - $obj.one | Should -BeExactly 1 |
12 | | - $obj.two[0].a | Should -Be 'a' |
13 | | - $obj.two[1] | Should -Be 'b' |
14 | | - $obj.two[2] | Should -BeExactly 2 |
15 | | - $obj.three.four | Should -BeExactly 4 |
16 | | - $obj.five | Should -BeTrue |
17 | | - $obj.six | Should -BeFalse |
18 | | - $obj.seven | Should -BeOfType [System.DateTime] |
19 | | - if ($PSVersionTable.PSVersion.Major -lt 6) { |
20 | | - $obj.eight | Should -BeOfType [System.String] |
21 | | - } else { |
22 | | - $obj.eight | Should -BeOfType [System.DateTime] |
23 | | - } |
| 12 | + BeforeEach { |
| 13 | + $scoopConfig = $null |
24 | 14 | } |
25 | 15 |
|
26 | | - It 'load_config should return PSObject' { |
27 | | - Mock Get-Content { $json } |
28 | | - Mock Test-Path { $true } |
29 | | - (load_cfg 'file') | Should -Not -BeNullOrEmpty |
30 | | - (load_cfg 'file') | Should -BeOfType [System.Management.Automation.PSObject] |
31 | | - (load_cfg 'file').one | Should -BeExactly 1 |
| 16 | + It 'load_cfg should return null if config file does not exist' { |
| 17 | + load_cfg $configFile | Should -Be $null |
32 | 18 | } |
33 | 19 |
|
34 | | - It 'get_config should return exactly the same values' { |
35 | | - $scoopConfig = ConvertFrom-Json $json |
36 | | - get_config 'does_not_exist' 'default' | Should -Be 'default' |
| 20 | + It 'set_config should be able to save typed values correctly' { |
| 21 | + # number |
| 22 | + $scoopConfig = set_config 'one' 1 |
| 23 | + $scoopConfig.one | Should -BeExactly 1 |
37 | 24 |
|
38 | | - get_config 'one' | Should -BeExactly 1 |
39 | | - (get_config 'two')[0].a | Should -Be 'a' |
40 | | - (get_config 'two')[1] | Should -Be 'b' |
41 | | - (get_config 'two')[2] | Should -BeExactly 2 |
42 | | - (get_config 'three').four | Should -BeExactly 4 |
43 | | - get_config 'five' | Should -BeTrue |
44 | | - get_config 'six' | Should -BeFalse |
45 | | - get_config 'seven' | Should -BeOfType [System.DateTime] |
46 | | - if ($PSVersionTable.PSVersion.Major -lt 6) { |
47 | | - get_config 'eight' | Should -BeOfType [System.String] |
48 | | - } else { |
49 | | - get_config 'eight' | Should -BeOfType [System.DateTime] |
50 | | - } |
51 | | - } |
| 25 | + # boolean |
| 26 | + $scoopConfig = set_config 'two' $true |
| 27 | + $scoopConfig.two | Should -BeTrue |
| 28 | + $scoopConfig = set_config 'three' $false |
| 29 | + $scoopConfig.three | Should -BeFalse |
52 | 30 |
|
53 | | - It 'set_config should create a new PSObject and ensure existing directory' { |
54 | | - $scoopConfig = $null |
55 | | - $configFile = "$PSScriptRoot\.scoop" |
| 31 | + # underline key |
| 32 | + $scoopConfig = set_config 'under_line' 'four' |
| 33 | + $scoopConfig.under_line | Should -BeExactly 'four' |
56 | 34 |
|
57 | | - Mock ensure { $PSScriptRoot } -Verifiable -ParameterFilter { $dir -eq (Split-Path -Path $configFile) } |
58 | | - Mock Set-Content {} -Verifiable -ParameterFilter { $Path -eq $configFile } |
59 | | - Mock ConvertTo-Json { '' } -Verifiable -ParameterFilter { $InputObject -is [System.Management.Automation.PSObject] } |
| 35 | + # string |
| 36 | + $scoopConfig = set_config 'five' 'not null' |
60 | 37 |
|
61 | | - set_config 'does_not_exist' 'default' |
| 38 | + # datetime |
| 39 | + $scoopConfig = set_config 'time' ([System.DateTime]::Parse('2019-03-18T15:22:09.3930000+00:00', $null, [System.Globalization.DateTimeStyles]::AdjustToUniversal)) |
| 40 | + $scoopConfig.time | Should -BeOfType [System.DateTime] |
62 | 41 |
|
63 | | - Assert-VerifiableMock |
| 42 | + # non-ASCII |
| 43 | + $scoopConfig = set_config 'unicode' $unicode |
| 44 | + $scoopConfig.unicode | Should -Be $unicode |
64 | 45 | } |
65 | 46 |
|
66 | | - It "set_config should remove a value if set to `$null" { |
67 | | - $scoopConfig = New-Object PSObject |
68 | | - $scoopConfig | Add-Member -MemberType NoteProperty -Name 'should_be_removed' -Value 'a_value' |
69 | | - $scoopConfig | Add-Member -MemberType NoteProperty -Name 'should_stay' -Value 'another_value' |
70 | | - $configFile = "$PSScriptRoot\.scoop" |
71 | | - |
72 | | - Mock Set-Content {} -Verifiable -ParameterFilter { $Path -eq $configFile } |
73 | | - Mock ConvertTo-Json { '' } -Verifiable -ParameterFilter { $InputObject -is [System.Management.Automation.PSObject] } |
| 47 | + It 'load_cfg should return PSObject if config file exist' { |
| 48 | + $scoopConfig = load_cfg $configFile |
| 49 | + $scoopConfig | Should -Not -BeNullOrEmpty |
| 50 | + $scoopConfig | Should -BeOfType [System.Management.Automation.PSObject] |
| 51 | + $scoopConfig.one | Should -BeExactly 1 |
| 52 | + $scoopConfig.two | Should -BeTrue |
| 53 | + $scoopConfig.three | Should -BeFalse |
| 54 | + $scoopConfig.under_line | Should -BeExactly 'four' |
| 55 | + $scoopConfig.five | Should -Be 'not null' |
| 56 | + $scoopConfig.time | Should -BeOfType [System.DateTime] |
| 57 | + $scoopConfig.time | Should -Be ([System.DateTime]::Parse('2019-03-18T15:22:09.3930000+00:00', $null, [System.Globalization.DateTimeStyles]::AdjustToUniversal)) |
| 58 | + $scoopConfig.unicode | Should -Be $unicode |
| 59 | + } |
74 | 60 |
|
75 | | - $scoopConfig = set_config 'should_be_removed' $null |
76 | | - $scoopConfig.should_be_removed | Should -BeNullOrEmpty |
77 | | - $scoopConfig.should_stay | Should -Be 'another_value' |
| 61 | + It 'get_config should return exactly the same values' { |
| 62 | + $scoopConfig = load_cfg $configFile |
| 63 | + (get_config 'one') | Should -BeExactly 1 |
| 64 | + (get_config 'two') | Should -BeTrue |
| 65 | + (get_config 'three') | Should -BeFalse |
| 66 | + (get_config 'under_line') | Should -BeExactly 'four' |
| 67 | + (get_config 'five') | Should -Be 'not null' |
| 68 | + (get_config 'time') | Should -BeOfType [System.DateTime] |
| 69 | + (get_config 'time') | Should -Be ([System.DateTime]::Parse('2019-03-18T15:22:09.3930000+00:00', $null, [System.Globalization.DateTimeStyles]::AdjustToUniversal)) |
| 70 | + (get_config 'unicode') | Should -Be $unicode |
| 71 | + } |
78 | 72 |
|
79 | | - Assert-VerifiableMock |
| 73 | + It 'set_config should remove a value if being set to $null' { |
| 74 | + $scoopConfig = load_cfg $configFile |
| 75 | + $scoopConfig = set_config 'five' $null |
| 76 | + $scoopConfig.five | Should -BeNullOrEmpty |
80 | 77 | } |
81 | 78 | } |
0 commit comments