-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_case_attempt_parser.ts
194 lines (184 loc) · 5.63 KB
/
test_case_attempt_parser.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
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
import { getStepKeywordType, KeywordType } from './keyword_type'
import {
getGherkinScenarioLocationMap,
getGherkinStepMap,
} from './gherkin_document_parser'
import { getPickleStepMap, getStepKeyword } from './pickle_parser'
import * as messages from '@cucumber/messages'
import { ITestCaseAttempt } from './event_data_collector'
import StepDefinitionSnippetBuilder from '../step_definition_snippet_builder'
import { ISupportCodeLibrary } from '../../support_code_library_builder/types'
import { doesHaveValue, valueOrDefault } from '../../value_checker'
import TestCaseHookDefinition from '../../models/test_case_hook_definition'
import { ILineAndUri } from '../../types'
import { TestStepResult } from '@cucumber/messages'
export interface IParsedTestStep {
actionLocation?: ILineAndUri
argument?: messages.PickleStepArgument
attachments: messages.Attachment[]
keyword: string
name?: string
result: messages.TestStepResult
snippet?: string
sourceLocation?: ILineAndUri
text?: string
}
export interface IParsedTestCase {
attempt: number
name: string
sourceLocation?: ILineAndUri
worstTestStepResult: messages.TestStepResult
}
export interface IParsedTestCaseAttempt {
testCase: IParsedTestCase
testSteps: IParsedTestStep[]
}
interface IParseStepRequest {
isBeforeHook: boolean
gherkinStepMap: Record<string, messages.Step>
keyword: string
keywordType: KeywordType
pickleStep: messages.PickleStep
pickleUri: string
snippetBuilder: StepDefinitionSnippetBuilder
supportCodeLibrary: ISupportCodeLibrary
testStep: messages.TestStep
testStepResult: messages.TestStepResult
testStepAttachments: messages.Attachment[]
}
function parseStep({
isBeforeHook,
gherkinStepMap,
keyword,
keywordType,
pickleStep,
pickleUri,
snippetBuilder,
supportCodeLibrary,
testStep,
testStepResult,
testStepAttachments,
}: IParseStepRequest): IParsedTestStep {
const out: IParsedTestStep = {
attachments: testStepAttachments,
keyword: doesHaveValue(testStep.pickleStepId)
? keyword
: isBeforeHook
? 'Before'
: 'After',
result: testStepResult,
}
if (doesHaveValue(testStep.hookId)) {
let hookDefinition: TestCaseHookDefinition
if (isBeforeHook) {
hookDefinition = supportCodeLibrary.beforeTestCaseHookDefinitions.find(
(x) => x.id === testStep.hookId
)
} else {
hookDefinition = supportCodeLibrary.afterTestCaseHookDefinitions.find(
(x) => x.id === testStep.hookId
)
}
out.actionLocation = {
uri: hookDefinition.uri,
line: hookDefinition.line,
}
out.name = hookDefinition.name
}
if (
doesHaveValue(testStep.stepDefinitionIds) &&
testStep.stepDefinitionIds.length === 1
) {
const stepDefinition = supportCodeLibrary.stepDefinitions.find(
(x) => x.id === testStep.stepDefinitionIds[0]
)
out.actionLocation = {
uri: stepDefinition.uri,
line: stepDefinition.line,
}
}
if (doesHaveValue(testStep.pickleStepId)) {
out.sourceLocation = {
uri: pickleUri,
line: gherkinStepMap[pickleStep.astNodeIds[0]].location.line,
}
out.text = pickleStep.text
if (doesHaveValue(pickleStep.argument)) {
out.argument = pickleStep.argument
}
}
if (testStepResult.status === messages.TestStepResultStatus.UNDEFINED) {
out.snippet = snippetBuilder.build({ keywordType, pickleStep })
}
return out
}
export interface IParseTestCaseAttemptRequest {
testCaseAttempt: ITestCaseAttempt
snippetBuilder: StepDefinitionSnippetBuilder
supportCodeLibrary: ISupportCodeLibrary
}
// Converts a testCaseAttempt into a json object with all data needed for
// displaying it in a pretty format
export function parseTestCaseAttempt({
testCaseAttempt,
snippetBuilder,
supportCodeLibrary,
}: IParseTestCaseAttemptRequest): IParsedTestCaseAttempt {
const { testCase, pickle, gherkinDocument } = testCaseAttempt
const gherkinStepMap = getGherkinStepMap(gherkinDocument)
const gherkinScenarioLocationMap =
getGherkinScenarioLocationMap(gherkinDocument)
const pickleStepMap = getPickleStepMap(pickle)
const relativePickleUri = pickle.uri
const parsedTestCase: IParsedTestCase = {
attempt: testCaseAttempt.attempt,
name: pickle.name,
sourceLocation: {
uri: relativePickleUri,
line: gherkinScenarioLocationMap[
pickle.astNodeIds[pickle.astNodeIds.length - 1]
].line,
},
worstTestStepResult: testCaseAttempt.worstTestStepResult,
}
const parsedTestSteps: IParsedTestStep[] = []
let isBeforeHook = true
let previousKeywordType = KeywordType.Precondition
testCase.testSteps.forEach((testStep) => {
const testStepResult =
testCaseAttempt.stepResults[testStep.id] || new TestStepResult()
isBeforeHook = isBeforeHook && doesHaveValue(testStep.hookId)
let keyword, keywordType, pickleStep
if (doesHaveValue(testStep.pickleStepId)) {
pickleStep = pickleStepMap[testStep.pickleStepId]
keyword = getStepKeyword({ pickleStep, gherkinStepMap })
keywordType = getStepKeywordType({
keyword,
language: gherkinDocument.feature.language,
previousKeywordType,
})
}
const parsedStep = parseStep({
isBeforeHook,
gherkinStepMap,
keyword,
keywordType,
pickleStep,
pickleUri: relativePickleUri,
snippetBuilder,
supportCodeLibrary,
testStep,
testStepResult,
testStepAttachments: valueOrDefault(
testCaseAttempt.stepAttachments[testStep.id],
[]
),
})
parsedTestSteps.push(parsedStep)
previousKeywordType = keywordType
})
return {
testCase: parsedTestCase,
testSteps: parsedTestSteps,
}
}