Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Incomplete] Support for docstring rules #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions src/lib/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feature
= _ annotations:Annotations TFeature name:String NL Preamble? background:Background? _ scenarios:Scenarios
{ return { name, background, scenarios, annotations } }

Annotation
Annotation
= TAt attribute:Keyword "(" args:[^)]* ")" _
{ return { name: attribute, arguments: JSON.parse('[' + args.join('') + ']') } }
/ TAt attribute:Keyword _
Expand All @@ -46,7 +46,7 @@ As
{ return actor }

Want
= _ TIWant want:String NL
= _ TIWant want:String NL
{ return want }

Reason
Expand All @@ -60,12 +60,12 @@ Background
Scenarios
= scenarios:Scenario*
{ return flatten(scenarios) }

Scenario = _ annotations:Annotations TScenario name:String NL rules:Rules _
{ return { name, rules, annotations } }

/ _ annotations:Annotations TScenarioOutline name:String NL rules:Rules examples:Examples _
{
{
return examples.map((example) => ({
name: {value: expandTemplateString(name.value, example), location: name.location},
rules: rules.map((template) => ({value: expandTemplateString(template.value, example), location: template.location})),
Expand All @@ -78,16 +78,32 @@ Rules
{ return rules }

Rule
= _ Clause rule:String EOS table:Table
{ return Object.assign({}, rule, {table}) }
= _ Clause rule:String EOS addon:( DocStringAddon / TableAddon )
{ return Object.assign({}, rule, addon) }

TableAddon
= table:Table
{ return {table} }

DocStringAddon
= DocStringMarker lines:DocStringLine+ DocStringMarker
{ return {docstring: lines.join('\n')} }

DocStringLine
= WS line:(string: String ! {return string.value === ('"""')}) NL
{ return line[0].value }

DocStringMarker
= WS TTrippleQuote NL


Clause
= TGiven
/ TWhen
/ TThen
/ TAnd
/ TStar

Examples
= _ TExamples NL table:Table
{
Expand Down Expand Up @@ -134,10 +150,12 @@ TExamples = "Examples:"
TTableSep = "|"
TStar = "*"
TAt = "@"
TTrippleQuote = "\"\"\""


TTableCell
= data:[^|\n]+
{
{
const cell = data.join('').trim();

return /^"[^"]*"$/.test(cell)
Expand All @@ -147,7 +165,7 @@ TTableCell

String
= str:[^\n]+
{
{
return {
value: str.join('').trim(),
location: location().start
Expand All @@ -159,7 +177,7 @@ EOS
/ EOF

EOF
= !.
= !.

Keyword
= str:[a-zA-Z0-9_-]+
Expand All @@ -176,4 +194,4 @@ Comment

WS "whitespace"
= [ \t\n\r]*

40 changes: 40 additions & 0 deletions src/test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,46 @@ Object {
}
`;

exports[`Parser should parse a feature with docstring 1`] = `
Object {
"annotations": Array [],
"background": null,
"name": Object {
"location": Object {
"column": 9,
"line": 1,
"offset": 8,
},
"value": "doc strings",
},
"scenarios": Array [
Object {
"annotations": Array [],
"name": Object {
"location": Object {
"column": 12,
"line": 2,
"offset": 32,
},
"value": "doc strings",
},
"rules": Array [
Object {
"docstring": "I have a line.
And another line.",
"location": Object {
"column": 10,
"line": 3,
"offset": 54,
},
"value": "I have a docstring",
},
],
},
],
}
`;

exports[`Parser should parse a feature with no newline at the end of the file 1`] = `
Object {
"annotations": Array [],
Expand Down
7 changes: 7 additions & 0 deletions src/test/doc-string.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: doc strings
Scenario: doc strings
Given I have a docstring
"""
I have a line.
And another line.
"""
4 changes: 4 additions & 0 deletions src/test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe("Parser", () => {
const result = parse(readFileSync(__dirname + '/data-table.feature', 'utf8'));
expect(result).toMatchSnapshot();
});
it("should parse a feature with docstring", () => {
const result = parse(readFileSync(__dirname + '/doc-string.feature', 'utf8'));
expect(result).toMatchSnapshot();
});

it("should parse a feature with a background", () => {
const result = parse(readFileSync(__dirname + '/background.feature', 'utf8'));
Expand Down