generated from Templates/Dotnet_Library
203 lines
8.0 KiB
C#
203 lines
8.0 KiB
C#
namespace TextParser.Tests;
|
|
|
|
using Parsing;
|
|
using Parsing.Schema;
|
|
using Parsing.Schema.BuildingBlocks;
|
|
using Parsing.Tokenization;
|
|
|
|
public class TextParserTests
|
|
{
|
|
private const string testInput1 = "2 4 6 8";
|
|
private const string testInput2 = "2 ab ba 8 cd dc";
|
|
private const string testInput3 = @"2 4 6 1
|
|
3 5 7 2
|
|
4 6 8 3";
|
|
private const string testInput4 = @"2 ab ba fd er sd
|
|
8 cd dc
|
|
7 uh 6 yp rt";
|
|
|
|
[Fact]
|
|
public void LineParser_TestSimpleRepetition()
|
|
{
|
|
var schemaBuilder = new InputSchemaBuilder();
|
|
var schema = schemaBuilder
|
|
.Repeat(4)
|
|
.Expect(InputType.Integer)
|
|
.EndRepetition()
|
|
.Build();
|
|
|
|
var parser = new LineParser(schema);
|
|
var tokens = parser.ParseLine(testInput1);
|
|
|
|
Assert.Equal(4, tokens.Count);
|
|
Assert.Equal(InputType.Integer, tokens[0].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[1].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[2].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[3].GetInputType());
|
|
Assert.Equal(2, (tokens[0] as IntegerToken)?.GetValue());
|
|
Assert.Equal(4, (tokens[1] as IntegerToken)?.GetValue());
|
|
Assert.Equal(6, (tokens[2] as IntegerToken)?.GetValue());
|
|
Assert.Equal(8, (tokens[3] as IntegerToken)?.GetValue());
|
|
}
|
|
|
|
[Fact]
|
|
public void LineParser_TestSimpleInput()
|
|
{
|
|
var schemaBuilder = new InputSchemaBuilder();
|
|
var schema = schemaBuilder
|
|
.Expect(InputType.Integer)
|
|
.Expect(InputType.Integer)
|
|
.Expect(InputType.Integer)
|
|
.Expect(InputType.Integer)
|
|
.Build();
|
|
|
|
var parser = new LineParser(schema);
|
|
var tokens = parser.ParseLine(testInput1);
|
|
|
|
Assert.Equal(4, tokens.Count);
|
|
Assert.Equal(InputType.Integer, tokens[0].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[1].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[2].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[3].GetInputType());
|
|
Assert.Equal(2, (tokens[0] as IntegerToken)?.GetValue());
|
|
Assert.Equal(4, (tokens[1] as IntegerToken)?.GetValue());
|
|
Assert.Equal(6, (tokens[2] as IntegerToken)?.GetValue());
|
|
Assert.Equal(8, (tokens[3] as IntegerToken)?.GetValue());
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void LineParser_TestNestedRepetition()
|
|
{
|
|
var schemaBuilder = new InputSchemaBuilder();
|
|
var schema = schemaBuilder
|
|
.Repeat(2)
|
|
.Expect(InputType.Integer)
|
|
.Repeat(2)
|
|
.Expect(InputType.String)
|
|
.EndRepetition()
|
|
.EndRepetition()
|
|
.Build();
|
|
|
|
var parser = new LineParser(schema);
|
|
var tokens = parser.ParseLine(testInput2);
|
|
|
|
Assert.Equal(6, tokens.Count);
|
|
Assert.Equal(InputType.Integer, tokens[0].GetInputType());
|
|
Assert.Equal(InputType.String, tokens[1].GetInputType());
|
|
Assert.Equal(InputType.String, tokens[2].GetInputType());
|
|
Assert.Equal(InputType.Integer, tokens[3].GetInputType());
|
|
Assert.Equal(InputType.String, tokens[4].GetInputType());
|
|
Assert.Equal(InputType.String, tokens[5].GetInputType());
|
|
Assert.Equal(2, (tokens[0] as IntegerToken)?.GetValue());
|
|
Assert.Equal("ab", (tokens[1] as StringToken)?.GetValue());
|
|
Assert.Equal("ba", (tokens[2] as StringToken)?.GetValue());
|
|
Assert.Equal(8, (tokens[3] as IntegerToken)?.GetValue());
|
|
Assert.Equal("cd", (tokens[4] as StringToken)?.GetValue());
|
|
Assert.Equal("dc", (tokens[5] as StringToken)?.GetValue());
|
|
}
|
|
|
|
[Fact]
|
|
public void TextParser_TestRepetitionAsRows()
|
|
{
|
|
var schemaBuilder = new InputSchemaBuilder();
|
|
var schema = schemaBuilder
|
|
.Repeat(4)
|
|
.Expect(InputType.Integer)
|
|
.EndRepetition()
|
|
.Build();
|
|
|
|
var parser = new TextParser(schema);
|
|
var rows = parser
|
|
.SetInputText(testInput3)
|
|
.Parse()
|
|
.AsRows<int>();
|
|
|
|
Assert.Equal(3, rows.Count);
|
|
Assert.Equal(4, rows[0].Length);
|
|
Assert.Equal(2, rows[0][0]);
|
|
Assert.Equal(4, rows[0][1]);
|
|
Assert.Equal(6, rows[0][2]);
|
|
Assert.Equal(1, rows[0][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]);
|
|
}
|
|
|
|
[Fact]
|
|
public void TextParser_TestGreedyRepetitionAsRows()
|
|
{
|
|
var schemaBuilder = new InputSchemaBuilder();
|
|
var schema = schemaBuilder
|
|
.Repeat()
|
|
.Expect(InputType.Integer)
|
|
.Repeat()
|
|
.Expect(InputType.String)
|
|
.EndRepetition()
|
|
.EndRepetition()
|
|
.Build();
|
|
|
|
var parser = new TextParser(schema);
|
|
var rows = parser
|
|
.SetInputText(testInput4)
|
|
.Parse()
|
|
.AsRawData();
|
|
|
|
Assert.Equal(3, rows.Count);
|
|
Assert.Equal(6, rows[0].Count);
|
|
Assert.Equal(3, rows[1].Count);
|
|
Assert.Equal(5, rows[2].Count);
|
|
Assert.Equal(InputType.Integer, rows[0][0].GetInputType());
|
|
Assert.Equal(InputType.String, rows[0][1].GetInputType());
|
|
Assert.Equal(InputType.String, rows[0][2].GetInputType());
|
|
Assert.Equal(InputType.String, rows[0][3].GetInputType());
|
|
Assert.Equal(InputType.String, rows[0][4].GetInputType());
|
|
Assert.Equal(InputType.String, rows[0][5].GetInputType());
|
|
Assert.Equal(InputType.Integer, rows[1][0].GetInputType());
|
|
Assert.Equal(InputType.String, rows[1][1].GetInputType());
|
|
Assert.Equal(InputType.String, rows[1][2].GetInputType());
|
|
Assert.Equal(InputType.Integer, rows[2][0].GetInputType());
|
|
Assert.Equal(InputType.String, rows[2][1].GetInputType());
|
|
Assert.Equal(InputType.Integer, rows[2][2].GetInputType());
|
|
Assert.Equal(InputType.String, rows[2][3].GetInputType());
|
|
Assert.Equal(InputType.String, rows[2][4].GetInputType());
|
|
}
|
|
}
|