jackfiled
cf19f8197e
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/3 Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using CanonSharp.Combinator;
|
|
using CanonSharp.Combinator.Abstractions;
|
|
using CanonSharp.Combinator.Extensions;
|
|
using CanonSharp.Tests.Utils;
|
|
using static CanonSharp.Combinator.ParserBuilder;
|
|
|
|
namespace CanonSharp.Tests.CombinatorsTests;
|
|
|
|
public class PrimitiveParserTests : ParserTestsBase
|
|
{
|
|
[Fact]
|
|
public void PureTest()
|
|
{
|
|
IParser<char, char> parser = Pure<char, char>('a');
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
|
|
parser = Pure<char, char>(_ => 'a');
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
}
|
|
|
|
[Fact]
|
|
public void NullTest()
|
|
{
|
|
IParser<char, Unit> parser = Null<char>();
|
|
ValidateSuccessfulResult(parser, Unit.Instance, "abc");
|
|
}
|
|
|
|
[Fact]
|
|
public void FailTest()
|
|
{
|
|
IParser<char, char> parser = Fail<char, char>();
|
|
ValidateFailedResult(parser, "abc");
|
|
|
|
parser = Fail<char, char>("Failed message");
|
|
IFailedResult<char, char> result = ValidateFailedResult(parser, "abc");
|
|
Assert.Equal("Failed message", result.Message);
|
|
|
|
parser = Fail<char, char>(x => $"{x}");
|
|
result = ValidateFailedResult(parser, "abc");
|
|
Assert.Equal("a<0x61>", result.Message);
|
|
|
|
parser = Fail<char, char>(new InvalidOperationException());
|
|
result = ValidateFailedResult(parser, "abc");
|
|
Assert.IsType<InvalidOperationException>(result.Exception.InnerException);
|
|
}
|
|
|
|
[Fact]
|
|
public void SatisfyTest()
|
|
{
|
|
IParser<char, char> parser = Satisfy<char>(char.IsLetter);
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
ValidateFailedResult(parser, "123");
|
|
}
|
|
|
|
[Fact]
|
|
public void SatisfyFailedTest()
|
|
{
|
|
IParser<char, char> parser = Satisfy<char>(char.IsLetter);
|
|
ValidateFailedResult(parser, "");
|
|
}
|
|
|
|
[Fact]
|
|
public void AnyTest()
|
|
{
|
|
IParser<char, char> parser = Any<char>();
|
|
ValidateSuccessfulResult(parser, '1', "123");
|
|
}
|
|
|
|
[Fact]
|
|
public void TokenTest()
|
|
{
|
|
IParser<char, char> parser = Token('a');
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
}
|
|
|
|
[Fact]
|
|
public void TakeTest()
|
|
{
|
|
IParser<char, IEnumerable<char>> parser = Take<char>(5);
|
|
ValidateSuccessfulResult(parser, ['h', 'e', 'l', 'l', 'o'], "hello");
|
|
ValidateFailedResult(parser, "abc");
|
|
}
|
|
|
|
[Fact]
|
|
public void SkipTest()
|
|
{
|
|
IParser<char, char> parser = Skip<char>(5).Bind(_ => Token(','));
|
|
ValidateSuccessfulResult(parser, ',', "hello,world.");
|
|
ValidateFailedResult(parser, "abc");
|
|
}
|
|
}
|