feat: Parser Combinator库和词法分析器 (#2)
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/2 Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
169
CanonSharp.Tests/TextTests/TextParserTests.cs
Normal file
169
CanonSharp.Tests/TextTests/TextParserTests.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using CanonSharp.Combinator.Abstractions;
|
||||
using CanonSharp.Combinator.Extensions;
|
||||
using CanonSharp.Combinator.Text;
|
||||
using CanonSharp.Common.Scanner;
|
||||
using CanonSharp.Tests.Utils;
|
||||
using static CanonSharp.Combinator.Text.TextParserBuilder;
|
||||
|
||||
namespace CanonSharp.Tests.TextTests;
|
||||
|
||||
public class TextParserTests : ParserTestsBase
|
||||
{
|
||||
[Fact]
|
||||
public void CharTest()
|
||||
{
|
||||
ValidateSuccessfulResult(Char('a'), 'a', "abc");
|
||||
ValidateSuccessfulResult(CharIgnoreCase('a'), 'a', "abc");
|
||||
ValidateSuccessfulResult(CharIgnoreCase('a'), 'A', "ABC");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a', "a")]
|
||||
[InlineData('b', "b")]
|
||||
[InlineData('c', "c")]
|
||||
[InlineData('d', "d")]
|
||||
public void OneOfTest(char except, string input)
|
||||
{
|
||||
Parser<char, char> parser = OneOf("abcd");
|
||||
ValidateSuccessfulResult(parser, except, input);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a', "a")]
|
||||
[InlineData('b', "b")]
|
||||
[InlineData('c', "c")]
|
||||
[InlineData('d', "d")]
|
||||
[InlineData('A', "A")]
|
||||
[InlineData('B', "B")]
|
||||
[InlineData('C', "C")]
|
||||
[InlineData('D', "D")]
|
||||
public void OneOfIgnoreCaseTest(char except, string input)
|
||||
{
|
||||
Parser<char, char> parser = OneOfIgnoreCase("abcd");
|
||||
ValidateSuccessfulResult(parser, except, input);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("hello,world.")]
|
||||
[InlineData("HELLO,WORLD.")]
|
||||
[InlineData("Hello,world.")]
|
||||
[InlineData("Hello,World.")]
|
||||
public void StringIgnoreCaseTest(string literalValue)
|
||||
{
|
||||
Parser<char, string> parser = StringIgnoreCase("hello,world.");
|
||||
ValidateSuccessfulResult(parser, literalValue, literalValue);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('0')]
|
||||
[InlineData('5')]
|
||||
[InlineData('9')]
|
||||
public void RangeTest(char except)
|
||||
{
|
||||
Parser<char, char> parser = Range('0', '9');
|
||||
ValidateSuccessfulResult(parser, except, except.ToString());
|
||||
|
||||
ValidateFailedResult(parser, "abc");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a')]
|
||||
[InlineData('A')]
|
||||
[InlineData('z')]
|
||||
[InlineData('测')]
|
||||
public void LetterTest(char except)
|
||||
{
|
||||
ValidateSuccessfulResult(Letter(), except, except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('0')]
|
||||
[InlineData(',')]
|
||||
[InlineData('%')]
|
||||
public void FailedLetterTest(char except)
|
||||
{
|
||||
ValidateFailedResult(Letter(), except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('0')]
|
||||
public void DigitTest(char except)
|
||||
{
|
||||
ValidateSuccessfulResult(Digit(), except, except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a')]
|
||||
[InlineData('A')]
|
||||
[InlineData('z')]
|
||||
[InlineData('测')]
|
||||
[InlineData(',')]
|
||||
[InlineData('%')]
|
||||
public void FailedDigitTest(char except)
|
||||
{
|
||||
ValidateFailedResult(Digit(), except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a')]
|
||||
[InlineData('z')]
|
||||
[InlineData('A')]
|
||||
[InlineData('Z')]
|
||||
public void AsciiLetterTest(char except)
|
||||
{
|
||||
ValidateSuccessfulResult(AsciiLetter(), except, except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('0')]
|
||||
[InlineData(',')]
|
||||
[InlineData('%')]
|
||||
public void FailedAsciiLetterTest(char except)
|
||||
{
|
||||
ValidateFailedResult(AsciiLetter(), except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('0')]
|
||||
public void AsciiDigitTest(char except)
|
||||
{
|
||||
ValidateSuccessfulResult(AsciiDigit(), except, except.ToString());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData('a')]
|
||||
[InlineData('A')]
|
||||
[InlineData('z')]
|
||||
[InlineData('测')]
|
||||
[InlineData(',')]
|
||||
[InlineData('%')]
|
||||
public void FailedAsciiDigitTest(char except)
|
||||
{
|
||||
ValidateFailedResult(AsciiDigit(), except.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SkipSpacesTest()
|
||||
{
|
||||
ValidateSuccessfulResult(String("test").SkipSpaces(), "test", " test");
|
||||
ValidateSuccessfulResult(String("test").SkipSpaces(), "test", "\t test");
|
||||
|
||||
ValidateSuccessfulResult(String("test").SkipSpaces().Many(), ["test", "test", "test"],
|
||||
"test test test test");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SkipSpaceAndLineBreakTest()
|
||||
{
|
||||
StringReadState state = new("""
|
||||
test test test
|
||||
test test
|
||||
test
|
||||
""");
|
||||
|
||||
Parser<char, IEnumerable<string>> parser = StringIgnoreCase("test").SkipSpaceAndLineBreak().Many();
|
||||
ParseResult<char, IEnumerable<string>> result = parser.Parse(state);
|
||||
|
||||
Assert.All(result.Value, x => Assert.Equal("test", x.ToLower()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user