feat: added initial implementation of TextParser, ref: A24-3
This commit is contained in:
93
TextParser.Tests/TextParserTests.cs
Normal file
93
TextParser.Tests/TextParserTests.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
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";
|
||||
|
||||
[Fact]
|
||||
public void TestSimpleRepetition()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Repeat(4)
|
||||
.Expect(InputType.Integer)
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(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 TestSimpleInput()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Expect(InputType.Integer)
|
||||
.Expect(InputType.Integer)
|
||||
.Expect(InputType.Integer)
|
||||
.Expect(InputType.Integer)
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(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 TestNestedRepetition()
|
||||
{
|
||||
var schemaBuilder = new InputSchemaBuilder();
|
||||
var schema = schemaBuilder
|
||||
.Repeat(2)
|
||||
.Expect(InputType.Integer)
|
||||
.Repeat(2)
|
||||
.Expect(InputType.String)
|
||||
.EndRepetition()
|
||||
.EndRepetition()
|
||||
.Build();
|
||||
|
||||
var parser = new TextParser(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());
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace TextParser.Tests;
|
||||
|
||||
using TextParser;
|
||||
|
||||
public class UnitTest1
|
||||
{
|
||||
[Fact]
|
||||
public void Test1()
|
||||
{
|
||||
var hwp = new HelloWorldProvider();
|
||||
Assert.Equal("Hello, Simon!", hwp.GetHelloWorld("Simon"));
|
||||
Assert.Equal("Hello world!", hwp.GetHelloWorld());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user