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 parser = Pure('a'); ValidateSuccessfulResult(parser, 'a', "abc"); parser = Pure(_ => 'a'); ValidateSuccessfulResult(parser, 'a', "abc"); } [Fact] public void NullTest() { IParser parser = Null(); ValidateSuccessfulResult(parser, Unit.Instance, "abc"); } [Fact] public void FailTest() { IParser parser = Fail(); ValidateFailedResult(parser, "abc"); parser = Fail("Failed message"); IFailedResult result = ValidateFailedResult(parser, "abc"); Assert.Equal("Failed message", result.Message); parser = Fail(x => $"{x}"); result = ValidateFailedResult(parser, "abc"); Assert.Equal("a<0x61>", result.Message); parser = Fail(new InvalidOperationException()); result = ValidateFailedResult(parser, "abc"); Assert.IsType(result.Exception.InnerException); } [Fact] public void SatisfyTest() { IParser parser = Satisfy(char.IsLetter); ValidateSuccessfulResult(parser, 'a', "abc"); ValidateFailedResult(parser, "123"); } [Fact] public void SatisfyFailedTest() { IParser parser = Satisfy(char.IsLetter); ValidateFailedResult(parser, ""); } [Fact] public void AnyTest() { IParser parser = Any(); ValidateSuccessfulResult(parser, '1', "123"); } [Fact] public void TokenTest() { IParser parser = Token('a'); ValidateSuccessfulResult(parser, 'a', "abc"); } [Fact] public void TakeTest() { IParser> parser = Take(5); ValidateSuccessfulResult(parser, ['h', 'e', 'l', 'l', 'o'], "hello"); ValidateFailedResult(parser, "abc"); } [Fact] public void SkipTest() { IParser parser = Skip(5).Bind(_ => Token(',')); ValidateSuccessfulResult(parser, ',', "hello,world."); ValidateFailedResult(parser, "abc"); } }