fix: fix some bugs and tests, ref: A24-3
Some checks failed
CI / linter (9.0.X, ubuntu-latest) (push) Failing after 1m57s
SonarQube Scan / SonarQube Trigger (push) Successful in 1m57s
CI / tests_linux (9.0.X, ubuntu-latest) (push) Has been skipped

This commit is contained in:
Simon Diesenreiter 2024-12-02 10:39:18 +01:00
parent 8707e0da3a
commit b74a8f5212
6 changed files with 68 additions and 32 deletions

View File

@ -95,7 +95,7 @@ public class TextParserTests
} }
[Fact] [Fact]
public void TextParser_TestRepetition() public void TextParser_TestRepetitionAsRows()
{ {
var schemaBuilder = new InputSchemaBuilder(); var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder var schema = schemaBuilder
@ -108,7 +108,7 @@ public class TextParserTests
var rows = parser var rows = parser
.SetInputText(testInput3) .SetInputText(testInput3)
.Parse() .Parse()
.AsRows(); .AsRows<int>();
Assert.Equal(3, rows.Count); Assert.Equal(3, rows.Count);
Assert.Equal(4, rows[0].Length); Assert.Equal(4, rows[0].Length);
@ -116,13 +116,45 @@ public class TextParserTests
Assert.Equal(4, rows[0][1]); Assert.Equal(4, rows[0][1]);
Assert.Equal(6, rows[0][2]); Assert.Equal(6, rows[0][2]);
Assert.Equal(1, rows[0][3]); Assert.Equal(1, rows[0][3]);
Assert.Equal(2, rows[1][0]); Assert.Equal(3, rows[1][0]);
Assert.Equal(4, rows[1][1]); Assert.Equal(5, rows[1][1]);
Assert.Equal(6, rows[1][2]); Assert.Equal(7, rows[1][2]);
Assert.Equal(1, rows[1][3]); Assert.Equal(2, rows[1][3]);
Assert.Equal(2, rows[2][0]); Assert.Equal(4, rows[2][0]);
Assert.Equal(4, rows[2][1]); Assert.Equal(6, rows[2][1]);
Assert.Equal(6, rows[2][2]); Assert.Equal(8, rows[2][2]);
Assert.Equal(1, rows[2][3]); Assert.Equal(3, rows[2][3]);
}
[Fact]
public void TextParser_TestRepetitionAsColumns()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat(4)
.Expect(InputType.Integer)
.EndRepetition()
.Build();
var parser = new TextParser(schema);
var columns = parser
.SetInputText(testInput3)
.Parse()
.AsColumns<int>();
Assert.Equal(4, columns.Count);
Assert.Equal(3, columns[0].Length);
Assert.Equal(2, columns[0][0]);
Assert.Equal(3, columns[0][1]);
Assert.Equal(4, columns[0][2]);
Assert.Equal(4, columns[1][0]);
Assert.Equal(5, columns[1][1]);
Assert.Equal(6, columns[1][2]);
Assert.Equal(6, columns[2][0]);
Assert.Equal(7, columns[2][1]);
Assert.Equal(8, columns[2][2]);
Assert.Equal(1, columns[3][0]);
Assert.Equal(2, columns[3][1]);
Assert.Equal(3, columns[3][2]);
} }
} }

View File

@ -15,6 +15,7 @@ public class LineParser
this.delimiters = delimiters ?? new string[] { " " }; this.delimiters = delimiters ?? new string[] { " " };
this.removeEmptyEntries = removeEmptyEntries; this.removeEmptyEntries = removeEmptyEntries;
this.schema = schema; this.schema = schema;
this.context = this.schema.CreateContext();
} }
private string[] ParseLineIntoWords(string line) private string[] ParseLineIntoWords(string line)
@ -27,7 +28,7 @@ public class LineParser
return line.Split(this.delimiters, options); return line.Split(this.delimiters, options);
} }
public IList<IToken> ParseLine(string line) public List<IToken> ParseLine(string line)
{ {
this.context = this.schema.CreateContext(); this.context = this.schema.CreateContext();
var words = this.ParseLineIntoWords(line); var words = this.ParseLineIntoWords(line);

View File

@ -50,7 +50,7 @@ public class InputSchema
} }
} }
public IList<IToken> ProcessWordList(string[] words) public List<IToken> ProcessWordList(string[] words)
{ {
List<IToken> tokens = new List<IToken>(); List<IToken> tokens = new List<IToken>();
InputProvider inputs = new InputProvider(words); InputProvider inputs = new InputProvider(words);

View File

@ -9,12 +9,13 @@ public class TextParser : TokenConverter
{ {
private LineParser lineParser; private LineParser lineParser;
private string[] lines; private string[] lines;
private bool removeEmptyEntries;
private List<List<IToken>> rawTokens = new List<List<IToken>>();
public TextParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true) : base() public TextParser(InputSchema schema, string[]? delimiters = null, bool removeEmptyEntries = true) : base()
{ {
this.lineParser = new LineParser(schema, delimiters, removeEmptyEntries); this.lineParser = new LineParser(schema, delimiters, removeEmptyEntries);
this.lines = new string[] { };
this.removeEmptyEntries = removeEmptyEntries;
} }
public TextParser SetInputText(string text) public TextParser SetInputText(string text)

View File

@ -7,13 +7,13 @@ using Parsing.Tokenization;
public class TokenConverter public class TokenConverter
{ {
private List<List<IToken>> rawTokens = new List<List<IToken>>(); protected List<List<IToken>> rawTokens = new List<List<IToken>>();
public TokenConverter() public TokenConverter()
{ {
} }
private List<T> AsGenericCollection<T, U>() where T : ICollection<U> private List<T> AsGenericCollection<T, U>() where T : ICollection<U>, new()
{ {
List<T> returnData = new List<T>(); List<T> returnData = new List<T>();
foreach (var tokenRow in this.rawTokens) foreach (var tokenRow in this.rawTokens)
@ -21,14 +21,21 @@ public class TokenConverter
T newRow = new T(); T newRow = new T();
foreach (IToken token in tokenRow) foreach (IToken token in tokenRow)
{ {
IValueToken<U> valueToken = token as IValueToken<U>; if (token == null)
{
throw new Exception("No token was provided, but token was expected!");
}
IValueToken<U>? valueToken = token as IValueToken<U>;
if (valueToken == null) if (valueToken == null)
{ {
throw new Exception("Provided token is not a ValueToken"); throw new Exception("Provided token is not a ValueToken");
} }
newRow.Add(valueToken.GetValue()); newRow.Add(valueToken.GetValue());
} }
returnData.Add(newRow);
} }
return returnData;
} }
public List<T[]> AsRows<T>() public List<T[]> AsRows<T>()

View File

@ -39,11 +39,6 @@ public class InputProvider
public string YieldWord() public string YieldWord()
{ {
Console.WriteLine("current words:");
foreach (var word in words)
{
Console.WriteLine(word);
}
if (this.CurrentPosition > this.words.Length) if (this.CurrentPosition > this.words.Length)
{ {
return string.Empty; return string.Empty;