Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Provide Format-Checklist parameter support #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/formatters.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
}
}

Expand Down
87 changes: 58 additions & 29 deletions test/formatters.tests.ps1
Original file line number Diff line number Diff line change
@@ -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+ .+" }
}
Expand Down