generated from Templates/Dotnet_Library
fix: fix some bugs and tests, ref: A24-3
This commit is contained in:
parent
8707e0da3a
commit
b74a8f5212
@ -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<int>();
|
||||
|
||||
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<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]);
|
||||
}
|
||||
}
|
||||
|
@ -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<IToken> ParseLine(string line)
|
||||
public List<IToken> ParseLine(string line)
|
||||
{
|
||||
this.context = this.schema.CreateContext();
|
||||
var words = this.ParseLineIntoWords(line);
|
||||
|
@ -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>();
|
||||
InputProvider inputs = new InputProvider(words);
|
||||
|
@ -9,12 +9,13 @@ public class TextParser : TokenConverter
|
||||
{
|
||||
private LineParser lineParser;
|
||||
private string[] lines;
|
||||
|
||||
private List<List<IToken>> rawTokens = new List<List<IToken>>();
|
||||
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));
|
||||
}
|
||||
|
@ -7,28 +7,35 @@ using Parsing.Tokenization;
|
||||
|
||||
public class TokenConverter
|
||||
{
|
||||
private List<List<IToken>> rawTokens = new List<List<IToken>>();
|
||||
protected List<List<IToken>> rawTokens = new List<List<IToken>>();
|
||||
|
||||
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>();
|
||||
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<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)
|
||||
{
|
||||
throw new Exception("Provided token is not a ValueToken");
|
||||
}
|
||||
newRow.Add(valueToken.GetValue());
|
||||
}
|
||||
|
||||
returnData.Add(newRow);
|
||||
}
|
||||
return returnData;
|
||||
}
|
||||
|
||||
public List<T[]> AsRows<T>()
|
||||
@ -36,7 +43,7 @@ public class TokenConverter
|
||||
var listRows = this.AsListRows<T>();
|
||||
var newList = new List<T[]>();
|
||||
|
||||
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<T>();
|
||||
var newList = new List<T[]>();
|
||||
|
||||
foreach(var columnList in listColumns)
|
||||
foreach (var columnList in listColumns)
|
||||
{
|
||||
newList.Add(columnList.ToArray());
|
||||
}
|
||||
@ -67,14 +74,14 @@ public class TokenConverter
|
||||
var rows = AsListRows<T>();
|
||||
|
||||
var columns = new List<List<T>>();
|
||||
for(int i =0; i<rows[0].Count; i++)
|
||||
for (int i = 0; i < rows[0].Count; i++)
|
||||
{
|
||||
columns.Add(new List<T>());
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user