<commit_before>﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Parseq;
using Parseq.Combinators;

namespace Parseq.Test
{
    [TestClass]
    public class CombinatorTest
    {
        [TestMethod]
        public void SequenceTest()
        {
            Combinator.Sequence(Enumerable.Empty<Parser<Char, Char>>())
                .ExpectSuccess(Enumerable.Empty<Char>())
                .Run("foobar".AsStream());

            var p1 = Prims.Return<Char, Char>('a');
            var p2 = Prims.Return<Char, Char>('b');
            var p3 = Prims.Return<Char, Char>('c');
            /* ... */
            var pn = Prims.Return<Char, Char>('n');

            Combinator.Sequence(p1, p2, p3, /* ... */ pn)
                .ExpectSuccess(new[] { 'a', 'b', 'c', /* ... */ 'n' })
                .Run("foobar".AsStream());

            var success = Prims.Return<Char, Char>(default(Char));
            var failure = Prims.Fail<Char, Char>();

            Combinator.Sequence(failure, success, success, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, failure, success, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, success, failure, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, success, success, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());

            var error = Errors.Error<Char, Char>("Error!");

            Combinator.Sequence(error, success, success, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, error, success, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, success, error, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, success, success, /* ... */ error).ExpectError().Run("foobar".AsStream());

            Combinator.Sequence(failure, error).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(error, failure).ExpectError().Run("foobar".AsStream());
        }

        [TestMethod]
        public void ChoiceTest()
        {
            Combinator.Choice(Enumerable.Empty<Parser<Char, Char>>())
                .ExpectFailure()
                .Run("foobar".AsStream());

            var failure = Prims.Fail<Char, Char>();
            var success = Prims.Return<Char, Char>(default(Char));

            Combinator.Choice(failure, failure, failure, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());
            Combinator.Choice(success, failure, failure, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, success, failure, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, success, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, failure, /* ... */ success).ExpectSuccess().Run("foobar".AsStream());

            var error = Errors.Error<Char, Char>("Error!");

            Combinator.Choice(failure, failure, failure, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());
            Combinator.Choice(error,   failure, failure, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure,   error, failure, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure, failure,   error, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, failure, /* ... */   error).ExpectError().Run("foobar".AsStream());

            Combinator.Choice(failure, success).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, error).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(success, error).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(error, success).ExpectError().Run("foobar".AsStream());

            // Issue #4 
            // Combinator.Choice does not work since second time parsing in Parseq 
            var parser = Combinator.Choice(new[] { 'a', 'b', 'c' }.Select(_ => _.Satisfy()));
            parser.ExpectSuccess('c').Run("c".AsStream());
            parser.ExpectSuccess('c').Run("c".AsStream());        
        }

