public void GetMatchesOf_A_3_BForAAABBBReturnsAAAB()
        {
            int index = 0;
             int length = 4;
             var expression =
            new ListExpression<char> (
               new GreedyRepeatExpression<char> (m_A, 3),
               m_B);

             var list = AAABBB;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             foreach (var match in matches)
             {
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (length, match.Items.Count, "match.Items.Count at");
            for (int i = 0; i < length; i++)
            {
               Assert.AreEqual (list[index + i], match.Items [i], "match.Items [" + i + "]");
            }
             }
        }
        public void GetMatchesOf_LookBack_List_AB_LookAhead_BB_GreedyAny_B_ForAAABBBReturnsBB()
        {
            int index = 4;
             int [] lengths = {2,1};
             var expression =
            new ListExpression<char> (
               new LookBackExpression<char> (
                  new ListExpression<char> (
                     m_A,
                     m_B,
                     new LookAheadExpression<char> (new RepeatExpression<char>(m_B,2)),
                     new NegativeLookAheadExpression<char> (new RepeatExpression<char>(m_A,2))
                     )),
               new GreedyRepeatExpression<char> (m_any),
               m_B);

             var list = AAABBB;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (lengths.Length, matches.Count (), "Count");
             for(int matchIndex = 0; matchIndex < lengths.Length; matchIndex++)
             {
            var match = matches[matchIndex];
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (lengths[matchIndex], match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (lengths[matchIndex], match.Items.Count, "match.Items.Count at");
            for (int i = 0; i < lengths[matchIndex]; i++)
            {
               Assert.AreEqual (list[index + i], match.Items [i], "match.Items [" + i + "]");
            }
             }
        }
        public void GetMatchesOf_LookBack_Start_GreedyAny_B_ForAAABBBReturnsAAABBB()
        {
            int index = 0;
             int [] lengths = new []{6,5,4};
             var expression =
            new ListExpression<char> (
               new LookBackExpression<char> (
                  new StartExpression<char> ()),
               new GreedyRepeatExpression<char> (m_any),
               m_B);

             var list = AAABBB;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (lengths.Length, matches.Count, "Count");
             for (int matchIndex = 0; matchIndex < matches.Count; matchIndex++)
             {
            var match = matches [matchIndex];
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (lengths[matchIndex], match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (lengths[matchIndex], match.Items.Count, "match.Items.Count at");
            for (int i = 0; i < lengths[matchIndex]; i++)
            {
               Assert.AreEqual (list[index + i], match.Items [i], "match.Items [" + i + "]");
            }
             }
        }
        public void GetMatchesOf_A_3_BForAAABBB_GetEnumerator_MoveNextTooManyTimesThrows()
        {
            int index = 0;
             var expression =
            new ListExpression<char> (
               new RepeatExpression<char> (m_A, 3),
               m_B);

             var list = AAABBB;
             var matches = expression.GetMatches (list, index).ToList ();
             var enumerator = matches.GetEnumerator ();
             var current = enumerator.Current;
             Assert.IsNull (current, "current");
             for (int i = 0; i < 100; i++)
             {
            enumerator.MoveNext ();
             }
        }
        public void GetMatchesOf_LookBack_A_B_2_ForAAABBBReturnsBB()
        {
            int index = 3;
             int length = 2;
             var expression =
            new ListExpression<char> (
               new LookBackExpression<char> (m_A),
               new RepeatExpression<char> (m_B, length));

             var list = AAABBB;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             foreach (var match in matches)
             {
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (length, match.Items.Count, "match.Items.Count");
            for (int i = 0; i < length; i++)
            {
               Assert.AreEqual (list[index + i], match.Items [i], "match.Items [" + i + "]");
            }
             }
        }
        public void GetMatchesOf_AnyOr56_AnyOr6789_ForDigitsStartingAt5()
        {
            int index = 5;
             ListExpression<char> expression = new ListExpression<char> (
            new AlternationExpression<char>(m_any, new ListExpression<char>(m_digits[5],m_digits[6])),
            new AlternationExpression<char>(m_any, new ListExpression<char>(m_digits[6],m_digits[7],m_digits[8],m_digits[9]))
            );

             var list = DigetsList;
             var matches = expression.GetMatches (list, index).ToList ();

             string[] expectedMatches = new [] {"56","56789","567"};

             Assert.AreEqual (expectedMatches.Length, matches.Count (), "Count");
             int matchesIndex = 0;
             foreach (var match in matches)
             {
            string expected = expectedMatches [matchesIndex];
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (expected.Length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (expected.Length, match.Items.Count, "match.Items.Count");
            string matchedString = new string (match.Items.ToArray ());
            Assert.AreEqual (expected, matchedString, "matched string does not match expected value.");

            matchesIndex++;
             }
        }
        public void ParamsConstructorCopiesParamsArraySoParamsArrayModificationDoesNotAffectConstructedExpression()
        {
            IExpression<char> [] expressions = new [] {m_any, m_any, m_any, m_any, m_any, m_any};
             var expression = new ListExpression<char> (expressions);

             string expected = "012345";

             int index = 0;
             var list = DigetsList;

             // alter array
             expressions [0] = m_digits[5];
             expressions [1] = m_digits[8];
             expressions [2] = m_a;

             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             foreach (var match in matches)
             {
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (expressions.Length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (expressions.Length, match.Items.Count, "match.Items.Count");
            string matchedString = new string (match.Items.ToArray ());
            Assert.AreEqual (expected, matchedString, "matched string does not match expected value.");
             }
        }
        public void GetMatchesOfEmptyFindsOneZeroLengthMatchForDigitsStartingAt8()
        {
            ListExpression<char> expression = new ListExpression<char> ();

             int index = 8;
             var list = DigetsList;
             int length = 0;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");

             var match = matches.First ();
             Assert.AreEqual (index, match.Index, "match.Index");
             Assert.AreEqual (length, match.Length, "match.Length");
             Assert.IsTrue (match.Success, "match.Success");
             Assert.AreEqual (length, match.Items.Count, "match.Items.Count");
        }
        public void GetMatchesOfNullThrowsArgumentNullException()
        {
            ListExpression<char> expression = new ListExpression<char> (m_digits[5],m_a);

             var matches = expression.GetMatches (null, 8);
        }
        public void GetMatchesOfDigits_Any_Any_Any()
        {
            IExpression<char> [] expressions = new [] {m_any, m_any, m_any};
             var expression = new ListExpression<char> (expressions);

             string expected = "012";

             int index = 0;
             var list = DigetsList;

             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             var match = matches.First ();

             Assert.AreEqual (index, match.Index, "match.Index");
             Assert.AreEqual (expected.Length, match.Length, "match.Length");
             Assert.IsTrue (match.Success, "match.Success");
             Assert.AreEqual (expected.Length, match.Items.Count, "match.Items.Count");
             string matchedString = new string (match.Items.ToArray ());
             Assert.AreEqual (expected, matchedString, "matched string does not match expected value.");
        }
        public void GetMatchesOfDigits_AnyOr5OrAny_Any()
        {
            IExpression<char> [] expressions = new [] {m_any, m_digits[5], m_any};
             var expression = new ListExpression<char> (new AlternationExpression<char> (expressions), m_any);

             string expected = "56";

             int index = 5;
             var list = DigetsList;

             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (3, matches.Count (), "Count");
             foreach (var match in matches)
             {
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (expected.Length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (expected.Length, match.Items.Count, "match.Items.Count");
            string matchedString = new string (match.Items.ToArray ());
            Assert.AreEqual (expected, matchedString, "matched string does not match expected value.");
             }
        }
        public void GetMatchesOf56ForDigetsStartingAt5()
        {
            int index = 5;
             ListExpression<char> expression = new ListExpression<char> (m_digits[index], m_digits[index+1]);
             int length = 2;

             var list = DigetsList;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             foreach (var match in matches)
             {
            Assert.AreEqual (index, match.Index, "match.Index");
            Assert.AreEqual (length, match.Length, "match.Length");
            Assert.IsTrue (match.Success, "match.Success");
            Assert.AreEqual (length, match.Items.Count, "match.Items.Count");
            for (int i = 0; i < length; i++)
            {
               Assert.AreEqual (list[index + i], match.Items [i], "match.Items [" + i + "]");
            }
             }
        }
        public void GetMatchesFindsOneMatchForA()
        {
            ListExpression<char> expression = new ListExpression<char> (m_a);

             int index = 0;
             var list = AList;
             var matches = expression.GetMatches (list, index).ToList ();

             Assert.AreEqual (1, matches.Count (), "Count");
             var match = matches.First ();
             Assert.AreEqual (index, match.Index, "match.Index");
             Assert.AreEqual (1, match.Length, "match.Length");
             Assert.IsTrue (match.Success, "match.Success");
             Assert.AreEqual (1, match.Items.Count, "match.Items.Count");
             Assert.AreEqual (list[index], match.Items [0], "match.Items [0]");
        }
        public void GetMatchesFindsNoMatchesForEmpty()
        {
            ListExpression<char> expression = new ListExpression<char> (m_any);

             var matches = expression.GetMatches (EmptyList, 0);

             Assert.AreEqual (0, matches.Count (), "Count");
        }