Skip to content

Commit

Permalink
remove unnecessary syntax elements + simplify syntax builder
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed May 5, 2010
1 parent 5f87e75 commit 065dfd3
Show file tree
Hide file tree
Showing 27 changed files with 58 additions and 206 deletions.
2 changes: 1 addition & 1 deletion Parser/GherkinBuilder/BackgroundBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ProcessStep(StepBuilder step)

public Background GetResult()
{
return new Background(new Text(text), new ScenarioSteps(steps.Select(s => s.GetResult()).ToArray())) { FilePosition = position };
return new Background(text, new ScenarioSteps(steps.Select(s => s.GetResult()).ToArray())) { FilePosition = position };
}
}
}
6 changes: 3 additions & 3 deletions Parser/GherkinBuilder/ExampleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ public ExampleSet GetResult()
{
Header = new Row
{
Cells = _exampleLines.Values.First().Select(c => new Cell(new Text(c))).ToArray(),
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(new Text(c))).ToArray(),
Cells = r.Value.Select(c => new Cell(c)).ToArray(),
FilePosition = new FilePosition(r.Key, 1)
}).ToArray()
};
return new ExampleSet(new Text(text), exampleTable);
return new ExampleSet(text, exampleTable);
}

public void ProcessTableRow(string[] row, int lineNumber)
Expand Down
18 changes: 15 additions & 3 deletions Parser/GherkinBuilder/FeatureBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using TechTalk.SpecFlow.Parser.SyntaxElements;

namespace TechTalk.SpecFlow.Parser.GherkinBuilder
Expand All @@ -17,13 +18,24 @@ public FeatureBuilder(string text, Tags tags)
this.tags = tags;
}

private static readonly Regex firstLineRe = new Regex(@"^(?<firstline>[^\r\n]*)[\r\n]+(?<rest>.*)", RegexOptions.Singleline);

public Feature GetResult()
{
var textLines = text.Split('\n');
string title = text;
string description = null;

var match = firstLineRe.Match(text);
if (match.Success)
{
title = match.Groups["firstline"].Value;
description = match.Groups["rest"].Value;
}

return new Feature(
new Text(textLines[0]),
title,
tags,
textLines.Skip(1).SkipWhile(line => string.IsNullOrEmpty(line.Trim('\n', '\r', '\t', ' '))).Select(line => new DescriptionLine(new Text(line))).ToArray(),
description,
background == null ? null : background.GetResult(),
scenarios.Select(sb => sb.GetResult()).ToArray());
}
Expand Down
2 changes: 1 addition & 1 deletion Parser/GherkinBuilder/GherkinListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private Tags FlushTags()
public void tag(string name, int i)
{
string nameWithoutAt = name.Remove(0, 1);
tags.Add(new Tag(new Word(nameWithoutAt)));
tags.Add(new Tag(nameWithoutAt));
}

public void comment(string str, int i)
Expand Down
2 changes: 1 addition & 1 deletion Parser/GherkinBuilder/ScenarioBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void ProcessStep(StepBuilder step)

public Scenario GetResult()
{
return new Scenario(new Text(name), tags, new ScenarioSteps(steps.Select(s => s.GetResult()).ToArray())) { FilePosition = position };
return new Scenario(name, tags, new ScenarioSteps(steps.Select(s => s.GetResult()).ToArray())) { FilePosition = position };
}
}
}
2 changes: 1 addition & 1 deletion Parser/GherkinBuilder/ScenarioOutlineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Scenario GetResult()
});

return new ScenarioOutline(
new Text(name),
name,
tags,
new ScenarioSteps(steps.Select(step => step.GetResult()).ToArray()),
new Examples(examples.Select(example => example.GetResult()).ToArray())) { FilePosition = position };
Expand Down
23 changes: 16 additions & 7 deletions Parser/GherkinBuilder/StepBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using gherkin;
using TechTalk.SpecFlow.Parser.SyntaxElements;

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

