fixed issue with implementation and tests
parent
4889a1654c
commit
129e751a44
|
@ -8,83 +8,83 @@ $ErrorActionPreference = "Stop"
|
|||
."$PSScriptRoot\types.ps1"
|
||||
|
||||
function writePending($timestamp, $description) {
|
||||
$symbol = " "
|
||||
$color = "Yellow"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host $message -ForegroundColor $color -NoNewline
|
||||
$symbol = " "
|
||||
$color = "Yellow"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host $message -ForegroundColor $color -NoNewline
|
||||
}
|
||||
|
||||
function writeSuccess($timestamp, $description, $clearString) {
|
||||
$symbol = [char]8730
|
||||
$color = "Green"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host "`r$clearString" -NoNewline
|
||||
Write-Host "`r$message" -ForegroundColor $color
|
||||
$symbol = [char]8730
|
||||
$color = "Green"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host "`r$clearString" -NoNewline
|
||||
Write-Host "`r$message" -ForegroundColor $color
|
||||
}
|
||||
|
||||
function writeFail($timestamp, $description, $clearString) {
|
||||
$symbol = "X"
|
||||
$color = "Red"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host "`r$clearString" -NoNewline
|
||||
Write-Host "`n$message`n" -ForegroundColor $color
|
||||
exit -1
|
||||
$symbol = "X"
|
||||
$color = "Red"
|
||||
$message = "$timestamp [ $symbol ] $description"
|
||||
Write-Host "`r$clearString" -NoNewline
|
||||
Write-Host "`n$message`n" -ForegroundColor $color
|
||||
exit -1
|
||||
}
|
||||
|
||||
$fsm = @{
|
||||
"Test Test Start $false" = {
|
||||
writePending @args
|
||||
@{
|
||||
"Test Test Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"Test Test Stop $false" = {
|
||||
writeFail @args
|
||||
}
|
||||
}
|
||||
"Test Test Start $false" = {
|
||||
writePending @args
|
||||
@{
|
||||
"Test Test Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"Test Test Stop $false" = {
|
||||
writeFail @args
|
||||
}
|
||||
}
|
||||
"Set Set Start $false" = {
|
||||
writePending @args
|
||||
@{
|
||||
"Set Set Stop $false" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
}
|
||||
}
|
||||
"Set Set Start *" = {
|
||||
writePending @args
|
||||
@{
|
||||
"Set Set Stop *" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
}
|
||||
"TestSet Test Start $false" = {
|
||||
writePending @args
|
||||
}
|
||||
"TestSet Test Start $false" = {
|
||||
writePending @args
|
||||
@{
|
||||
"TestSet Test Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"TestSet Test Stop $false" = {
|
||||
@{
|
||||
"TestSet Test Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"TestSet Test Stop $false" = {
|
||||
"TestSet Set Start *" = {
|
||||
@{
|
||||
"TestSet Set Stop *" = {
|
||||
@{
|
||||
"TestSet Set Start $false" = {
|
||||
@{
|
||||
"TestSet Set Stop $false" = {
|
||||
@{
|
||||
"TestSet Validate Start $false" = {
|
||||
@{
|
||||
"TestSet Validate Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"TestSet Validate Stop $false" = {
|
||||
writeFail @args
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"TestSet Validate Start $false" = {
|
||||
@{
|
||||
"TestSet Validate Stop $true" = {
|
||||
writeSuccess @args
|
||||
$fsm
|
||||
}
|
||||
"TestSet Validate Stop $false" = {
|
||||
writeFail @args
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
|
@ -94,46 +94,46 @@ $fsm = @{
|
|||
Uses Write-Host
|
||||
#>
|
||||
function Format-Checklist {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
# Logged Requirement lifecycle events
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[Alias("Event")]
|
||||
[RequirementEvent[]]$RequirementEvent
|
||||
)
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
# Logged Requirement lifecycle events
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[Alias("Event")]
|
||||
[RequirementEvent[]]$RequirementEvent
|
||||
)
|
||||
|
||||
begin {
|
||||
$previousRequirement = $null
|
||||
$nextFsm = $fsm
|
||||
}
|
||||
begin {
|
||||
$previousRequirement = $null
|
||||
$nextFsm = $fsm
|
||||
}
|
||||
|
||||
process {
|
||||
$requirement = $_.Requirement
|
||||
process {
|
||||
$requirement = $_.Requirement
|
||||
|
||||
# build state vector
|
||||
$requirementType = ("Test", "Set" | ? { $requirement.$_ }) -join ""
|
||||
$method = $_.Method
|
||||
$lifecycleState = $_.State
|
||||
$successResult = [bool]$_.Result
|
||||
$stateVector = "$requirementType $method $lifecycleState $successResult"
|
||||
# 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 transition arguments
|
||||
$timestamp = Get-Date -Date $_.Date -Format "hh:mm:ss"
|
||||
$description = $requirement.Describe
|
||||
$clearString = ' ' * "??:??:?? [ ? ] $($previousRequirement.Describe)".Length
|
||||
$transitionArgs = @($timestamp, $description, $clearString)
|
||||
# build transition arguments
|
||||
$timestamp = Get-Date -Date $_.Date -Format "hh:mm:ss"
|
||||
$description = $requirement.Describe
|
||||
$clearString = ' ' * "??:??:?? [ ? ] $($previousRequirement.Describe)".Length
|
||||
$transitionArgs = @($timestamp, $description, $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
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
|
@ -143,79 +143,79 @@ cmdlet, then this is probably a bug in Format-Checklist.
|
|||
Uses Write-Host
|
||||
#>
|
||||
function Format-CallStack {
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
# Logged Requirement lifecycle events
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[Alias("Event")]
|
||||
[RequirementEvent[]]$RequirementEvent
|
||||
)
|
||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")]
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
# Logged Requirement lifecycle events
|
||||
[Parameter(Mandatory, ValueFromPipeline)]
|
||||
[Alias("Event")]
|
||||
[RequirementEvent[]]$RequirementEvent
|
||||
)
|
||||
|
||||
begin {
|
||||
$context = [Stack[string]]::new()
|
||||
}
|
||||
begin {
|
||||
$context = [Stack[string]]::new()
|
||||
}
|
||||
|
||||
process {
|
||||
$timestamp = Get-Date -Date $_.Date -Format 'hh:mm:ss'
|
||||
$name = $_.Requirement.Name
|
||||
$description = $_.Requirement.Describe
|
||||
$method, $state, $result = $_.Method, $_.State, $_.Result
|
||||
switch ($method) {
|
||||
"Test" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN TEST $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END TEST => $result"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
"Set" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN SET $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END SET"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
"Validate" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN TEST $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END TEST => $result"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
process {
|
||||
$timestamp = Get-Date -Date $_.Date -Format 'hh:mm:ss'
|
||||
$name = $_.Requirement.Name
|
||||
$description = $_.Requirement.Describe
|
||||
$method, $state, $result = $_.Method, $_.State, $_.Result
|
||||
switch ($method) {
|
||||
"Test" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN TEST $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END TEST => $result"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
"Set" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN SET $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END SET"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
"Validate" {
|
||||
switch ($state) {
|
||||
"Start" {
|
||||
$context.Push($name)
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] BEGIN TEST $description"
|
||||
}
|
||||
"Stop" {
|
||||
$callstack = $context.ToArray()
|
||||
[array]::Reverse($callstack)
|
||||
$serialized = $callstack -join ">"
|
||||
Write-Host "$timestamp [$serialized] END TEST => $result"
|
||||
$context.Pop() | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,50 +6,50 @@ $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", $null)
|
||||
[RequirementEvent]::new($Requirement, "Validate", "Start")
|
||||
[RequirementEvent]::new($Requirement, "Validate", "Stop", $true)
|
||||
[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" {
|
||||
Mock Get-Date { return "00:00:00" }
|
||||
$script:InDesiredState = 0
|
||||
$requirement = @{
|
||||
Name = "simple-requirement"
|
||||
Describe = "Simple Requirement"
|
||||
Test = { $script:InDesiredState++ }
|
||||
Set = { }
|
||||
Mock Get-Date { return "00:00:00" }
|
||||
$script:InDesiredState = 0
|
||||
$requirement = @{
|
||||
Name = "simple-requirement"
|
||||
Describe = "Simple Requirement"
|
||||
Test = { $script:InDesiredState++ }
|
||||
Set = { }
|
||||
}
|
||||
$events = invoke $requirement
|
||||
$tempContainer = $PSScriptRoot
|
||||
Context "Format-Table" {
|
||||
$output = $events | Format-Table | Out-String
|
||||
It "Should print a non-empty string" {
|
||||
$output.Trim().Length | Should -BeGreaterThan 10
|
||||
}
|
||||
$events = invoke $requirement
|
||||
$tempContainer = $PSScriptRoot
|
||||
Context "Format-Table" {
|
||||
$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
|
||||
It "Should format each line as a checklist" {
|
||||
$output | Should -Match "^\d\d:\d\d:\d\d \[ . \] Simple Requirement"
|
||||
}
|
||||
Context "Format-Checklist" {
|
||||
$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 \[ . \] Simple Requirement"
|
||||
}
|
||||
}
|
||||
Context "Format-Callstack" {
|
||||
$path = "$tempContainer\$(New-Guid).txt"
|
||||
($events | Format-CallStack) *> $path
|
||||
$output = Get-Content $path
|
||||
Remove-Item $path
|
||||
It "Should format each line as a callstack" {
|
||||
$output | % { $_ | Should -Match "^\d\d:\d\d:\d\d \[.+\] .+" }
|
||||
}
|
||||
Context "Format-Callstack" {
|
||||
$path = "$tempContainer\$(New-Guid).txt"
|
||||
$events | Format-CallStack *> $path
|
||||
$output = Get-Content $path
|
||||
Remove-Item $path
|
||||
It "Should format each line as a callstack" {
|
||||
$output | % { $_ | Should -Match "^\d\d:\d\d:\d\d \[.+\] .+" }
|
||||
}
|
||||
It "Should print 6 lines" {
|
||||
$output.Count | Should -Be 6
|
||||
}
|
||||
It "Should print 6 lines" {
|
||||
$output.Count | Should -Be 6
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue