From b74a8f521233dedd85667de1c5ca7b466779d00c Mon Sep 17 00:00:00 2001 From: Simon Diesenreiter Date: Mon, 2 Dec 2024 10:39:18 +0100 Subject: [PATCH] fix: fix some bugs and tests, ref: A24-3 --- TextParser.Tests/TextParserTests.cs | 54 +++++++++++++++++++----- TextParser/LineParser.cs | 3 +- TextParser/Schema/InputSchema.cs | 2 +- TextParser/TextParser.cs | 9 ++-- TextParser/TokenConverter.cs | 27 +++++++----- TextParser/Tokenization/InputProvider.cs | 5 --- 6 files changed, 68 insertions(+), 32 deletions(-) diff --git a/TextParser.Tests/TextParserTests.cs b/TextParser.Tests/TextParserTests.cs index 0cde1ab..2e8d64b 100644 --- a/TextParser.Tests/TextParserTests.cs +++ b/TextParser.Tests/TextParserTests.cs @@ -94,8 +94,8 @@ public class TextParserTests Assert.Equal("dc", (tokens[5] as StringToken)?.GetValue()); } - [Fact] - public void TextParser_TestRepetition() + [Fact] + public void TextParser_TestRepetitionAsRows() { var schemaBuilder = new InputSchemaBuilder(); var schema = schemaBuilder @@ -108,7 +108,7 @@ public class TextParserTests var rows = parser .SetInputText(testInput3) .Parse() - .AsRows(); + .AsRows(); Assert.Equal(3, rows.Count); Assert.Equal(4, rows[0].Length); @@ -116,13 +116,45 @@ public class TextParserTests Assert.Equal(4, rows[0][1]); Assert.Equal(6, rows[0][2]); Assert.Equal(1, rows[0][3]); - Assert.Equal(2, rows[1][0]); - Assert.Equal(4, rows[1][1]); - Assert.Equal(6, rows[1][2]); - Assert.Equal(1, rows[1][3]); - Assert.Equal(2, rows[2][0]); - Assert.Equal(4, rows[2][1]); - Assert.Equal(6, rows[2][2]); - Assert.Equal(1, rows[2][3]); + Assert.Equal(3, rows[1][0]); + Assert.Equal(5, rows[1][1]); + Assert.Equal(7, rows[1][2]); + Assert.Equal(2, rows[1][3]); + Assert.Equal(4, rows[2][0]); + Assert.Equal(6, rows[2][1]); + Assert.Equal(8, rows[2][2]); + 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(); + + 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]); } } diff --git a/TextParser/LineParser.cs b/TextParser/LineParser.cs index 675005f..1a1ce58 100644 --- a/TextParser/LineParser.cs +++ b/TextParser/LineParser.cs @@ -15,6 +15,7 @@ public class LineParser this.delimiters = delimiters ?? new string[] { " " }; this.removeEmptyEntries = removeEmptyEntries; this.schema = schema; + this.context = this.schema.CreateContext(); } private string[] ParseLineIntoWords(string line) @@ -27,7 +28,7 @@ public class LineParser return line.Split(this.delimiters, options); } - public IList ParseLine(string line) + public List ParseLine(string line) { this.context = this.schema.CreateContext(); var words = this.ParseLineIntoWords(line); diff --git a/TextParser/Schema/InputSchema.cs b/TextParser/Schema/InputSchema.cs index 45cab2e..dd5c576 100644 --- a/TextParser/Schema/InputSchema.cs +++ b/TextParser/Schema/InputSchema.cs @@ -50,7 +50,7 @@ public class InputSchema } } - public IList ProcessWordList(string[] words) + public List ProcessWordList(string[] words) { List tokens = new List(); InputProvider inputs = new InputProvider(words); diff --git a/TextParser/TextParser.cs b/TextParser/TextParser.cs index 70bd5ca..b5f9fc6 100644 --- a/TextParser/TextParser.cs +++ b/TextParser/TextParser.cs @@ -9,12 +9,13 @@ public class TextParser : TokenConverter { private LineParser lineParser; private string[] lines; - - private List> rawTokens = new List>(); + private bool removeEmptyEntries; 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) @@ -30,7 +31,7 @@ public class TextParser : TokenConverter public TextParser Parse() { - foreach(var line in this.lines) + foreach (var line in this.lines) { this.rawTokens.Add(this.lineParser.ParseLine(line)); } diff --git a/TextParser/TokenConverter.cs b/TextParser/TokenConverter.cs index b548429..4ec1f1c 100644 --- a/TextParser/TokenConverter.cs +++ b/TextParser/TokenConverter.cs @@ -7,28 +7,35 @@ using Parsing.Tokenization; public class TokenConverter { - private List> rawTokens = new List>(); + protected List> rawTokens = new List>(); public TokenConverter() { } - private List AsGenericCollection() where T : ICollection + private List AsGenericCollection() where T : ICollection, new() { List returnData = new List(); - foreach(var tokenRow in this.rawTokens) + foreach (var tokenRow in this.rawTokens) { T newRow = new T(); - foreach(IToken token in tokenRow) + foreach (IToken token in tokenRow) { - IValueToken valueToken = token as IValueToken; + if (token == null) + { + throw new Exception("No token was provided, but token was expected!"); + } + IValueToken? valueToken = token as IValueToken; if (valueToken == null) { throw new Exception("Provided token is not a ValueToken"); } newRow.Add(valueToken.GetValue()); } + + returnData.Add(newRow); } + return returnData; } public List AsRows() @@ -36,7 +43,7 @@ public class TokenConverter var listRows = this.AsListRows(); var newList = new List(); - foreach(var rowList in listRows) + foreach (var rowList in listRows) { newList.Add(rowList.ToArray()); } @@ -54,7 +61,7 @@ public class TokenConverter var listColumns = this.AsListColumns(); var newList = new List(); - foreach(var columnList in listColumns) + foreach (var columnList in listColumns) { newList.Add(columnList.ToArray()); } @@ -67,14 +74,14 @@ public class TokenConverter var rows = AsListRows(); var columns = new List>(); - for(int i =0; i()); } - foreach(var row in rows) + foreach (var row in rows) { - for(int i = 0; i < row.Count; i++) + for (int i = 0; i < row.Count; i++) { columns[i].Add(row[i]); } diff --git a/TextParser/Tokenization/InputProvider.cs b/TextParser/Tokenization/InputProvider.cs index bafa7a9..7494bb2 100644 --- a/TextParser/Tokenization/InputProvider.cs +++ b/TextParser/Tokenization/InputProvider.cs @@ -39,11 +39,6 @@ public class InputProvider public string YieldWord() { - Console.WriteLine("current words:"); - foreach (var word in words) - { - Console.WriteLine(word); - } if (this.CurrentPosition > this.words.Length) { return string.Empty;