Skip to content

Commit

Permalink
[.NET] Replace use of arrays with Enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
obligaron committed Jan 3, 2025
1 parent 47c481a commit 096e606
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/Background.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Gherkin.Ast;

public class Background(Location location, string keyword, string name, string description, Step[] steps)
public class Background(Location location, string keyword, string name, string description, IEnumerable<Step> steps)
: StepsContainer(location, keyword, name, description, steps);
4 changes: 2 additions & 2 deletions dotnet/Gherkin/Ast/DataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ public class DataTable : StepArgument, IHasRows, IHasLocation
public Location Location { get; private set; }
public IEnumerable<TableRow> Rows { get; private set; }

public DataTable(TableRow[] rows)
public DataTable(List<TableRow> rows)
{
if (rows == null) throw new ArgumentNullException("rows");
if (rows.Length == 0) throw new ArgumentException("DataTable must have at least one row", "rows");
if (rows.Count == 0) throw new ArgumentException("DataTable must have at least one row", "rows");

Location = rows[0].Location;
Rows = rows;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/Examples.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class Examples(Tag[] tags, Location location, string keyword, string name, string description, TableRow header, TableRow[] body)
public class Examples(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, TableRow header, IEnumerable<TableRow> body)
: IHasLocation, IHasDescription, IHasRows, IHasTags
{
public IEnumerable<Tag> Tags { get; } = tags;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/Feature.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class Feature(Tag[] tags, Location location, string language, string keyword, string name, string description, IHasLocation[] children)
public class Feature(IEnumerable<Tag> tags, Location location, string language, string keyword, string name, string description, IEnumerable<IHasLocation> children)
: IHasLocation, IHasDescription, IHasTags, IHasChildren
{
public IEnumerable<Tag> Tags { get; } = tags;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/GherkinDocument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class GherkinDocument(Feature feature, Comment[] comments)
public class GherkinDocument(Feature feature, IEnumerable<Comment> comments)
{
public Feature Feature { get; } = feature;
public IEnumerable<Comment> Comments { get; } = comments;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/Rule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class Rule(Tag[] tags, Location location, string keyword, string name, string description, IHasLocation[] children)
public class Rule(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, IEnumerable<IHasLocation> children)
: IHasLocation, IHasDescription, IHasChildren, IHasTags
{
public Location Location { get; } = location;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/Scenario.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class Scenario(Tag[] tags, Location location, string keyword, string name, string description, Step[] steps, Examples[] examples)
public class Scenario(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, IEnumerable<Step> steps, IEnumerable<Examples> examples)
: StepsContainer(location, keyword, name, description, steps), IHasTags
{
public IEnumerable<Tag> Tags { get; } = tags;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/StepsContainer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public abstract class StepsContainer(Location location, string keyword, string name, string description, Step[] steps)
public abstract class StepsContainer(Location location, string keyword, string name, string description, IEnumerable<Step> steps)
: IHasLocation, IHasDescription, IHasSteps
{
public Location Location { get; } = location;
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Ast/TableRow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Gherkin.Ast;

public class TableRow(Location location, TableCell[] cells) : IHasLocation
public class TableRow(Location location, IEnumerable<TableCell> cells) : IHasLocation
{
public Location Location { get; } = location;
public IEnumerable<TableCell> Cells { get; } = cells;
Expand Down
40 changes: 20 additions & 20 deletions dotnet/Gherkin/AstBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private object GetTransformedNode(AstNode node)

var description = GetDescription(scenarioNode);
var steps = GetSteps(scenarioNode);
var examples = scenarioNode.GetItems<Examples>(RuleType.ExamplesDefinition).ToArray();
List<Examples> examples = [.. scenarioNode.GetItems<Examples>(RuleType.ExamplesDefinition)];
return CreateScenario(tags, GetLocation(scenarioLine), scenarioLine.MatchedKeyword, scenarioLine.MatchedText, description, steps, examples, node);
}
case RuleType.ExamplesDefinition:
Expand All @@ -102,9 +102,9 @@ private object GetTransformedNode(AstNode node)
var examplesLine = examplesNode.GetToken(TokenType.ExamplesLine);
var description = GetDescription(examplesNode);

var allRows = examplesNode.GetSingle<TableRow[]>(RuleType.ExamplesTable);
var header = allRows != null ? allRows.First() : null;
var rows = allRows != null ? allRows.Skip(1).ToArray() : null;
var allRows = examplesNode.GetSingle<List<TableRow>>(RuleType.ExamplesTable);
var header = allRows != null ? allRows[0] : null;
var rows = allRows != null ? allRows.Skip(1).ToList() : null;
return CreateExamples(tags, GetLocation(examplesLine), examplesLine.MatchedKeyword, examplesLine.MatchedText, description, header, rows, node);
}
case RuleType.ExamplesTable:
Expand Down Expand Up @@ -139,7 +139,7 @@ private object GetTransformedNode(AstNode node)
if (featureLine.MatchedGherkinDialect == null) return null;
var language = featureLine.MatchedGherkinDialect.Language;

return CreateFeature(tags, GetLocation(featureLine), language, featureLine.MatchedKeyword, featureLine.MatchedText, description, children.ToArray(), node);
return CreateFeature(tags, GetLocation(featureLine), language, featureLine.MatchedKeyword, featureLine.MatchedText, description, children, node);
}
case RuleType.Rule:
{
Expand All @@ -157,7 +157,7 @@ private object GetTransformedNode(AstNode node)
var description = GetDescription(header);
if (ruleLine.MatchedGherkinDialect == null) return null;

return CreateRule(tags, GetLocation(ruleLine), ruleLine.MatchedKeyword, ruleLine.MatchedText, description, children.ToArray(), node);
return CreateRule(tags, GetLocation(ruleLine), ruleLine.MatchedKeyword, ruleLine.MatchedText, description, children, node);
}
case RuleType.GherkinDocument:
{
Expand All @@ -178,12 +178,12 @@ protected virtual StepKeywordType GetKeywordType(Token stepLine)
return stepKeywordType.Value;
}

protected virtual Background CreateBackground(Location location, string keyword, string name, string description, Step[] steps, AstNode node)
protected virtual Background CreateBackground(Location location, string keyword, string name, string description, IEnumerable<Step> steps, AstNode node)
{
return new Background(location, keyword, name, description, steps);
}

protected virtual DataTable CreateDataTable(TableRow[] rows, AstNode node)
protected virtual DataTable CreateDataTable(List<TableRow> rows, AstNode node)
{
return new DataTable(rows);
}
Expand All @@ -193,12 +193,12 @@ protected virtual Comment CreateComment(Location location, string text)
return new Comment(location, text);
}

protected virtual Examples CreateExamples(Tag[] tags, Location location, string keyword, string name, string description, TableRow header, TableRow[] body, AstNode node)
protected virtual Examples CreateExamples(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, TableRow header, IEnumerable<TableRow> body, AstNode node)
{
return new Examples(tags, location, keyword, name, description, header, body);
}

protected virtual Scenario CreateScenario(Tag[] tags, Location location, string keyword, string name, string description, Step[] steps, Examples[] examples, AstNode node)
protected virtual Scenario CreateScenario(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, IEnumerable<Step> steps, IEnumerable<Examples> examples, AstNode node)
{
return new Scenario(tags, location, keyword, name, description, steps, examples);
}
Expand All @@ -213,17 +213,17 @@ protected virtual Step CreateStep(Location location, string keyword, StepKeyword
return new Step(location, keyword, keywordType, text, argument);
}

protected virtual GherkinDocument CreateGherkinDocument(Feature feature, Comment[] gherkinDocumentComments, AstNode node)
protected virtual GherkinDocument CreateGherkinDocument(Feature feature, IEnumerable<Comment> gherkinDocumentComments, AstNode node)
{
return new GherkinDocument(feature, gherkinDocumentComments);
}

protected virtual Feature CreateFeature(Tag[] tags, Location location, string language, string keyword, string name, string description, IHasLocation[] children, AstNode node)
protected virtual Feature CreateFeature(IEnumerable<Tag> tags, Location location, string language, string keyword, string name, string description, IEnumerable<IHasLocation> children, AstNode node)
{
return new Feature(tags, location, language, keyword, name, description, children);
}

protected virtual Rule CreateRule(Tag[] tags, Location location, string keyword, string name, string description, IHasLocation[] children, AstNode node)
protected virtual Rule CreateRule(IEnumerable<Tag> tags, Location location, string keyword, string name, string description, IEnumerable<IHasLocation> children, AstNode node)
{
return new Rule(tags, location, keyword, name, description, children);
}
Expand All @@ -238,7 +238,7 @@ protected Location CreateLocation(int line, int column)
return new Location(line, column);
}

protected virtual TableRow CreateTableRow(Location location, TableCell[] cells, AstNode node)
protected virtual TableRow CreateTableRow(Location location, IEnumerable<TableCell> cells, AstNode node)
{
return new TableRow(location, cells);
}
Expand All @@ -253,7 +253,7 @@ private Location GetLocation(Token token, int column = 0)
return column == 0 ? token.Location : CreateLocation(token.Location.Line, column);
}

private Tag[] GetTags(AstNode node)
private IEnumerable<Tag> GetTags(AstNode node)
{
var tagsNode = node.GetSingle<AstNode>(RuleType.Tags);
if (tagsNode == null)
Expand All @@ -265,10 +265,10 @@ private Tag[] GetTags(AstNode node)
foreach (var matchedItem in line.MatchedItems)
tags.Add(CreateTag(GetLocation(line, matchedItem.Column), matchedItem.Text, tagsNode));
}
return tags.ToArray();
return tags;
}

private TableRow[] GetTableRows(AstNode node)
private List<TableRow> GetTableRows(AstNode node)
{
var rows = new List<TableRow>();
int cellCount = 0;
Expand All @@ -290,17 +290,17 @@ private TableRow[] GetTableRows(AstNode node)
}
rows.Add(CreateTableRow(rowLocation, cells, node));
}
return rows.ToArray();
return rows;
}

