diff --git a/Parser/GherkinBuilder/ExampleBuilder.cs b/Parser/GherkinBuilder/ExampleBuilder.cs index 748933dd8..23eefe135 100644 --- a/Parser/GherkinBuilder/ExampleBuilder.cs +++ b/Parser/GherkinBuilder/ExampleBuilder.cs @@ -9,8 +9,7 @@ internal class ExampleBuilder : ITableProcessor { private readonly string text; private readonly FilePosition position; - - private Dictionary _exampleLines = new Dictionary(); + private readonly TableBuilder tableBuilder = new TableBuilder(); public ExampleBuilder(string text, FilePosition position) { @@ -20,25 +19,22 @@ public ExampleBuilder(string text, FilePosition position) public ExampleSet GetResult() { - Table exampleTable = new Table - { - Header = new Row - { - Cells = _exampleLines.Values.First().Select(c => new Cell(c)).ToArray(), - FilePosition = new FilePosition(_exampleLines.Keys.First(), 1) - }, - Body = _exampleLines.Skip(1).Select(r => new Row - { - Cells = r.Value.Select(c => new Cell(c)).ToArray(), - FilePosition = new FilePosition(r.Key, 1) - }).ToArray() - }; + Table exampleTable = tableBuilder.GetResult(); + if (exampleTable == null) + throw new SpecFlowParserException( + new ErrorDetail + { + Line = position.Line, + Column = position.Column, + Message = "No examples defined in the example set!" + }); + return new ExampleSet(text, exampleTable); } public void ProcessTableRow(string[] row, int lineNumber) { - _exampleLines.Add(lineNumber, row); + tableBuilder.ProcessTableRow(row, lineNumber); } } } \ No newline at end of file diff --git a/Parser/GherkinBuilder/ScenarioOutlineBuilder.cs b/Parser/GherkinBuilder/ScenarioOutlineBuilder.cs index acee5496c..f25e694b4 100644 --- a/Parser/GherkinBuilder/ScenarioOutlineBuilder.cs +++ b/Parser/GherkinBuilder/ScenarioOutlineBuilder.cs @@ -32,7 +32,7 @@ public Scenario GetResult() new ErrorDetail { Line = position.Line, - Column = position.Column - 1, + Column = position.Column, Message = "There are no examples defined for the scenario outline." }); diff --git a/Parser/GherkinBuilder/StepBuilder.cs b/Parser/GherkinBuilder/StepBuilder.cs index a12c01da3..1b19c30c1 100644 --- a/Parser/GherkinBuilder/StepBuilder.cs +++ b/Parser/GherkinBuilder/StepBuilder.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using gherkin; using TechTalk.SpecFlow.Parser.SyntaxElements; @@ -9,7 +7,7 @@ namespace TechTalk.SpecFlow.Parser.GherkinBuilder internal class StepBuilder : ITableProcessor { private readonly ScenarioStep step; - private readonly List tableRows = new List(); + private readonly TableBuilder tableBuilder = new TableBuilder(); public StepBuilder(string keyword, string text, FilePosition position, I18n i18n) { @@ -26,8 +24,7 @@ public StepBuilder(string keyword, string text, FilePosition position, I18n i18n public ScenarioStep GetResult() { - step.TableArg = tableRows.Count == 0 ? null : - new Table(tableRows[0], tableRows.Skip(1).ToArray()); + step.TableArg = tableBuilder.GetResult(); return step; } @@ -39,9 +36,7 @@ public void SetMultilineArg(string text) public void ProcessTableRow(string[] cells, int lineNumber) { - var row = new Row(cells.Select(c => new Cell(c)).ToArray()); - row.FilePosition = new FilePosition(lineNumber, 1); - tableRows.Add(row); + tableBuilder.ProcessTableRow(cells, lineNumber); } } } \ No newline at end of file diff --git a/Parser/GherkinBuilder/TableBuilder.cs b/Parser/GherkinBuilder/TableBuilder.cs new file mode 100644 index 000000000..75183bad0 --- /dev/null +++ b/Parser/GherkinBuilder/TableBuilder.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using TechTalk.SpecFlow.Parser.SyntaxElements; + +namespace TechTalk.SpecFlow.Parser.GherkinBuilder +{ + internal class TableBuilder : ITableProcessor + { + private readonly List tableRows = new List(); + + public Table GetResult() + { + return tableRows.Count == 0 ? null : + new Table(tableRows[0], tableRows.Skip(1).ToArray()); + } + + public void ProcessTableRow(string[] cells, int lineNumber) + { + var row = new Row(cells.Select(c => new Cell(c)).ToArray()); + row.FilePosition = new FilePosition(lineNumber, 1); + + if (tableRows.Count > 0 && tableRows[0].Cells.Length != row.Cells.Length) + { + throw new SpecFlowParserException( + new ErrorDetail + { + Line = row.FilePosition.Line, + Column = row.FilePosition.Column, + Message = "Number of cells in the row does not match the number of cells in the header!" + }); + } + + tableRows.Add(row); + } + } +} \ No newline at end of file diff --git a/Parser/TechTalk.SpecFlow.Parser.csproj b/Parser/TechTalk.SpecFlow.Parser.csproj index 49179a740..fbe90559a 100644 --- a/Parser/TechTalk.SpecFlow.Parser.csproj +++ b/Parser/TechTalk.SpecFlow.Parser.csproj @@ -78,6 +78,7 @@ +