Skip to content

Commit

Permalink
clarify interfaces and types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Dec 2, 2023
1 parent 1986844 commit c694f37
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
15 changes: 13 additions & 2 deletions src/search/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ class Search {
}
}

/**
* Creates a search index that supports querying by term or tag expression, and
* returns an array of abridged Gherkin documents matching the query.
*
* @param gherkinQuery - query instance used internally for searching, with any
* documents already present being pre-populated in the search index
*/
export async function createSearch(gherkinQuery: GherkinQuery): Promise<Searchable> {
const [tagSearch, textSearch] = await Promise.all([
createTagSearch(gherkinQuery),
createTextSearch(gherkinQuery.getGherkinDocuments()),
createTextSearch(),
])
return new Search(tagSearch, textSearch)
const searchImpl = new Search(tagSearch, textSearch)
for (const doc of gherkinQuery.getGherkinDocuments()) {
await searchImpl.add(doc)
}
return searchImpl
}
5 changes: 4 additions & 1 deletion src/search/TagSearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ Feature: Solar System
newId: messages.IdGenerator.incrementing(),
}
)
const tagSearch = await createTagSearch(gherkinQuery)
for (const envelope of envelopes) {
gherkinQuery.update(envelope)
}
const tagSearch = await createTagSearch(gherkinQuery)
for (const document of gherkinQuery.getGherkinDocuments()) {
await tagSearch.add(document)
}
return pretty((await tagSearch.search(query))[0])
}

Expand Down
6 changes: 1 addition & 5 deletions src/search/TagSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,5 @@ class TagSearch {
}

export async function createTagSearch(gherkinQuery: GherkinQuery): Promise<Searchable> {
const tagSearchImpl = new TagSearch(gherkinQuery)
for (const document of gherkinQuery.getGherkinDocuments()) {
await tagSearchImpl.add(document)
}
return tagSearchImpl
return new TagSearch(gherkinQuery)
}
3 changes: 2 additions & 1 deletion src/search/TextSearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('TextSearch', () => {
beforeEach(async () => {
const gherkinDocument = parse(source)

search = await createTextSearch([gherkinDocument])
search = await createTextSearch()
await search.add(gherkinDocument)
})

describe('Hit found in step', () => {
Expand Down
16 changes: 2 additions & 14 deletions src/search/TextSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class TextSearch {
}
}

export async function createTextSearch(
gherkinDocuments: readonly GherkinDocument[]
): Promise<Searchable> {
export async function createTextSearch(): Promise<Searchable> {
const [stepSearch, backgroundSearch, scenarioSearch, ruleSearch, featureSearch] =
await Promise.all([
createStepSearch(),
Expand All @@ -82,15 +80,5 @@ export async function createTextSearch(
createScenarioLikeSearch<Rule>(),
createFeatureSearch(),
])
const textSearchImpl = new TextSearch(
stepSearch,
backgroundSearch,
scenarioSearch,
ruleSearch,
featureSearch
)
for (const document of gherkinDocuments) {
await textSearchImpl.add(document)
}
return textSearchImpl
return new TextSearch(stepSearch, backgroundSearch, scenarioSearch, ruleSearch, featureSearch)
}

0 comments on commit c694f37

Please sign in to comment.