protected virtual void HandleAstError(string message, Location location)
{
throw new AstBuilderException(message, location);
}

private static Step[] GetSteps(AstNode scenarioDefinitionNode)
private static List<Step> GetSteps(AstNode scenarioDefinitionNode)
{
return scenarioDefinitionNode.GetItems<Step>(RuleType.Step).ToArray();
return [..scenarioDefinitionNode.GetItems<Step>(RuleType.Step)];
}

private static string GetDescription(AstNode scenarioDefinitionNode)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Gherkin/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Token(Location location)
public TokenType MatchedType { get; set; }
public string MatchedKeyword { get; set; }
public string MatchedText { get; set; }
public GherkinLineSpan[] MatchedItems { get; set; }
public IEnumerable<GherkinLineSpan> MatchedItems { get; set; }
public int MatchedIndent { get; set; }
public GherkinDialect MatchedGherkinDialect { get; set; }
public Location Location { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions dotnet/Gherkin/TokenMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Reset()
currentDialect = dialectProvider.DefaultDialect;
}

protected virtual void SetTokenMatched(Token token, TokenType matchedType, string text = null, string keyword = null, int? indent = null, GherkinLineSpan[] items = null)
protected virtual void SetTokenMatched(Token token, TokenType matchedType, string text = null, string keyword = null, int? indent = null, IEnumerable<GherkinLineSpan> items = null)
{
token.MatchedType = matchedType;
token.MatchedKeyword = keyword;
Expand Down Expand Up @@ -113,7 +113,7 @@ public bool Match_TagLine(Token token)
{
if (token.Line.StartsWith(GherkinLanguageConstants.TAG_PREFIX))
{
SetTokenMatched(token, TokenType.TagLine, items: token.Line.GetTags().ToArray());
SetTokenMatched(token, TokenType.TagLine, items: token.Line.GetTags());
return true;
}
return false;
Expand Down Expand Up @@ -212,7 +212,7 @@ public bool Match_TableRow(Token token)
{
if (token.Line.StartsWith(GherkinLanguageConstants.TABLE_CELL_SEPARATOR))
{
SetTokenMatched(token, TokenType.TableRow, items: token.Line.GetTableCells().ToArray());
SetTokenMatched(token, TokenType.TableRow, items: token.Line.GetTableCells());
return true;
}
return false;
Expand Down

0 comments on commit 096e606

Please sign in to comment.