        [TestMethod]
        public void ForkTest()
        {
            var p1 = Chars.Satisfy('a');
            var p2 = Chars.Satisfy('b');
            Combinator.Fork(p1, p2).ExpectTrue(Either.Left<Char, Char>('a').Equals).Run("a".AsStream());
            Combinator.Fork(p1, p2).ExpectTrue(Either.Right<Char, Char>('b').Equals).Run("b".AsStream());
            Combinator.Fork(p1, p2).ExpectFailure().Run("c".AsStream());

            var p3 = Prims.Return<Char, Char>('c');
            var p4 = Prims.Return<Char, Char>('d');
            Combinator.Fork(p3, p4).ExpectTrue(Either.Left<Char, Char>('c').Equals).Run("foobar".AsStream());
            Combinator.Fork(p4, p3).ExpectTrue(Either.Left<Char, Char>('d').Equals).Run("foobar".AsStream());<commit_msg>Add Test<commit_after>﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Parseq;
using Parseq.Combinators;

namespace Parseq.Test
{
    [TestClass]
    public class CombinatorTest
    {
        [TestMethod]
        public void SequenceTest()
        {
            Combinator.Sequence(Enumerable.Empty<Parser<Char, Char>>())
                .ExpectSuccess(Enumerable.Empty<Char>())
                .Run("foobar".AsStream());

            var p1 = Prims.Return<Char, Char>('a');
            var p2 = Prims.Return<Char, Char>('b');
            var p3 = Prims.Return<Char, Char>('c');
            /* ... */
            var pn = Prims.Return<Char, Char>('n');

            Combinator.Sequence(p1, p2, p3, /* ... */ pn)
                .ExpectSuccess(new[] { 'a', 'b', 'c', /* ... */ 'n' })
                .Run("foobar".AsStream());

            var success = Prims.Return<Char, Char>(default(Char));
            var failure = Prims.Fail<Char, Char>();

            Combinator.Sequence(failure, success, success, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, failure, success, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, success, failure, /* ... */ success).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(success, success, success, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());

            var error = Errors.Error<Char, Char>("Error!");

            Combinator.Sequence(error, success, success, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, error, success, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, success, error, /* ... */ success).ExpectError().Run("foobar".AsStream());
            Combinator.Sequence(success, success, success, /* ... */ error).ExpectError().Run("foobar".AsStream());

            Combinator.Sequence(failure, error).ExpectFailure().Run("foobar".AsStream());
            Combinator.Sequence(error, failure).ExpectError().Run("foobar".AsStream());

            // Issue #7
            // Combinator.Sequence does not work since second time parsing in Parseq 
            var issue_7 = Combinator.Sequence(new[] { 'a', 'b', 'c' }.Select(Chars.Satisfy));
            issue_7.ExpectSuccess("abc").Run("abc".AsStream());
            issue_7.ExpectSuccess("abc").Run("abc".AsStream());     
        }

        [TestMethod]
        public void ChoiceTest()
        {
            Combinator.Choice(Enumerable.Empty<Parser<Char, Char>>())
                .ExpectFailure()
                .Run("foobar".AsStream());

            var failure = Prims.Fail<Char, Char>();
            var success = Prims.Return<Char, Char>(default(Char));

            Combinator.Choice(failure, failure, failure, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());
            Combinator.Choice(success, failure, failure, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, success, failure, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, success, /* ... */ failure).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, failure, /* ... */ success).ExpectSuccess().Run("foobar".AsStream());

            var error = Errors.Error<Char, Char>("Error!");

            Combinator.Choice(failure, failure, failure, /* ... */ failure).ExpectFailure().Run("foobar".AsStream());
            Combinator.Choice(error,   failure, failure, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure,   error, failure, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure, failure,   error, /* ... */ failure).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(failure, failure, failure, /* ... */   error).ExpectError().Run("foobar".AsStream());

            Combinator.Choice(failure, success).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(failure, error).ExpectError().Run("foobar".AsStream());
            Combinator.Choice(success, error).ExpectSuccess().Run("foobar".AsStream());
            Combinator.Choice(error, success).ExpectError().Run("foobar".AsStream());

            // Issue #4 
            // Combinator.Choice does not work since second time parsing in Parseq 
            var issue_4 = Combinator.Choice(new[] { 'a', 'b', 'c' }.Select(_ => _.Satisfy()));
            issue_4.ExpectSuccess('c').Run("c".AsStream());
            issue_4.ExpectSuccess('c').Run("c".AsStream());        
        }

        [TestMethod]
        public void ForkTest()
        {
            var p1 = Chars.Satisfy('a');
            var p2 = Chars.Satisfy('b');
            Combinator.Fork(p1, p2).ExpectTrue(Either.Left<Char, Char>('a').Equals).Run("a".AsStream());
            Combinator.Fork(p1, p2).ExpectTrue(Either.Right<Char, Char>('b').Equals).Run("b".AsStream());
            Combinator.Fork(p1, p2).ExpectFailure().Run("c".AsStream());

            var p3 = Prims.Return<Char, Char>('c');
            var p4 = Prims.Return<Char, Char>('d');
            Combinator.Fork(p3, p4).ExpectTrue(Either.Left<Char, Char>('c').Equals).Run("foobar".AsStream());
            Combinator.Fork(p4, p3).ExpectTrue(Either.Left<Char, Char>('d').Equals).Run("foobar".AsStream());
{'fraction_non_alphanumeric': 0.18932128365916637, 'fraction_numerical': 0.004518627812615271, 'mean_word_length': 2.6029900332225915, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6247908', 'n_tokens_mistral': 3166, 'n_tokens_neox': 3047, 'n_words': 469}