From 40d83d924a084f15edc137aebfe7efce76a2b38d Mon Sep 17 00:00:00 2001 From: Richard Kuhnt Date: Mon, 18 Mar 2019 17:24:18 +0100 Subject: [PATCH] refactor(config): Add more tests for load_config and get_config --- test/Scoop-Config.Tests.ps1 | 50 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/test/Scoop-Config.Tests.ps1 b/test/Scoop-Config.Tests.ps1 index 812a9efd4..fd90b2303 100644 --- a/test/Scoop-Config.Tests.ps1 +++ b/test/Scoop-Config.Tests.ps1 @@ -1,19 +1,45 @@ . "$psscriptroot\..\lib\core.ps1" -describe "hashtable" -Tag 'Scoop' { - $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+01:00" }' +describe "config" -Tag 'Scoop' { + BeforeAll { + $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+01:00" }' + } - it "converts pscustomobject to hashtable" { + it "converts JSON to PSObject" { $obj = ConvertFrom-Json $json - $obj.one | should -beexactly 1 - $obj.two[0].a | should -be "a" - $obj.two[1] | should -be "b" - $obj.two[2] | should -beexactly 2 - $obj.three.four | should -beexactly 4 - $obj.five | should -beexactly $true - $obj.six | should -beexactly $false - [System.DateTime]::Equals($obj.seven, $(New-Object System.DateTime (2018, 06, 25, 09, 03, 15, 805))) | should -betrue - [System.DateTime]::Equals([System.DateTime]::parse($obj.eight), $(New-Object System.DateTime (2019, 03, 18, 15, 22, 09, 393))) | should -betrue + $obj.one | Should -BeExactly 1 + $obj.two[0].a | Should -Be "a" + $obj.two[1] | Should -Be "b" + $obj.two[2] | Should -BeExactly 2 + $obj.three.four | Should -BeExactly 4 + $obj.five | Should -BeTrue + $obj.six | Should -BeFalse + [System.DateTime]::Equals($obj.seven, $(New-Object System.DateTime (2018, 06, 25, 09, 03, 15, 805))) | Should -BeTrue + [System.DateTime]::Equals([System.DateTime]::parse($obj.eight), $(New-Object System.DateTime (2019, 03, 18, 15, 22, 09, 393))) | Should -BeTrue + } + + it "load_config should return PSObject" { + mock Get-Content { $json } + mock Test-Path { $true } + (load_cfg 'file') | Should -Not -BeNullOrEmpty + (load_cfg 'file') | Should -BeOfType System.Management.Automation.PSObject + (load_cfg 'file').one | Should -BeExactly 1 + + } + + it "get_config should return exactly the same values" { + $scoopConfig = ConvertFrom-Json $json + get_config 'does_not_exist' 'default' | Should -Be 'default' + + get_config 'one' | Should -BeExactly 1 + (get_config 'two')[0].a | Should -Be "a" + (get_config 'two')[1] | Should -Be "b" + (get_config 'two')[2] | Should -BeExactly 2 + (get_config 'three').four | Should -BeExactly 4 + get_config 'five' | Should -BeTrue + get_config 'six' | Should -BeFalse + [System.DateTime]::Equals((get_config 'seven'), $(New-Object System.DateTime (2018, 06, 25, 09, 03, 15, 805))) | Should -BeTrue + [System.DateTime]::Equals([System.DateTime]::parse((get_config 'eight')), $(New-Object System.DateTime (2019, 03, 18, 15, 22, 09, 393))) | Should -BeTrue } }