public StepBuilder(string keyword, string text, FilePosition position, I18n i18n)
{
if (i18n.keywords("given").contains(keyword)) step = new Given(new Text(text), null, null);
else if (i18n.keywords("when").contains(keyword)) step = new When(new Text(text), null, null);
else if (i18n.keywords("then").contains(keyword)) step = new Then(new Text(text), null, null);
else if (i18n.keywords("and").contains(keyword)) step = new And(new Text(text), null, null);
else if (i18n.keywords("but").contains(keyword)) step = new But(new Text(text), null, null);
if (i18n.keywords("given").contains(keyword)) step = new Given();
else if (i18n.keywords("when").contains(keyword)) step = new When();
else if (i18n.keywords("then").contains(keyword)) step = new Then();
else if (i18n.keywords("and").contains(keyword)) step = new And();
else if (i18n.keywords("but").contains(keyword)) step = new But();
else throw new ArgumentOutOfRangeException(string.Format("Parameter 'keyword' has value that can not be translated! Value:'{0}'", keyword));

step.Text = text;
step.FilePosition = position;
}

public ScenarioStep GetResult()
{
step.TableArg = tableRows.Count == 0 ? null :
new Table(tableRows[0], tableRows.Skip(1).ToArray());

return step;
}

Expand All @@ -30,9 +37,11 @@ public void SetMultilineArg(string text)
step.MultiLineTextArgument = text;
}

public void ProcessTableRow(string[] row, int lineNumber)
public void ProcessTableRow(string[] cells, int lineNumber)
{
step.AddTableArgRow(row, lineNumber);
var row = new Row(cells.Select(c => new Cell(c)).ToArray());
row.FilePosition = new FilePosition(lineNumber, 1);
tableRows.Add(row);
}
}
}
7 changes: 0 additions & 7 deletions Parser/SyntaxElements/And.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@ namespace TechTalk.SpecFlow.Parser.SyntaxElements
{
public class And : ScenarioStep
{
public And()
{
}

public And(Text stepText, MultilineText multilineTextArgument, Table tableArg) : base(stepText, multilineTextArgument, tableArg)
{
}
}
}
4 changes: 2 additions & 2 deletions Parser/SyntaxElements/Background.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public Background()
{
}

public Background(Text title, ScenarioSteps scenarioSteps)
public Background(string title, ScenarioSteps scenarioSteps)
{
Title = title == null ? "" : title.Value;
Title = title ?? "";
Steps = scenarioSteps ?? new ScenarioSteps();
}
}
Expand Down
7 changes: 0 additions & 7 deletions Parser/SyntaxElements/But.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@ namespace TechTalk.SpecFlow.Parser.SyntaxElements
{
public class But : ScenarioStep
{
public But()
{
}

public But(Text stepText, MultilineText multilineTextArgument, Table tableArg) : base(stepText, multilineTextArgument, tableArg)
{
}
}
}
4 changes: 2 additions & 2 deletions Parser/SyntaxElements/Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public ExampleSet()
{
}

public ExampleSet(Text title, Table table)
public ExampleSet(string title, Table table)
{
Title = title == null ? string.Empty : title.Value;
Title = title ?? string.Empty;
Table = table;
}
}
Expand Down
19 changes: 3 additions & 16 deletions Parser/SyntaxElements/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@

