public void TestGetQuotedString_EscapeAtEnd() { var cursor = new PatternCursor("'abc\\"); Assert.AreEqual('\'', GetNextCharacter(cursor)); cursor.GetQuotedString('\'', ref failure); Assert.Throws<InvalidPatternException>(() => failure.GetResultOrThrow()); }
public void TestGetQuotedString_handlesDoubleQuote() { var cursor = new PatternCursor("\"abc\""); char openQuote = GetNextCharacter(cursor); string actual = cursor.GetQuotedString(openQuote, ref failure); Assert.AreEqual("abc", actual); Assert.IsFalse(cursor.MoveNext()); }
public void TestGetQuotedString_HandlesEscapedCloseQuote() { var cursor = new PatternCursor("'ab\\'c'"); char openQuote = GetNextCharacter(cursor); string actual = cursor.GetQuotedString(openQuote); Assert.AreEqual("ab'c", actual); Assert.IsFalse(cursor.MoveNext()); }
public void TestGetQuotedString_HandlesOtherQuote() { var cursor = new PatternCursor("[abc]"); GetNextCharacter(cursor); string actual = cursor.GetQuotedString(']'); Assert.AreEqual("abc", actual); Assert.IsFalse(cursor.MoveNext()); }
public void TestGetQuotedString_Empty() { var cursor = new PatternCursor("''"); char openQuote = GetNextCharacter(cursor); string actual = cursor.GetQuotedString(openQuote); Assert.AreEqual(string.Empty, actual); Assert.IsFalse(cursor.MoveNext()); }
public void TestGetQuotedString() { var cursor = new PatternCursor("'abc'"); Assert.AreEqual('\'', GetNextCharacter(cursor)); string actual = cursor.GetQuotedString('\''); Assert.AreEqual("abc", actual); Assert.IsFalse(cursor.MoveNext()); }
public void GetQuotedString_Valid(string pattern, string expected) { var cursor = new PatternCursor(pattern); Assert.AreEqual('\'', GetNextCharacter(cursor)); string actual = cursor.GetQuotedString('\''); Assert.AreEqual(expected, actual); Assert.IsFalse(cursor.MoveNext()); }
public void GetQuotedString_NotAtEnd() { var cursor = new PatternCursor("'abc'more"); char openQuote = GetNextCharacter(cursor); string actual = cursor.GetQuotedString(openQuote); Assert.AreEqual("abc", actual); ValidateCurrentCharacter(cursor, 4, '\''); Assert.AreEqual('m', GetNextCharacter(cursor)); }
internal static void HandleQuote(PatternCursor pattern, SteppedPatternBuilder <TResult, TBucket> builder) { string quoted = pattern.GetQuotedString(pattern.Current); builder.AddLiteral(quoted, ParseResult <TResult> .QuotedStringMismatch); }
public void TestGetQuotedString_simple() { var cursor = new PatternCursor("'abc'"); char openQuote = GetNextCharacter(cursor); string actual = cursor.GetQuotedString(openQuote, ref failure); AssertNoFailure(); Assert.AreEqual("abc", actual); Assert.IsFalse(cursor.MoveNext()); }
public void TestGetQuotedString_missingCloseQuote() { var cursor = new PatternCursor("'abc"); char openQuote = GetNextCharacter(cursor); cursor.GetQuotedString(openQuote, ref failure); Assert.Throws<InvalidPatternException>(() => failure.GetResultOrThrow()); }
/// <summary> /// Returns true if the next character in the pattern might represent a digit from another value (e.g. a different /// field). Returns false otherwise, e.g. if we've reached the end of the pattern, or the next character is a literal /// non-digit. /// </summary> private static bool CheckIfNextCharacterMightBeDigit(PatternCursor pattern) { int originalIndex = pattern.Index; try { if (!pattern.MoveNext()) { return(false); } char next = pattern.Current; // If we've got an unescaped letter, assume it could be a field. // If we've got an unescaped digit, it's a no-brainer. if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'z') || (next >= 'A' && next <= 'Z')) { return(true); } // A % is tricky - could be any number of things. Act conservatively. if (next == '%') { return(true); } // Quoting: find the unquoted text, and see whether it starts with a non-digit. if (next == '\'' || next == '\"') { // If this throws, catch it and let it get thrown later, in the right context. try { string quoted = pattern.GetQuotedString(next); // Empty quotes - could be trying to disguise a digit afterwards... if (quoted.Length == 0) { return(true); } char firstQuoted = quoted[0]; // Check if the quoted string starts with a digit, basically. return(firstQuoted >= '0' && firstQuoted <= '9'); } catch (InvalidPatternException) { // Doesn't really matter... return(true); } } if (next == '\\') { if (!pattern.MoveNext()) { return(true); // Doesn't really matter; we'll throw an exception soon anyway. } char quoted = pattern.Current; return(quoted >= '0' && quoted <= '9'); } // Could be a date/time separator, but otherwise it's just something that we'll include // as a literal and won't be a digit. return(false); } finally { pattern.Move(originalIndex); } }
public void TestGetQuotedString_MissingCloseQuote() { var cursor = new PatternCursor("'abc"); char openQuote = GetNextCharacter(cursor); Assert.Throws<InvalidPatternException>(() => cursor.GetQuotedString(openQuote)); }
public void GetQuotedString_Invalid(string pattern) { var cursor = new PatternCursor(pattern); Assert.AreEqual('\'', GetNextCharacter(cursor)); Assert.Throws<InvalidPatternException>(() => cursor.GetQuotedString('\'')); }