Skip to content

Commit cdd851a

Browse files
Also search in step docstring
1 parent 2104fa0 commit cdd851a

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/search/StepSearch.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ interface SearchableStep {
55
id: string
66
keyword: string
77
text: string
8+
docstring?: string
89
}
910

1011
export default class StepSearch {
1112
private readonly index = elasticlunr<SearchableStep>((ctx) => {
1213
ctx.addField('keyword')
1314
ctx.addField('text')
15+
ctx.addField('docstring')
1416
ctx.setRef('id')
1517
ctx.saveDocument(true)
1618
})
@@ -21,6 +23,7 @@ export default class StepSearch {
2123
id: step.id,
2224
keyword: step.keyword,
2325
text: step.text,
26+
docstring: this.docstringToString(step),
2427
}
2528

2629
this.index.addDoc(doc)
@@ -32,9 +35,16 @@ export default class StepSearch {
3235
fields: {
3336
keyword: { bool: 'OR', expand: true, boost: 1 },
3437
text: { bool: 'OR', expand: true, boost: 2 },
38+
docstring: { bool: 'OR', expand: true, boost: 1 },
3539
},
3640
})
3741

3842
return results.map((result) => this.stepById.get(result.ref))
3943
}
44+
45+
private docstringToString(
46+
step: messages.GherkinDocument.Feature.IStep
47+
): string {
48+
return step.docString ? step.docString.content : ''
49+
}
4050
}

test/search/StepSearchTest.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('StepSearch', () => {
1111
stepSearch = new StepSearch()
1212

1313
steps = [
14-
makeStep('Given', 'a passed step'),
14+
makeStep('Given', 'a passed step', 'There is a docstring here'),
1515
makeStep('When', 'another passed step'),
1616
makeStep('Then', 'a failed step'),
1717
]
@@ -50,5 +50,10 @@ describe('StepSearch', () => {
5050
// amongst them are 'Then' and 'When'.
5151
// See: http://elasticlunr.com/docs/stop_word_filter.js.html#resetStopWords
5252
})
53+
54+
it('returns step which DocString matches the query', () => {
55+
const searchResults = stepSearch.search('docstring')
56+
assert.deepStrictEqual(searchResults, [steps[0]])
57+
})
5358
})
5459
})

test/search/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ function makeScenario(
5252

5353
function makeStep(
5454
keyword: string,
55-
text: string
55+
text: string,
56+
docstring = ''
5657
): messages.GherkinDocument.Feature.IStep {
5758
const idGenerator = IdGenerator.uuid()
59+
const docStringParam = docstring
60+
? messages.GherkinDocument.Feature.Step.DocString.create({
61+
content: docstring,
62+
})
63+
: undefined
5864

5965
return messages.GherkinDocument.Feature.Step.create({
6066
id: idGenerator(),
6167
keyword: keyword,
6268
text: text,
69+
docString: docStringParam,
6370
})
6471
}
6572

0 commit comments

Comments
 (0)