TextParser/TextParser.Tests/TextParserTests.cs

447 lines
18 KiB
C#
Raw Normal View History

namespace TextParser.Tests;
using Parsing;
using Parsing.Data;
using Parsing.Schema;
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";
private const string testInput5 = @"asdfnums(2,5,3)ght
cv strs(test) jh 4,3,2
34,54,2nums(2,8) strs(aa,ab,ba,bb)aa,bb";
private const string testInput6 = @"adfdf1()324ddf3()svsdvs
davnsldkvjs2()m23423()
mcsodkcn owdjnfj 1() asdfnad 23234 2() sdvsdv";
private const string testInput7 = @"adfdf1()324ddf3()()()svsdvs
davnsldkvjs2()()m23423()()()
mcsodkcn owdjnfj 1() asdfnad 23234 2()() sdvsdv";
private const string testInput8 = @"2 4 6 4 1 3 5 4 7 2 4 6 8 3";
private const string testInput9 = @"2 4 6 4 1
3 5 4 7 6
4 6 8 3 9";
private const string testInput10 = @"abc
bca
cab";
private const string testInput11 = @"2 x y 4 x y 6 x y 4 x y 1 x y";
[Fact]
public void LineParser_TestSimpleRepetition()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat(4)
.Expect(InputType.Integer)
.EndRepetition()
.Build();
var parser = new LineParser<InputSchemaContext>(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<InputSchemaContext>(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<InputSchemaContext>(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<InputSchemaContext>(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<InputSchemaContext>(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<InputSchemaContext>(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());
}
[Fact]
public void FragmentParser_SimpleTest()
{
var schemaBuilder = new FragmentSchemaBuilder();
var schema = schemaBuilder
.StartOptions()
.Option()
.Expect("nums(")
.Expect(InputType.Integer, "num")
.Repeat()
.Expect(",")
.Expect(InputType.Integer, "num")
.EndRepetition()
.Expect(")")
.Option()
.Expect("strs(")
.Expect(InputType.String, "str")
.Repeat()
.Expect(",")
.Expect(InputType.String, "str")
.EndRepetition()
.Expect(")")
.EndOptions()
.Build();
var parser = TextParser.Create(schema);
var fragmentData = parser
.SetInputText(testInput5)
.Parse()
.AsFragments();
var convertedData = fragmentData
.ConvertAll((Fragment f) =>
{
int numSum = 0;
foreach (var numString in f["num"])
{
numSum += int.Parse(numString);
}
return f["num"].Count + f["str"].Count + numSum;
});
Assert.Equal(4, fragmentData.Count);
Assert.Equal(3, fragmentData[0]["num"].Count);
Assert.Single(fragmentData[1]["str"]);
Assert.Equal(2, fragmentData[2]["num"].Count);
Assert.Equal(4, fragmentData[3]["str"].Count);
Assert.Equal(13, convertedData[0]);
Assert.Equal(1, convertedData[1]);
Assert.Equal(12, convertedData[2]);
Assert.Equal(4, convertedData[3]);
}
[Fact]
public void FragmentParser_LiteralTest()
{
var schemaBuilder = new FragmentSchemaBuilder();
var schema = schemaBuilder
.StartOptions()
.Option()
.Expect("1()", "option1")
.Option()
.Expect("2()", "option2")
.Option()
.Expect("3()", "option3")
.EndOptions()
.Build();
var parser = TextParser.Create(schema);
var fragmentData = parser
.SetInputText(testInput6)
.Parse()
.AsFragments();
var convertedData = fragmentData
.ConvertAll((Fragment f) =>
{
bool saw1 = f.ContainsKey("option1") ? f["option1"].Count > 0 : false;
bool saw2 = f.ContainsKey("option2") ? f["option2"].Count() > 0 : false;
bool saw3 = f.ContainsKey("option3") ? f["option3"].Count() > 0 : false;
int indicator = 0;
if (saw1)
{
indicator += 1;
}
if (saw2)
{
indicator += 2;
}
if (saw3)
{
indicator += 4;
}
return indicator;
});
Assert.Equal(6, convertedData.Count);
Assert.Equal(1, convertedData[0]);
Assert.Equal(4, convertedData[1]);
Assert.Equal(2, convertedData[2]);
Assert.Equal(4, convertedData[3]);
Assert.Equal(1, convertedData[4]);
Assert.Equal(2, convertedData[5]);
}
[Fact]
public void DataManipulator_SimpleOneDimensionalTest()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat()
.Expect(InputType.Integer)
.EndRepetition()
.Build();
var parser = new TextParser<InputSchemaContext>(schema);
var row = parser
.SetInputText(testInput8)
.Parse()
.AsSingleStream<int>();
var searchSequence = new List<int> { 4, 6 };
var manipulator = DefaultOneDimensionalManipulator.Create(row);
var searchResults = manipulator.FindInSet(searchSequence);
Assert.Equal(3, searchResults.Count);
Assert.Equal(1, searchResults[0].DataIndex.GetIndices()[0]);
Assert.Equal(3, searchResults[1].DataIndex.GetIndices()[0]);
Assert.Equal(10, searchResults[2].DataIndex.GetIndices()[0]);
Assert.Equal(Direction.Forward, searchResults[0].Direction);
Assert.Equal(Direction.Backward, searchResults[1].Direction);
Assert.Equal(Direction.Forward, searchResults[2].Direction);
}
[Fact]
public void DataManipulator_SimpleTwoDimensionalTest()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat()
.Expect(InputType.Integer)
.EndRepetition()
.Build();
var parser = new TextParser<InputSchemaContext>(schema);
var row = parser
.SetInputText(testInput9)
.Parse()
.AsListRows<int>();
var searchSequence = new List<int> { 4, 6 };
var manipulator = DefaultTwoDimensionalManipulator.Create(row);
var searchResults = manipulator.FindInSet(searchSequence);
Assert.Equal(6, searchResults.Count);
Assert.Equal(0, searchResults[0].DataIndex.GetIndices()[0]);
Assert.Equal(0, searchResults[0].DataIndex.GetIndices()[1]);
Assert.Equal(2, searchResults[1].DataIndex.GetIndices()[0]);
Assert.Equal(1, searchResults[1].DataIndex.GetIndices()[1]);
Assert.Equal(2, searchResults[2].DataIndex.GetIndices()[0]);
Assert.Equal(1, searchResults[2].DataIndex.GetIndices()[1]);
Assert.Equal(1, searchResults[3].DataIndex.GetIndices()[0]);
Assert.Equal(2, searchResults[3].DataIndex.GetIndices()[1]);
Assert.Equal(3, searchResults[4].DataIndex.GetIndices()[0]);
Assert.Equal(2, searchResults[4].DataIndex.GetIndices()[1]);
Assert.Equal(3, searchResults[5].DataIndex.GetIndices()[0]);
Assert.Equal(2, searchResults[5].DataIndex.GetIndices()[1]);
Assert.Equal(Direction.E, searchResults[0].Direction);
Assert.Equal(Direction.N, searchResults[1].Direction);
Assert.Equal(Direction.SW, searchResults[2].Direction);
Assert.Equal(Direction.E, searchResults[3].Direction);
Assert.Equal(Direction.SE, searchResults[4].Direction);
Assert.Equal(Direction.W, searchResults[5].Direction);
}
[Fact]
public void TextParser_TestReadingChars()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat()
.Expect(InputType.Char)
.EndRepetition()
.Build();
var parser = new TextParser<InputSchemaContext>(schema);
var row = parser
.SetInputText(testInput10)
.Parse()
.AsListRows<string>();
Assert.Equal(3, row.Count);
Assert.Equal("a", row[0][0]);
Assert.Equal(3, row[0].Count);
Assert.Equal(3, row[1].Count);
Assert.Equal(3, row[2].Count);
}
[Fact]
public void TextParser_TestFilter()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
.Repeat()
.Expect(InputType.Integer)
.Expect(InputType.Char)
.Expect(InputType.Char)
.EndRepetition()
.Build();
var parser = new TextParser<InputSchemaContext>(schema);
var numbers = parser
.SetInputText(testInput11)
.Parse()
.Filter(InputType.Integer)
.AsSingleStream<int>();
Assert.Equal(5, numbers.Count);
Assert.Equal(2, numbers[0]);
Assert.Equal(4, numbers[1]);
Assert.Equal(6, numbers[2]);
Assert.Equal(4, numbers[3]);
Assert.Equal(1, numbers[4]);
}
}