Skip to content

Commit

Permalink
Additional error handling edge cases (#356)
Browse files Browse the repository at this point in the history
* Add additional bad tests

* [c] Optimise error handling for empty datatable rows

* [Perl] Optimise error handling for unclosed DocStrings
  • Loading branch information
obligaron authored Feb 12, 2025
1 parent a405d98 commit c6c72ca
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt

### Fixed
- [.NET] Fix NuGet package generation
- [c] Optimise error handling for empty datatable rows
- [Perl] Optimise error handling for unclosed DocStrings

## [31.0.0] - 2025-01-29
### Added
Expand Down
13 changes: 9 additions & 4 deletions c/src/gherkin_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,22 @@ wchar_t* GherkinLine_copy_line_text(const GherkinLine* line, int indent_to_remov

const Items* GherkinLine_table_cells(const GherkinLine* line) {
int item_count = calculate_cell_count(line->trimmed_line);
if (item_count == 0)
return (Items*)0;
Items* item_array = (Items*)malloc(sizeof(Items));
item_array->count = item_count;

if (item_count == 0) {
item_array->items = 0;
return item_array;
}

Span* items = (Span*)malloc(item_count * sizeof(Span));
int i;
for (i = 0; i < item_count; ++i) {
items[i].column = 0;
items[i].text = 0;
}
Items* item_array = (Items*)malloc(sizeof(Items));
item_array->count = item_count;
item_array->items = items;

const wchar_t* current_pos = line->trimmed_line;
for (i = 0; i < item_count; ++i) {
current_pos = populate_cell_data(&items[i], current_pos, current_pos - line->line_text);
Expand Down
3 changes: 3 additions & 0 deletions perl/lib/Gherkin/TokenMatcher.pm
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ sub match_StepLine {

sub match_DocStringSeparator {
my ( $self, $token ) = @_;
if ($token->is_eof) {
return 0;
}
if ( !$self->_active_doc_string_separator ) {
return $self->_match_DocStringSeparator( $token, '"""', 1 )
|| $self->_match_DocStringSeparator( $token, '```', 1 );
Expand Down
5 changes: 5 additions & 0 deletions testdata/bad/backslash_at_end_of_line_in_datatable.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Addition
Scenario: Add two numbers
When I press
| foo |
| bar \
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parseError":{"message":"(5:3): inconsistent cell count within the table","source":{"location":{"column":3,"line":5},"uri":"../testdata/bad/backslash_at_end_of_line_in_datatable.feature"}}}
4 changes: 4 additions & 0 deletions testdata/bad/file_ends_with_open_docstring.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: Addition
Scenario: Add two numbers
Given I have added
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parseError":{"message":"(5:0): unexpected end of file, expected: #DocStringSeparator, #Other","source":{"location":{"line":5},"uri":"../testdata/bad/file_ends_with_open_docstring.feature"}}}
2 changes: 2 additions & 0 deletions testdata/bad/unexpected_end_of_file.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Feature: Addition
@tag
1 change: 1 addition & 0 deletions testdata/bad/unexpected_end_of_file.feature.errors.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parseError":{"message":"(3:0): unexpected end of file, expected: #TagLine, #RuleLine, #Comment, #Empty","source":{"location":{"line":3},"uri":"../testdata/bad/unexpected_end_of_file.feature"}}}
5 changes: 5 additions & 0 deletions testdata/bad/unfinished_datatable.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Addition
Scenario: Add two numbers
When I press
| foo |
| bar
1 change: 1 addition & 0 deletions testdata/bad/unfinished_datatable.feature.errors.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"parseError":{"message":"(5:3): inconsistent cell count within the table","source":{"location":{"column":3,"line":5},"uri":"../testdata/bad/unfinished_datatable.feature"}}}

0 comments on commit c6c72ca

Please sign in to comment.