{{- define "OperationType" -}}
{{- if .Apply -}}
apply
{{- else if .Assert -}}
assert
{{- else if .Command -}}
command
{{- else if .Create -}}
create
{{- else if .Delete -}}
delete
{{- else if .Error -}}
error
{{- else if .Patch -}}
patch
{{- else if .Script -}}
script
{{- else if .Sleep -}}
sleep
{{- else if .Update -}}
update
{{- else if .Wait -}}
wait
{{- else -}}
{{- fail (print "unknown operation " (toJson .)) -}}
{{- end -}}
{{- end -}}

{{- define "CatchType" -}}
{{- if .Command -}}
command
{{- else if .Delete -}}
delete
{{- else if .Describe -}}
describe
{{- else if .Events -}}
events
{{- else if .Get -}}
get
{{- else if .PodLogs -}}
pod logs
{{- else if .Script -}}
script
{{- else if .Sleep -}}
sleep
{{- else if .Wait -}}
wait
{{- else -}}
{{- fail (print "unknown catch " (toJson .)) -}}
{{- end -}}
{{- end -}}

{{- define "FinallyType" -}}
{{- if .Command -}}
command
{{- else if .Delete -}}
delete
{{- else if .Describe -}}
describe
{{- else if .Events -}}
events
{{- else if .Get -}}
get
{{- else if .PodLogs -}}
pod logs
{{- else if .Script -}}
script
{{- else if .Sleep -}}
sleep
{{- else if .Wait -}}
wait
{{- else -}}
{{- fail (print "unknown finally " (toJson .)) -}}
{{- end -}}
{{- end -}}

{{- range . }}
{{- $test := . -}}

# Test: `{{ .Name }}`

{{ default "*No description*" .Spec.Description }}

{{- with .Spec.Bindings }}

## Bindings

| # | Name | Value |
|:-:|---|---|
{{- range $i, $step := . }}
| {{ add $i 1 }} | `{{ .Name }}` | {{ toJson .Value.Value }} |
{{- end }}

{{- end }}

## Steps

{{- with .Spec.Steps }}

| # | Name | Bindings | Try | Catch | Finally | Cleanup |
|:-:|---|:-:|:-:|:-:|:-:|
{{- range $i, $step := . }}
{{- $name := default (print "step-" (add $i 1)) $step.Name }}
| {{ add $i 1 }} | [{{ $name }}](#step-{{ $name }}) | {{ len $step.Bindings }} | {{ len $step.Try }} | {{ add (len $step.Catch) (len $test.Spec.Catch) }} | {{ len $step.Finally }} | {{ len $step.Cleanup }} |
{{- end }}
{{- end }}

{{- range $i, $step := .Spec.Steps }}
{{- $name := default (print "step-" (add $i 1)) $step.Name }}

### Step: `{{ $name }}`

{{ default "*No description*" $step.Description }}

{{- with $step.Bindings }}

#### Bindings

| # | Name | Value |
|:-:|---|---|
{{- range $i, $step := . }}
| {{ add $i 1 }} | `{{ .Name }}` | {{ toJson .Value.Value }} |
{{- end }}

{{- end }}

{{-  with $step.Try }}

#### Try

| # | Operation | Bindings | Outputs | Description |
|:-:|---|:-:|:-:|---|
{{- range $i, $op := . }}
| {{ add $i 1 }} | `{{ template "OperationType" $op }}` | {{ len $op.Bindings }} | {{ len $op.Outputs }} | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- end }}

{{-  with $step.Catch }}

#### Catch

| # | Operation | Bindings | Outputs | Description |
|:-:|---|:-:|:-:|---|
{{- range $i, $op := $test.Spec.Catch }}
| {{ add $i 1 }} | `{{ template "CatchType" $op }}` | {{ len $op.Bindings }} | {{ len $op.Outputs }} | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- range $i, $op := . }}
| {{ add (len $test.Spec.Catch) $i 1 }} | `{{ template "CatchType" $op }}` | {{ len $op.Bindings }} | {{ len $op.Outputs }} | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- end }}

{{- with $step.Finally }}

#### Finally

| # | Operation | Bindings | Outputs | Description |
|:-:|---|:-:|:-:|---|
{{- range $i, $op := . }}
| {{ add $i 1 }} | `{{ template "FinallyType" $op }}` | {{ len $op.Bindings }} | {{ len $op.Outputs }} | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- end }}

{{- with $step.Cleanup }}

#### Cleanup

| # | Operation | Bindings | Outputs | Description |
|:-:|---|:-:|:-:|---|
{{- range $i, $op := . }}
| {{ add $i 1 }} | `{{ template "FinallyType" $op }}` | {{ len $op.Bindings }} | {{ len $op.Outputs }} | {{ default "*No description*" $op.Description }} |
{{- end }}
{{- end }}

{{- end }}

---

{{ end -}}
