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

Fix line number reporting #33

Merged
merged 5 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ All user visible changes to `gherkin` crate will be documented in this file. Thi
### Added

- Support text after `Background` and `Examples` keywords. ([#31])
- Fix line number reporting ([#33])
ilslv marked this conversation as resolved.
Show resolved Hide resolved

[#32]: /../../pull/31
[#31]: /../../pull/31
[#33]: /../../pull/33



Expand Down
76 changes: 72 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ impl GherkinEnv {
}

fn increment_nl(&self, offset: usize) {
self.line_offsets.borrow_mut().push(offset);
let mut line_offsets = self.line_offsets.borrow_mut();
if !line_offsets.contains(&offset) {
line_offsets.push(offset);
}
}

fn position(&self, offset: usize) -> LineCol {
Expand Down Expand Up @@ -199,15 +202,15 @@ rule language_directive() -> ()
}

rule docstring() -> String
= "\"\"\"" n:$((!"\"\"\""[_])*) "\"\"\"" nl_eof() {
= "\"\"\"" n:$((!"\"\"\"" (nl() / [_]))*) "\"\"\"" nl_eof() {
textwrap::dedent(n)
}
/ "```" n:$((!"```"[_])*) "```" nl_eof() {
/ "```" n:$((!"```"(nl() / [_]))*) "```" nl_eof() {
textwrap::dedent(n)
}

rule table_cell() -> &'input str
= "|" _ !(nl0() / eof()) n:$((!"|"[_])*) { n }
= "|" _ !(nl0() / eof()) n:$((!("|" / nl0())[_])*) { n }

pub(crate) rule table_row() -> Vec<String>
= n:(table_cell() ** _) _ "|" _ nl_eof() {
Expand Down Expand Up @@ -664,6 +667,71 @@ Scenario: Hello
assert!(feature.scenarios[0].steps[0].position.line != 0);
}

#[test]
fn correct_line_number() {
let env = GherkinEnv::default();
let input = r#"Feature: Basic functionality
here's some text
really
@tag
Scenario: Hello
Given a step
Then a step
@tag
Scenario: Hello
Given a step

And more

Rule: rule
@tag
Scenario Outline: Hello
Given <step>
"""
Doc String
"""

Examples:
| step |
| 1 |
| 2 |


@tag
Rule: rule
Scenario: Hello
Given a step
"#;
let feature = gherkin_parser::feature(input, &env).unwrap();
assert_eq!(feature.scenarios.len(), 2);
assert!(feature.description.is_some());
assert_eq!(feature.position.line, 1);
assert_eq!(feature.scenarios[0].position.line, 5);
assert_eq!(feature.scenarios[0].steps[0].position.line, 6);
assert_eq!(feature.scenarios[0].steps[1].position.line, 7);
assert_eq!(feature.scenarios[1].position.line, 9);
assert_eq!(feature.scenarios[1].steps[0].position.line, 10);
assert_eq!(feature.scenarios[1].steps[1].position.line, 12);
assert_eq!(feature.rules[0].position.line, 14);
assert_eq!(feature.rules[0].scenarios[0].position.line, 16);
assert_eq!(feature.rules[0].scenarios[0].steps[0].position.line, 17);
assert_eq!(feature.rules[0].scenarios[0].examples[0].position.line, 22);
assert_eq!(
feature.rules[0].scenarios[0].examples[0]
.table
.position
.line,
23,
);
assert_eq!(
feature.rules[0].scenarios[0].examples[0].table.rows.len(),
3,
);
assert_eq!(feature.rules[1].position.line, 29);
assert_eq!(feature.rules[1].scenarios[0].position.line, 30);
assert_eq!(feature.rules[1].scenarios[0].steps[0].position.line, 31);
}

#[test]
fn feature_only() {
let env = GherkinEnv::default();
Expand Down