CanonSharp/CanonSharp.Tests/CombinatorsTests/ModifierParserTests.cs

52 lines
1.6 KiB
C#
Raw Normal View History

using CanonSharp.Combinator;
using CanonSharp.Combinator.Abstractions;
using CanonSharp.Combinator.Extensions;
using CanonSharp.Tests.Utils;
using static CanonSharp.Combinator.ParserBuilder;
using static CanonSharp.Combinator.Text.TextParserBuilder;
namespace CanonSharp.Tests.CombinatorsTests;
public class ModifierParserTests : ParserTestsBase
{
[Fact]
public void DoTest()
{
Parser<char, char> parser = Token('a').Do(x => Assert.Equal('a', x)).Do(x => Assert.Equal('a', x),
failedResult => Assert.ThrowsAny<ParseException>(() => failedResult.Value));
ValidateSuccessfulResult(parser, 'a', "abc");
ValidateFailedResult(parser, "bcd");
}
[Fact]
public void LookAheadTest()
{
Parser<char, char> parser = Token('a').LookAhead().Next(_ => Token('a'), _ => Token('b'));
ValidateSuccessfulResult(parser, 'a', "abc");
ValidateSuccessfulResult(parser, 'b', "bcd");
}
[Fact]
public void NotTest()
{
Parser<char, char> parser = Token('a').Not('b');
ValidateSuccessfulResult(parser, 'b', "bcd");
parser = Token('a').Not().Bind(_ => Token('b'));
ValidateSuccessfulResult(parser, 'b', "bcd");
}
[Fact]
public void TryTest()
{
Parser<char, string> parser = String("abc").Try("cde");
ValidateSuccessfulResult(parser, "abc", "abc");
ValidateSuccessfulResult(parser, "cde", "cde");
parser = String("abc").Try(_ => "cde");
ValidateSuccessfulResult(parser, "abc", "abc");
ValidateSuccessfulResult(parser, "cde", "cde");
}
}