feat: add text parser and output format options, ref: A24-3

This commit is contained in:
2024-12-02 01:22:20 -08:00
parent a4e4ee2b85
commit 8707e0da3a
5 changed files with 193 additions and 26 deletions

View File

@@ -9,9 +9,12 @@ 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";
[Fact]
public void TestSimpleRepetition()
public void LineParser_TestSimpleRepetition()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
@@ -20,7 +23,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new TextParser(schema);
var parser = new LineParser(schema);
var tokens = parser.ParseLine(testInput1);
Assert.Equal(4, tokens.Count);
@@ -35,7 +38,7 @@ public class TextParserTests
}
[Fact]
public void TestSimpleInput()
public void LineParser_TestSimpleInput()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
@@ -45,7 +48,7 @@ public class TextParserTests
.Expect(InputType.Integer)
.Build();
var parser = new TextParser(schema);
var parser = new LineParser(schema);
var tokens = parser.ParseLine(testInput1);
Assert.Equal(4, tokens.Count);
@@ -57,11 +60,11 @@ public class TextParserTests
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 TestNestedRepetition()
public void LineParser_TestNestedRepetition()
{
var schemaBuilder = new InputSchemaBuilder();
var schema = schemaBuilder
@@ -73,7 +76,7 @@ public class TextParserTests
.EndRepetition()
.Build();
var parser = new TextParser(schema);
var parser = new LineParser(schema);
var tokens = parser.ParseLine(testInput2);
Assert.Equal(6, tokens.Count);
@@ -90,4 +93,36 @@ public class TextParserTests
Assert.Equal("cd", (tokens[4] as StringToken)?.GetValue());
Assert.Equal("dc", (tokens[5] as StringToken)?.GetValue());
}
[Fact]
public void TextParser_TestRepetition()
{
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();
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(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]);
}
}