public void ConstructorSucceeds() { int min = 0; int max = int.MaxValue; var expression = new RepeatExpression<char> (m_A); Assert.AreEqual (min, expression.Minimum, "Minimum"); Assert.AreEqual (max, expression.Maximum, "Maximum"); }
public void NotSupportsLookBack_For_LookBackNotSupported() { var expression = new RepeatExpression<char> (LookBackNotSupported); Assert.IsFalse (expression.SupportsLookBack); }
public void ExactConstructorThrowArgumentOutOfRangeException() { var expression = new RepeatExpression<char> (m_any, -1); }
public void GetMatchesFindsOneMatchesForEmpty() { RepeatExpression<char> expression = new RepeatExpression<char> (m_any); var matches = expression.GetMatches (EmptyList, 0).ToList (); Assert.AreEqual (1, matches.Count (), "Count"); Assert.AreEqual (0, matches[0].Index, "Count"); Assert.AreEqual (0, matches[0].Length, "Count"); }
public LilypondSequenceReader(string lilyContent) { string[] lilypondText = lilyContent.Split().Where(item => item.Length > 0).ToArray(); LilypondContext context = new LilypondContext(); Stack <LilypondSection> sections = new Stack <LilypondSection>(); LilypondSection rootSection = null; for (int i = 0; i < lilypondText.Length; i++) { switch (lilypondText[i]) { case "\\relative": // New relative section start with a startPitch and an ocatveChange RelativeExpression relativeExpression = new RelativeExpression(lilypondText[i + 1][0], string.Concat(lilypondText[i + 1].Skip(1))); sections.Push(relativeExpression); if (rootSection == null) { rootSection = relativeExpression; } i += 2; break; case "\\repeat": RepeatExpression repeatExpression = new RepeatExpression(); sections.Peek()?.ChildExpressions.Add(repeatExpression); sections.Push(repeatExpression); i += 3; break; case "\\alternative": AlternativeExpression alternativeExpression = new AlternativeExpression(); sections.Peek()?.ChildExpressions.Add(alternativeExpression); sections.Push(alternativeExpression); context.InAlternative = true; i++; break; case "\\clef": sections.Peek().ChildExpressions.Add(new ClefExpression(lilypondText[i + 1])); i++; break; case "\\tempo": sections.Peek().ChildExpressions.Add(new TempoExpression(lilypondText[i + 1])); i += 1; break; case "\\time": sections.Peek().ChildExpressions.Add(new TimeSignatureExpression(lilypondText[i + 1])); i++; break; case "{": if (context.InAlternative) { // There is a new alternative group in the current alternative block AlternativeGroupExpression alternativeGroup = new AlternativeGroupExpression(); sections.Peek()?.ChildExpressions.Add(alternativeGroup); sections.Push(alternativeGroup); context.InAlternativeGroup = true; } else { LilypondSection lilypondSection = new LilypondSection(); if (sections.Any()) { sections.Peek()?.ChildExpressions.Add(lilypondSection); sections.Push(lilypondSection); } } break; case "}": // Section has ended. It is no longer the current section, so pop it from the stack if (context.InRepeat) { if (lilypondText[i + 1] != "\\alternative") { sections.Peek().ChildExpressions.Add(new BarlineExpression(true)); } context.InRepeat = false; } if (sections.Any()) { sections.Pop(); } break; case "|": sections.Peek().ChildExpressions.Add(new BarlineExpression()); break; // It is a note or an unknown token default: try { sections.Peek().ChildExpressions.Add(new NoteExpression(lilypondText[i])); break; } catch (Exception) { /* It is an unknown token, skip it. */ break; } } } rootSection.Interpret(context); if (!context.ClefAdded) { context.Sequence.Symbols.Insert(0, new Clef(ClefType.GClef)); } Sequence = context.Sequence; }
public void ConstructorThrowArgumentNullExceptionForNullArray() { var expression = new RepeatExpression<char> ((IExpression<char>) null); }
public void AnyLength_For_AnyLength_NotAnyLength() { var expression = new RepeatExpression<char> ( new ListExpression<char> (AnyLength, NotAnyLength)); Assert.IsTrue (expression.AnyLength); }
public void RangeConstructorArgumentOutOfRangeExceptionForNegativeMinimum() { var expression = new RepeatExpression<char> (m_any, -1, 23); }
public void GetMatchesOf_A_ForAAABBB_LikeRegex() { var expression = new RepeatExpression<char> (m_A); expression.AssertMatches ("AAABBB", "A*?"); }
public void GetMatchesOf_A_3_ForAAABBBReturnsAAA() { int index = 0; int length = 3; RepeatExpression<char> expression = new RepeatExpression<char> (m_A, 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_Any_2_ForDigetsStartingAt5Returns56() { int index = 5; RepeatExpression<char> expression = new RepeatExpression<char> (m_any, 2); 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 GetMatchesOf_AAorAB_0_4_ForAAABBB() { int index = 0; RepeatExpression<char> expression = new RepeatExpression<char> ( new AlternationExpression<char> ( new ListExpression<char>(m_A, m_A), new ListExpression<char>(m_A, m_B, m_B, m_B)), 0, 4); var list = AAABBB; var matches = expression.GetMatches (list, index).ToList (); string [] expectedValues = new [] {"", "AA", "AAABBB"}; Assert.AreEqual (expectedValues.Length, matches.Count (), "Count"); for (int i = 0; i < expectedValues.Length; i++) { var match = matches [i]; string expected = expectedValues [i]; Assert.AreEqual (index, match.Index, "match.Index at " + i); Assert.AreEqual (expected.Length, match.Length, "match.Length at " + i); Assert.IsTrue (match.Success, "match.Success at " + i); Assert.AreEqual (expected.Length, match.Items.Count, "match.Items.Count at " + i); Assert.AreEqual (expected, new string (match.Items.ToArray ()), "Value at " + i); } }
public void GetMatchesOfDigits_Any_4_8() { var expression = new RepeatExpression<char> (m_any, 4, 8); string [] expectedValues = new [] {"0123", "01234", "012345", "0123456", "01234567"}; int index = 0; var list = DigetsList; var matches = expression.GetMatches (list, index).ToList (); Assert.AreEqual (expectedValues.Length, matches.Count (), "Count"); for (int i = 0; i < expectedValues.Length; i++) { var match = matches [i]; var expected = expectedValues [i]; Assert.AreEqual (index, match.Index, "match.Index at " + i); Assert.AreEqual (expected.Length, match.Length, "match.Length at " + i); Assert.IsTrue (match.Success, "match.Success at " + i); Assert.AreEqual (expected.Length, match.Items.Count, "match.Items.Count at " + i); Assert.AreEqual (expected, new string (match.Items.ToArray ()), "Value at " + i); } }
public void GetMatchesFindsOneMatchForA() { RepeatExpression<char> expression = new RepeatExpression<char> (m_A); int index = 0; var list = AList; var matches = expression.GetMatches (list, index).ToList (); Assert.AreEqual (2, matches.Count (), "Count"); Assert.AreEqual (0, matches.First ().Length, "First match should be empty"); var match = matches.ElementAt(1); 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 NotSupportsLookBack_For_LookBackSupported_LookBackNotSupported() { var expression = new RepeatExpression<char> (new AlternationExpression<char> (LookBackSupported, LookBackNotSupported), 2); Assert.IsFalse (expression.SupportsLookBack); }
public void RangeConstructorArgumentExceptionForMaximumLessThanMinimum() { var expression = new RepeatExpression<char> (m_any, 4, 3); }
public void GetPossibleMatchLengths_For1_1() { var expected = new int [] {2}; var expression = new RepeatExpression<char> (MatchLength1,2); expression.AssertPossibleMatchLengths (expected); }
public void SupportsLookBack_For_LookBackSupported() { var expression = new RepeatExpression<char> (LookBackSupported, 2,4); Assert.IsTrue (expression.SupportsLookBack); }
public void GetPossibleMatchLengths_For1_4to6() { var expected = new int [] {4,5,6}; var expression = new RepeatExpression<char> (MatchLength1, 4, 6); expression.AssertPossibleMatchLengths (expected); }
public void GetPossibleMatchLengths_ForEmpty() { var expected = new int [] {0}; var expression = new RepeatExpression<char> (new AlternationExpression<char>()); expression.AssertPossibleMatchLengths (expected); }
public void GetPossibleMatchLengths_For_2_3to4() { var expected = new int [] {6,8}; var expression = new RepeatExpression<char> (MatchLength2, 3,4); expression.AssertPossibleMatchLengths (expected); }
public void ExactConstructorSucceeds() { int min = 2; int max = min; var expression = new RepeatExpression<char> (m_A, min); Assert.AreEqual (min, expression.Minimum, "Minimum"); Assert.AreEqual (max, expression.Maximum, "Maximum"); }
public void GetPossibleMatchLengths_For_3to5_2to3() { var expected = new int [] {3+3,3+3+3,3+3+4,3+3+5,3+4,3+4+5,3+5,3+5+5,4+5+5,5+5+5}; var expression = new RepeatExpression<char> (MatchLengths3to5, 2,3); expression.AssertPossibleMatchLengths (expected); }
public SingleExpression(TextReader reader) { while (reader.Peek() != -1) { char c = (char)reader.Peek(); if (c == '(') { reader.Read(); Expression = new SingleExpression(reader); if ((char)reader.Read() != ')') { throw new Exception("Expected )"); } } else if (c == ')') { return; } else if (c == '[') { reader.Read(); Expression = new RangeExpression(reader); if ((char)reader.Read() != ']') { throw new Exception("Expected ]"); } } else if (c == '{') { reader.Read(); Expression = new RepeatExpression(reader) { Expression = Expression }; if ((char)reader.Read() != '}') { throw new Exception("Expected }"); } } else if (c == '|') { reader.Read(); Expression = new OrExpression { Left = Expression, Right = new SingleExpression(reader) }; } else if (c == '.') { reader.Read(); Expression = new ConcatExpression { Left = Expression, Right = new SingleExpression(reader).Expression }; } else if (c == '*') { reader.Read(); Expression = new StarExpression { Expression = Expression }; } else if (c == '+') { reader.Read(); Expression = new PlusExpression { Expression = Expression }; } else { throw new Exception("Unexpected " + c); } } }
public void NotAnyLength_For_NotAnyLength() { var expression = new RepeatExpression<char> (NotAnyLength, 2); Assert.IsFalse (expression.AnyLength); }
public void GetMatchesOfAnyQuestionStar_LikeRegex() { RepeatExpression<char> expression = new RepeatExpression<char> (m_any); expression.AssertMatches ("", ".*?"); }
public SingleExpression(TextReader reader) { while (reader.Peek() != -1) { char c = (char)reader.Peek(); if (c == '(') { reader.Read(); Expression = new SingleExpression(reader); if ((char)reader.Read() != ')') throw new Exception("Expected )"); } else if (c == ')') { return; } else if (c == '[') { reader.Read(); Expression = new RangeExpression(reader); if ((char)reader.Read() != ']') throw new Exception("Expected ]"); } else if (c == '{') { reader.Read(); Expression = new RepeatExpression(reader) { Expression = Expression }; if ((char)reader.Read() != '}') throw new Exception("Expected }"); } else if (c == '|') { reader.Read(); Expression = new OrExpression { Left = Expression, Right = new SingleExpression(reader) }; } else if (c == '.') { reader.Read(); Expression = new ConcatExpression { Left = Expression, Right = new SingleExpression(reader).Expression }; } else if (c == '*') { reader.Read(); Expression = new StarExpression { Expression = Expression }; } else if (c == '+') { reader.Read(); Expression = new PlusExpression { Expression = Expression }; } else { throw new Exception("Unexpected " + c); } } }