diff --git a/src/formatters.ps1 b/src/formatters.ps1 index e4111ae..2aeb003 100644 --- a/src/formatters.ps1 +++ b/src/formatters.ps1 @@ -96,7 +96,7 @@ function Format-Checklist { [CmdletBinding()] Param( # Logged Requirement lifecycle events - [Parameter(Mandatory, ValueFromPipeline)] + [Parameter(Mandatory, ValueFromPipeline, Position = 0)] [Alias("Event")] [RequirementEvent[]]$RequirementEvent ) @@ -107,30 +107,32 @@ function Format-Checklist { } process { - $requirement = $_.Requirement + foreach ($event in $RequirementEvent) { + $requirement = $event.Requirement - # build state vector - $requirementType = ("Test", "Set" | ? { $requirement.$_ }) -join "" - $method = $_.Method - $lifecycleState = $_.State - $successResult = if ($method -eq "Set") { "*" } else { [bool]$_.Result } - $stateVector = "$requirementType $method $lifecycleState $successResult" + # build state vector + $requirementType = ("Test", "Set" | Where-Object { $requirement.$_ }) -join "" + $method = $event.Method + $lifecycleState = $event.State + $successResult = if ($method -eq "Set") { "*" } else { [bool]$event.Result } + $stateVector = "$requirementType $method $lifecycleState $successResult" - # build transition arguments - $timestamp = Get-Date -Date $_.Date -Format "hh:mm:ss" - $clearString = ' ' * "? ??:??:?? $previousRequirement".Length - $transitionArgs = @($timestamp, $requirement, $clearString) + # build transition arguments + $timestamp = Get-Date -Date $event.Date -Format "hh:mm:ss" + $clearString = ' ' * "? ??:??:?? $previousRequirement".Length + $transitionArgs = @($timestamp, $requirement, $clearString) - # transition FSM - if (-not $nextFsm[$stateVector]) { - throw @" + # transition FSM + if (-not $nextFsm[$stateVector]) { + throw @" Format-Checklist has reached an unexpected state '$stateVector'. If you are piping the output of Invoke-Requirement directly to this cmdlet, then this is probably a bug in Format-Checklist. "@ + } + $nextFsm = &$nextFsm[$stateVector] @transitionArgs + $previousRequirement = $requirement } - $nextFsm = &$nextFsm[$stateVector] @transitionArgs - $previousRequirement = $requirement } } diff --git a/test/formatters.tests.ps1 b/test/formatters.tests.ps1 index a9186fa..00b400c 100644 --- a/test/formatters.tests.ps1 +++ b/test/formatters.tests.ps1 @@ -1,49 +1,78 @@ +BeforeAll { -$ErrorActionPreference = "Stop" + $ErrorActionPreference = "Stop" -$RepoRoot = "$PSScriptRoot/.." -$SourceRoot = "$RepoRoot/src" -."$SourceRoot\formatters.ps1" + $RepoRoot = "$PSScriptRoot/.." + $SourceRoot = "$RepoRoot/src" + ."$SourceRoot\formatters.ps1" + function invoke($Requirement) { + [RequirementEvent]::new($Requirement, "Test", "Start") + [RequirementEvent]::new($Requirement, "Test", "Stop", $false) + [RequirementEvent]::new($Requirement, "Set", "Start") + [RequirementEvent]::new($Requirement, "Set", "Stop", $true) + [RequirementEvent]::new($Requirement, "Validate", "Start") + [RequirementEvent]::new($Requirement, "Validate", "Stop", $true) + } -function invoke($Requirement) { - [RequirementEvent]::new($Requirement, "Test", "Start") - [RequirementEvent]::new($Requirement, "Test", "Stop", $false) - [RequirementEvent]::new($Requirement, "Set", "Start") - [RequirementEvent]::new($Requirement, "Set", "Stop", $true) - [RequirementEvent]::new($Requirement, "Validate", "Start") - [RequirementEvent]::new($Requirement, "Validate", "Stop", $true) } Describe "formatters" { - $script:InDesiredState = 0 - $requirement = @{ - Namespace = "sr" - Describe = "Simple Requirement" - Test = { $script:InDesiredState++ } - Set = { } + + BeforeAll { + $script:InDesiredState = 0 + $requirement = @{ + Namespace = "sr" + Describe = "Simple Requirement" + Test = { $script:InDesiredState++ } + Set = { } + } + $events = invoke $requirement + $tempContainer = "TestDrive:\" } - $events = invoke $requirement - $tempContainer = $PSScriptRoot + Context "Format-Table" { - $output = $events | Format-Table | Out-String + BeforeAll { + $output = $events | Format-Table | Out-String + } + It "Should print a non-empty string" { $output.Trim().Length | Should -BeGreaterThan 10 } } - Context "Format-Checklist" { - $path = "$tempContainer\$(New-Guid).txt" - ($events | Format-Checklist) *> $path - $output = Get-Content $path -Raw - Remove-Item $path + Context "Format-Checklist via Pipeline" { + BeforeAll { + $path = Join-Path $tempContainer "$(New-Guid).txt" + ($events | Format-Checklist) *> $path + $output = Get-Content $path -Raw + Remove-Item $path + } + + It "Should format each line as a checklist" { + $output | Should -Match "^. \d\d:\d\d:\d\d\[sr|Simple Requirement" + } + } + + Context "Format-Checklist via Parameter" { + BeforeAll { + $path = Join-Path $tempContainer "$(New-Guid).txt" + (Format-Checklist $events) *> $path + $output = Get-Content $path -Raw + Remove-Item $path + } + It "Should format each line as a checklist" { $output | Should -Match "^. \d\d:\d\d:\d\d\[sr|Simple Requirement" } } + Context "Format-Verbose" { - $path = "$tempContainer\$(New-Guid).txt" - ($events | Format-Verbose) *> $path - $output = Get-Content $path - Remove-Item $path + BeforeAll { + $path = "$tempContainer\$(New-Guid).txt" + ($events | Format-Verbose) *> $path + $output = Get-Content $path + Remove-Item $path + } + It "Should format each line" { $output | % { $_ | Should -Match "^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d +\w+ +\w+ .+" } }