File: reporting_test.go

package info (click to toggle)
golang-github-franela-goblin 0.0.1%2Bgit20211003.62.0a4f594-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: makefile: 4
file content (199 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package goblin

import (
	"reflect"
	"sync"
	"testing"
	"time"
)

type FakeReporter struct {
	describes          []string
	fails              []string
	passes             []string
	pending            []string
	excluded           []string
	ends               int
	failures           int
	executionTime      time.Duration
	totalExecutionTime time.Duration
	executionTimeMu    sync.RWMutex
	beginFlag, endFlag bool
}

func (r *FakeReporter) BeginDescribe(name string) {
	r.describes = append(r.describes, name)
}

func (r *FakeReporter) EndDescribe() {
	r.ends++
}

func (r *FakeReporter) Failure(failure *Failure) {
	r.failures++
}

func (r *FakeReporter) ItFailed(name string) {
	r.fails = append(r.fails, name)
}

func (r *FakeReporter) ItPassed(name string) {
	r.passes = append(r.passes, name)
}

func (r *FakeReporter) ItIsPending(name string) {
	r.pending = append(r.pending, name)
}

func (r *FakeReporter) ItIsExcluded(name string) {
	r.excluded = append(r.excluded, name)
}

func (r *FakeReporter) ItTook(duration time.Duration) {
	r.executionTimeMu.Lock()
	defer r.executionTimeMu.Unlock()
	r.executionTime = duration
	r.totalExecutionTime += duration
}

func (r *FakeReporter) Begin() {
	r.beginFlag = true
}

func (r *FakeReporter) End() {
	r.endFlag = true
}

func TestReporting(t *testing.T) {
	fakeTest := &testing.T{}
	reporter := FakeReporter{}
	fakeReporter := Reporter(&reporter)

	g := Goblin(fakeTest)
	g.SetReporter(fakeReporter)

	g.Describe("One", func() {
		g.It("Foo", func() {
			g.Assert(0).Equal(1)
		})
		g.Describe("Two", func() {
			g.It("Bar", func() {
				g.Assert(0).Equal(0)
			})
		})
	})

	if !reflect.DeepEqual(reporter.describes, []string{"One", "Two"}) {
		t.FailNow()
	}
	if !reflect.DeepEqual(reporter.fails, []string{"Foo"}) {
		t.FailNow()
	}
	if !reflect.DeepEqual(reporter.passes, []string{"Bar"}) {
		t.FailNow()
	}
	if reporter.ends != 2 {
		t.FailNow()
	}

	if !reporter.beginFlag || !reporter.endFlag {
		t.FailNow()
	}
}

func TestReportingTime(t *testing.T) {
	fakeTest := &testing.T{}
	reporter := FakeReporter{}
	fakeReporter := Reporter(&reporter)

	g := Goblin(fakeTest)
	g.SetReporter(fakeReporter)

	g.Describe("One", func() {
		g.AfterEach(func() {
			testTime := int64(reporter.executionTime / time.Millisecond)
			if testTime < 5 || testTime > 6 {
				t.Fatalf("wrong execution time: %d", testTime)
			}
		})
		g.It("Foo", func() {
			time.Sleep(5 * time.Millisecond)
		})
		g.Describe("Two", func() {
			g.It("Bar", func() {
				time.Sleep(5 * time.Millisecond)
			})
		})
	})

	testTime := int64(reporter.executionTime / time.Millisecond)
	reporter.executionTimeMu.RLock()
	defer reporter.executionTimeMu.RUnlock()
	if int64(reporter.totalExecutionTime/time.Millisecond) < 10 {
		t.Fatalf("wrong execution time: %d", testTime)
	}
}

func TestReportingPending(t *testing.T) {
	fakeTest := &testing.T{}
	reporter := FakeReporter{}
	fakeReporter := Reporter(&reporter)

	g := Goblin(fakeTest)
	g.SetReporter(fakeReporter)

	g.Describe("One", func() {
		g.It("One")
		g.Describe("Two", func() {
			g.It("Two")
		})
	})

	if !reflect.DeepEqual(reporter.pending, []string{"One", "Two"}) {
		t.FailNow()
	}
}

func TestReportingExcluded(t *testing.T) {
	fakeTest := &testing.T{}
	reporter := FakeReporter{}
	fakeReporter := Reporter(&reporter)

	g := Goblin(fakeTest)
	g.SetReporter(fakeReporter)

	g.Describe("One", func() {
		g.Xit("One", func() {
			g.Assert(1).Equal(1)
		})
		g.Describe("Two", func() {
			g.Xit("Two", func() {
				g.Assert(2).Equal(2)
			})
		})
	})

	if !reflect.DeepEqual(reporter.excluded, []string{"One", "Two"}) {
		t.FailNow()
	}
}

func TestReportingErrors(t *testing.T) {
	fakeTest := &testing.T{}
	reporter := FakeReporter{}
	fakeReporter := Reporter(&reporter)

	g := Goblin(fakeTest)
	g.SetReporter(fakeReporter)

	g.Describe("Numbers", func() {
		g.It("Should make reporting add two errors ", func() {
			g.Assert(0).Equal(1)
			g.Assert(0).Equal(1)
		})
	})

	if reporter.failures != 1 {
		t.FailNow()
	}
}