Ejemplo n.º 1
0
        public void LazyTest()
        {
            var throwException = new Func <Parser <Char, Char> >(() =>
            {
                throw new InvalidOperationException();
            });

            Assert.Throws(typeof(InvalidOperationException), () =>
                          Combinator.Or(
                              Chars.Any(),
                              throwException())
                          .Run("inputString".AsStream()));

            Combinator.Or(
                Chars.Any(),
                Combinator.Lazy(throwException))
            .Run("inputString".AsStream())
            .Case(
                failure: (restStream, _) => Assert.Fail(),
                success: (restStream, value) =>
            {
                Assert.AreEqual('i', value);
                Assert.True(restStream.Current.HasValue);
                Assert.AreEqual('n', restStream.Current.Value.Item0);
                Assert.AreEqual(1, restStream.Current.Value.Item1.Line);
                Assert.AreEqual(2, restStream.Current.Value.Item1.Column);
            });
        }
Ejemplo n.º 2
0
        public void OrTest()
        {
            Combinator.Or(
                Parser.Fail <Char, Char>("Failure"),
                Parser.Fail <Char, Char>("Failure"))
            .Run("inputString".AsStream())
            .Case(
                failure: (restStream, _) => { /* OK */ },
                success: (restStream, _) => Assert.Fail());

            Combinator.Or(
                Parser.Return <Char, Char>('a'),
                Parser.Fail <Char, Char>("Failure"))
            .Run("inputString".AsStream())
            .Case(
                failure: (restStream, _) => Assert.Fail(),
                success: (restStream, value) =>
            {
                Assert.AreEqual('a', value);
            });

            Combinator.Or(
                Parser.Fail <Char, Char>("Failure"),
                Parser.Return <Char, Char>('a'))
            .Run("inputString".AsStream())
            .Case(
                failure: (restStream, _) => Assert.Fail(),
                success: (restStream, value) =>
            {
                Assert.AreEqual('a', value);
            });

            Combinator.Or(
                Parser.Return <Char, Char>('a'),
                Parser.Return <Char, Char>('b'))
            .Run("inputString".AsStream())
            .Case(
                failure: (restStream, _) => Assert.Fail(),
                success: (restStream, value) =>
            {
                Assert.AreEqual('a', value);
            });
        }