/
report.go
157 lines (124 loc) · 2.94 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package spec
import (
"fmt"
"net/http/httptest"
"net/url"
"testing"
"github.com/abdullin/omni/core"
"github.com/abdullin/omni/core/env"
)
// Scenario result
type Result struct {
UseCase *env.UseCase
Events []core.Event
ResponseRaw *httptest.ResponseRecorder
Response *env.Response
Issues []string
}
func (r *Result) Ok() bool {
return len(r.Issues) == 0
}
type Report struct {
Insanity []string
Resuls []*Result
}
func NewReport() *Report {
return &Report{}
}
func (r *Report) failSanity(s string, args ...interface{}) {
r.Insanity = append(r.Insanity, fmt.Sprintf(s, args...))
}
func (r *Report) addInsanities(insanities []string) {
r.Insanity = append(r.Insanity, insanities...)
}
func prettyPrintEvent(e core.Event) string {
if e == nil {
return fmt.Sprintf("%T<nil>", e)
}
if s, ok := e.(fmt.Stringer); ok {
return fmt.Sprintf("%s", s.String())
} else {
return fmt.Sprintf("%s %s", e.Meta().Contract, string(marshalIndent(e)))
}
}
func (r *Report) ToTesting(t *testing.T) {
fmt.Println("================== Module Use-Cases ===================\n")
for _, x := range r.Insanity {
t.Fail()
fmt.Println("☹", x)
}
specFailed := false
for _, r := range r.Resuls {
if r.Ok() {
fmt.Println("♥", r.UseCase.Name)
} else {
specFailed = true
t.Fail()
fmt.Println("✗", r.UseCase.Name)
}
}
if specFailed {
fmt.Println("\n============= Failure Details (TDD Mode) ==============\n")
for _, r := range r.Resuls {
if !r.Ok() {
fmt.Println("✗", r.UseCase.Name, "\n")
printDetail(r)
fmt.Println("-------------------------------------------------------\n")
}
}
}
}
func printDetail(r *Result) {
if len(r.UseCase.Given) > 0 {
fmt.Println("Given_events:")
for i, e := range r.UseCase.Given {
fmt.Printf("%v. %s\n", i+1, prettyPrintEvent(e))
}
}
if r.UseCase.When != nil {
when := r.UseCase.When
uri, err := url.Parse(when.Path)
guard("url.Parse", err)
fmt.Println("When_request:", when.Method, uri.Path)
query := uri.Query()
if len(query) > 0 {
fmt.Println(" with")
for k, _ := range query {
fmt.Printf(" %s = '%s'\n", k, query.Get(k))
}
}
}
if resp := r.UseCase.ThenResponse; resp != nil {
fmt.Printf("Expect_HTTP: ")
printResponse(resp)
}
if resp := r.Response; resp != nil {
fmt.Printf("Actual_HTTP: ")
printResponse(resp)
}
if es := r.UseCase.ThenEvents; es != nil {
fmt.Println("Expect_Events:", len(es))
for i, e := range es {
fmt.Printf("%v. %s\n", i, prettyPrintEvent(e))
}
}
if es := r.Events; es != nil {
fmt.Println("Actual_Events:", len(es))
for i, e := range es {
fmt.Printf("%v. %s\n", i+1, prettyPrintEvent(e))
}
}
if len(r.Issues) > 0 {
fmt.Println("Issues_to_fix:")
for i, x := range r.Issues {
fmt.Printf("%v. %s\n", i+1, x)
}
}
}
func printResponse(resp *env.Response) {
body := ""
if resp.Body != nil {
body = (string(marshalIndent(resp.Body)))
}
fmt.Println(resp.Status, body)
}