namespace TechTalk.SpecFlow.Parser.SyntaxElements
{
/// <summary>
/// not required in the final AST
/// </summary>
public class DescriptionLine
{
public string LineText { get; set; }

public DescriptionLine(Text lineText)
{
LineText = lineText.Value;
}
}

public class Feature
{
public string Language { get; set; }
Expand All @@ -32,13 +19,13 @@ public Feature()
{
}

public Feature(Text title, Tags tags, DescriptionLine[] description, Background background, params Scenario[] scenarios)
public Feature(string title, Tags tags, string description, Background background, params Scenario[] scenarios)
{
Tags = tags;
Description = description == null ? string.Empty : string.Join(Environment.NewLine, description.Select(d => d.LineText.Trim()).ToArray());
Description = description ?? string.Empty;
Background = background;
Scenarios = scenarios;
Title = title.Value;
Title = title;
}
}
}
2 changes: 1 addition & 1 deletion Parser/SyntaxElements/FilePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public FilePosition()
public FilePosition(int line, int column)
{
Line = line;
Column = column; // TODO: 20100430,jb: remove column since it is not provided by gherkin...
Column = column; //NOTE: this is always 1 now since it is not provided by gherkin...
}
}
}
7 changes: 0 additions & 7 deletions Parser/SyntaxElements/Given.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace TechTalk.SpecFlow.Parser.SyntaxElements
{
public class Given : ScenarioStep
{
public Given()
{
}

public Given(Text givenText, MultilineText multilineTextArgument, Table tableArg) : base(givenText, multilineTextArgument, tableArg)
{
}
}

public class Givens : List<Given>
Expand Down
18 changes: 0 additions & 18 deletions Parser/SyntaxElements/MultilineText.cs

This file was deleted.

4 changes: 2 additions & 2 deletions Parser/SyntaxElements/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public Scenario()
{
}

public Scenario(Text title, Tags tags, ScenarioSteps scenarioSteps)
public Scenario(string title, Tags tags, ScenarioSteps scenarioSteps)
{
Title = title.Value;
Title = title;
Tags = tags;
Steps = scenarioSteps ?? new ScenarioSteps();
}
Expand Down
2 changes: 1 addition & 1 deletion Parser/SyntaxElements/ScenarioOutline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public ScenarioOutline()
{
}

public ScenarioOutline(Text title, Tags tags, ScenarioSteps scenarioSteps, Examples examples) : base(title, tags, scenarioSteps)
public ScenarioOutline(string title, Tags tags, ScenarioSteps scenarioSteps, Examples examples) : base(title, tags, scenarioSteps)
{
Examples = examples;
}
Expand Down
53 changes: 1 addition & 52 deletions Parser/SyntaxElements/ScenarioStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,8 @@ public class ScenarioStep
{
public string Text { get; set; }
public string MultiLineTextArgument { get; set; }
public Table TableArg { get; set; }
public FilePosition FilePosition { get; set; }

private Dictionary<int, string[]> _tableArgLines = new Dictionary<int, string[]>();

public ScenarioStep()
{
}

public ScenarioStep(Text stepText, MultilineText multilineTextArgument, Table tableArg) // TODO: 20100428 jb: remove tableArg!
{
this.Text = stepText.Value;
MultiLineTextArgument = multilineTextArgument == null ? null : multilineTextArgument.Value;
this.TableArg = tableArg;
}

public Table TableArg
{
get
{
if(_tableArgLines.Count == 0)
return null;

return new Table
{
Header = new Row{
Cells = _tableArgLines.Values.First().Select(c => new Cell(new Text(c))).ToArray(),
FilePosition = new FilePosition(_tableArgLines.Keys.First(), 1)
},
Body = _tableArgLines.Skip(1).Select(r => new Row{
Cells = r.Value.Select(c => new Cell(new Text(c))).ToArray(),
FilePosition = new FilePosition(r.Key,1)
}).ToArray()
};
}
set
{
// TODO: 20100428 jb: Setter is probably not needed
_tableArgLines.Clear();
if (value != null)
{
((ICollection<KeyValuePair<int, string[]>>)_tableArgLines).Add(new KeyValuePair<int,string[]>(value.Header.FilePosition.Line, value.Header.Cells.Select(c => c.Value).ToArray()));
foreach (var row in value.Body)
{
((ICollection<KeyValuePair<int, string[]>>)_tableArgLines).Add(new KeyValuePair<int, string[]>(row.FilePosition.Line, row.Cells.Select(c => c.Value).ToArray()));
}
}
}
}

public void AddTableArgRow(string[] row, int linePosition)
{
_tableArgLines.Add(linePosition, row);
}
}

public class ScenarioSteps : List<ScenarioStep>
Expand Down
4 changes: 2 additions & 2 deletions Parser/SyntaxElements/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public Cell()
{
}

public Cell(Text value)
public Cell(string value)
{
Value = value.Value;
Value = value;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Parser/SyntaxElements/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public Tag()
{
}

public Tag(Word name)
public Tag(string name)
{
Name = name.Value;
Name = name;
}
}

Expand Down
Loading

0 comments on commit 065dfd3

Please sign in to comment.