-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
event_data_collector.ts
126 lines (112 loc) · 4.07 KB
/
event_data_collector.ts
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
import _, { Dictionary } from 'lodash'
import { messages } from 'cucumber-messages'
import { doesHaveValue, doesNotHaveValue } from '../../value_checker'
interface ITestCaseAttemptData {
attempt: number
testCaseId: string
result: messages.ITestResult
stepAttachments: Dictionary<messages.IAttachment[]>
stepResults: Dictionary<messages.ITestResult>
}
export interface ITestCaseAttempt {
attempt: number
gherkinDocument: messages.IGherkinDocument
pickle: messages.IPickle
result: messages.ITestResult
stepAttachments: Dictionary<messages.IAttachment[]>
stepResults: Dictionary<messages.ITestResult>
testCase: messages.ITestCase
}
export interface IEventBroadcaster {
on(name: string, handler: Function): void
}
export default class EventDataCollector {
private gherkinDocumentMap: Dictionary<messages.IGherkinDocument> = {}
private pickleMap: Dictionary<messages.IPickle> = {}
private testCaseMap: Dictionary<messages.ITestCase> = {}
private testCaseAttemptDataMap: Dictionary<ITestCaseAttemptData> = {}
constructor(eventBroadcaster: IEventBroadcaster) {
eventBroadcaster.on('envelope', this.parseEnvelope.bind(this))
}
getGherkinDocument(uri: string): messages.IGherkinDocument {
return this.gherkinDocumentMap[uri]
}
getPickle(pickleId: string): messages.IPickle {
return this.pickleMap[pickleId]
}
getTestCaseAttempts(): ITestCaseAttempt[] {
return _.keys(this.testCaseAttemptDataMap).map(testCaseStartedId => {
return this.getTestCaseAttempt(testCaseStartedId)
})
}
getTestCaseAttempt(testCaseStartedId: string): ITestCaseAttempt {
const testCaseAttemptData = this.testCaseAttemptDataMap[testCaseStartedId]
const testCase = this.testCaseMap[testCaseAttemptData.testCaseId]
const pickle = this.pickleMap[testCase.pickleId]
return {
gherkinDocument: this.gherkinDocumentMap[pickle.uri],
pickle,
testCase,
attempt: testCaseAttemptData.attempt,
result: testCaseAttemptData.result,
stepAttachments: testCaseAttemptData.stepAttachments,
stepResults: testCaseAttemptData.stepResults,
}
}
parseEnvelope(envelope: messages.Envelope): void {
if (doesHaveValue(envelope.gherkinDocument)) {
this.gherkinDocumentMap[envelope.gherkinDocument.uri] =
envelope.gherkinDocument
} else if (doesHaveValue(envelope.pickle)) {
this.pickleMap[envelope.pickle.id] = envelope.pickle
} else if (doesHaveValue(envelope.testCase)) {
this.testCaseMap[envelope.testCase.id] = envelope.testCase
} else if (doesHaveValue(envelope.testCaseStarted)) {
this.initTestCaseAttempt(envelope.testCaseStarted)
} else if (doesHaveValue(envelope.attachment)) {
this.storeAttachment(envelope.attachment)
} else if (doesHaveValue(envelope.testStepFinished)) {
this.storeTestStepResult(envelope.testStepFinished)
} else if (doesHaveValue(envelope.testCaseFinished)) {
this.storeTestCaseResult(envelope.testCaseFinished)
}
}
initTestCaseAttempt(testCaseStarted: messages.ITestCaseStarted): void {
this.testCaseAttemptDataMap[testCaseStarted.id] = {
attempt: testCaseStarted.attempt,
testCaseId: testCaseStarted.testCaseId,
result: {},
stepAttachments: {},
stepResults: {},
}
}
storeAttachment({
testCaseStartedId,
testStepId,
data,
media,
}: messages.IAttachment): void {
if (doesHaveValue(testCaseStartedId) && doesHaveValue(testStepId)) {
const { stepAttachments } = this.testCaseAttemptDataMap[testCaseStartedId]
if (doesNotHaveValue(stepAttachments[testStepId])) {
stepAttachments[testStepId] = []
}
stepAttachments[testStepId].push({ data, media })
}
}
storeTestStepResult({
testCaseStartedId,
testStepId,
testResult,
}: messages.ITestStepFinished): void {
this.testCaseAttemptDataMap[testCaseStartedId].stepResults[
testStepId
] = testResult
}
storeTestCaseResult({
testCaseStartedId,
testResult,
}: messages.ITestCaseFinished): void {
this.testCaseAttemptDataMap[testCaseStartedId].result = testResult
}
}