Tests/Set-AppKeyValue.Tests.ps1
Describe "Set-AppKeyValue" { function New-AppJsonFile { Param( # path to create the new file in [Parameter(Mandatory=$true)] [string] $Path ) Set-Content -Path (Join-Path $Path 'app.json') -Value '{"name": "test app", "publisher": "Acme Publishers", "brief": "this is a beautiful app"}' } Context "When setting the name key" { New-AppJsonFile -Path $TestDrive It "should maintain the original order of the keys" { Set-AppKeyValue -SourcePath $TestDrive -KeyName name -KeyValue 'Clever app names are hard to think of' $AppJson = ConvertFrom-Json (Get-Content (Join-Path $TestDrive 'app.json') -Raw) $PropertyNo = 1 foreach ($Property in $AppJson.PSObject.Properties) { switch ($PropertyNo) { 1 {$Property.Name | should be 'name'; $Property.Value | should be 'Clever app names are hard to think of'} 2 {$Property.Name | should be 'publisher'; $Property.Value | should be 'Acme Publishers'} 3 {$Property.Name | should be 'brief'; $Property.Value | should be 'this is a beautiful app'} } $PropertyNo++ } } } Context "When setting a key to blank" { New-AppJsonFile -Path $TestDrive It "should remove that key" { Set-AppKeyValue -SourcePath $TestDrive -KeyName brief -KeyValue '' $AppJson = ConvertFrom-Json (Get-Content (Join-Path $TestDrive 'app.json') -Raw) $AppJson.PSObject.Properties.Item('brief').Value | should be $null } } Context "When setting a key that doesn't exist" { New-AppJsonFile -Path $TestDrive It "should create the key" { Set-AppKeyValue -SourcePath $TestDrive -KeyName 'newkey' -KeyValue 'newvalue' $AppJson = ConvertFrom-Json (Get-Content (Join-Path $TestDrive 'app.json') -Raw) $AppJson.PSObject.Properties.Item('newkey').Value | should be 'newvalue' } } } |