2024-08-13 14:46:11 +08:00
|
|
|
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()
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
IParser<char, char> parser = Token('a').Do(x => Assert.Equal('a', x)).Do(x => Assert.Equal('a', x),
|
2024-08-13 14:46:11 +08:00
|
|
|
failedResult => Assert.ThrowsAny<ParseException>(() => failedResult.Value));
|
|
|
|
|
|
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
|
|
ValidateFailedResult(parser, "bcd");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void LookAheadTest()
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
IParser<char, char> parser = Token('a').LookAhead().Next(_ => Token('a'), _ => Token('b'));
|
2024-08-13 14:46:11 +08:00
|
|
|
ValidateSuccessfulResult(parser, 'a', "abc");
|
|
|
|
ValidateSuccessfulResult(parser, 'b', "bcd");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NotTest()
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
IParser<char, char> parser = Token('a').Not('b');
|
2024-08-13 14:46:11 +08:00
|
|
|
ValidateSuccessfulResult(parser, 'b', "bcd");
|
|
|
|
|
|
|
|
parser = Token('a').Not().Bind(_ => Token('b'));
|
|
|
|
ValidateSuccessfulResult(parser, 'b', "bcd");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void TryTest()
|
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
IParser<char, string> parser = String("abc").Try("cde");
|
2024-08-13 14:46:11 +08:00
|
|
|
ValidateSuccessfulResult(parser, "abc", "abc");
|
|
|
|
ValidateSuccessfulResult(parser, "cde", "cde");
|
|
|
|
|
|
|
|
parser = String("abc").Try(_ => "cde");
|
|
|
|
ValidateSuccessfulResult(parser, "abc", "abc");
|
|
|
|
ValidateSuccessfulResult(parser, "cde", "cde");
|
|
|
|
}
|
|
|
|
}
|