Skip to content

Commit

Permalink
fix table row count issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed May 5, 2010
1 parent f4dda02 commit fe5d740
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
28 changes: 12 additions & 16 deletions Parser/GherkinBuilder/ExampleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ internal class ExampleBuilder : ITableProcessor
{
private readonly string text;
private readonly FilePosition position;

private Dictionary<int, string[]> _exampleLines = new Dictionary<int, string[]>();
private readonly TableBuilder tableBuilder = new TableBuilder();

public ExampleBuilder(string text, FilePosition position)
{
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Parser/GherkinBuilder/ScenarioOutlineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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."
});

Expand Down
11 changes: 3 additions & 8 deletions Parser/GherkinBuilder/StepBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using gherkin;
using TechTalk.SpecFlow.Parser.SyntaxElements;

Expand All @@ -9,7 +7,7 @@ namespace TechTalk.SpecFlow.Parser.GherkinBuilder
internal class StepBuilder : ITableProcessor
{
private readonly ScenarioStep step;
private readonly List<Row> tableRows = new List<Row>();
private readonly TableBuilder tableBuilder = new TableBuilder();

public StepBuilder(string keyword, string text, FilePosition position, I18n i18n)
{
Expand All @@ -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;
}
Expand All @@ -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);
}
}
}
36 changes: 36 additions & 0 deletions Parser/GherkinBuilder/TableBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<Row> tableRows = new List<Row>();

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);
}
}
}
1 change: 1 addition & 0 deletions Parser/TechTalk.SpecFlow.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="GherkinBuilder\ScenarioBuilder.cs" />
<Compile Include="GherkinBuilder\ScenarioOutlineBuilder.cs" />
<Compile Include="GherkinBuilder\StepBuilder.cs" />
<Compile Include="GherkinBuilder\TableBuilder.cs" />
<Compile Include="SupportedLanguageHelper.cs" />
<Compile Include="SyntaxElements\FilePosition.cs" />
<Compile Include="SpecFlowLangParser.cs" />
Expand Down

0 comments on commit fe5d740

Please sign in to comment.