85 lines
2.4 KiB
C#
85 lines
2.4 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()
|
||
|
{
|
||
|
Parser<char, char> parser = Pure<char, char>('a');
|
||
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
||
|
|
||
|
parser = Pure<char, char>(_ => 'a');
|
||
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void NullTest()
|
||
|
{
|
||
|
Parser<char, Unit> parser = Null<char>();
|
||
|
ValidateSuccessfulResult(parser, Unit.Instance, "abc");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void FailTest()
|
||
|
{
|
||
|
Parser<char, char> parser = Fail<char, char>();
|
||
|
ValidateFailedResult(parser, "abc");
|
||
|
|
||
|
parser = Fail<char, char>("Failed message");
|
||
|
FailedResult<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()
|
||
|
{
|
||
|
Parser<char, char> parser = Satisfy<char>(char.IsLetter);
|
||
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
||
|
ValidateFailedResult(parser, "123");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void AnyTest()
|
||
|
{
|
||
|
Parser<char, char> parser = Any<char>();
|
||
|
ValidateSuccessfulResult(parser, '1', "123");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void TokenTest()
|
||
|
{
|
||
|
Parser<char, char> parser = Token('a');
|
||
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void TakeTest()
|
||
|
{
|
||
|
Parser<char, IEnumerable<char>> parser = Take<char>(5);
|
||
|
ValidateSuccessfulResult(parser, ['h', 'e', 'l', 'l', 'o'], "hello");
|
||
|
ValidateFailedResult(parser, "abc");
|
||
|
}
|
||
|
|
||
|
[Fact]
|
||
|
public void SkipTest()
|
||
|
{
|
||
|
Parser<char, char> parser = Skip<char>(5).Bind(_ => Token(','));
|
||
|
ValidateSuccessfulResult(parser, ',', "hello,world.");
|
||
|
ValidateFailedResult(parser, "abc");
|
||
|
}
|
||
|
}
|