-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use strong types for parsed stories.
Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
7 changed files
with
102 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import _ from "lodash"; | ||
import { ChapterRequest, Story } from "./types/story.types"; | ||
import { ParsedChapterRequest, ParsedStory } from "./types/parsed_story.types"; | ||
|
||
export default class StoryParser { | ||
static parse(story: Story): ParsedStory { | ||
return { | ||
...story, | ||
chapters: this.#expand_chapters(story.chapters), | ||
prologues: this.#expand_chapters(story.prologues), | ||
epilogues: this.#expand_chapters(story.epilogues) | ||
} | ||
} | ||
|
||
static #chapter_methods(methods: string[] | string): string[] { | ||
return [...(Array.isArray(methods) ? methods : [methods])] | ||
} | ||
|
||
static #expand_chapters(chapters: ChapterRequest[] | undefined): ParsedChapterRequest[] { | ||
if (chapters === undefined) return [] | ||
return _.flatMap(_.map(chapters, (chapter) => { | ||
return _.map(this.#chapter_methods(chapter.method), (method) => { | ||
return { | ||
...chapter, | ||
method | ||
} | ||
}) | ||
})) as ParsedChapterRequest[] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Chapter, HttpMethod, Story, SupplementalChapter } from "./story.types" | ||
|
||
export interface ParsedChapterRequest extends Chapter { | ||
method: HttpMethod | ||
} | ||
|
||
export type ParsedChapter = ParsedChapterRequest & Chapter | ||
export type ParsedSupplementalChapter = ParsedChapterRequest & SupplementalChapter | ||
|
||
export interface ParsedStory extends Story { | ||
chapters: ParsedChapter[] | ||
prologues?: ParsedSupplementalChapter[] | ||
epilogues?: ParsedSupplementalChapter[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { read_yaml } from "helpers"; | ||
import StoryParser from "../../src/tester/StoryParser"; | ||
import _ from "lodash"; | ||
|
||
describe('StoryReader', () => { | ||
const story = StoryParser.parse(read_yaml('tools/tests/tester/fixtures/stories/passed/multiple_methods.yaml')) | ||
|
||
test('expands prologues', () => { | ||
expect(story.prologues?.length).toEqual(2) | ||
expect(_.map(story.prologues, (prologue) => prologue.method)).toEqual(['HEAD', 'DELETE']) | ||
}) | ||
|
||
test('expands epilogues', () => { | ||
expect(story.epilogues?.length).toEqual(2) | ||
expect(_.map(story.epilogues, (epilogue) => epilogue.method)).toEqual(['HEAD', 'DELETE']) | ||
}) | ||
|
||
test('expands chapters', () => { | ||
expect(story.chapters?.length).toEqual(2) | ||
expect(_.map(story.chapters, (chapter) => chapter.method)).toEqual(['PUT', 'HEAD']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters