Beispiel #1
0
        public void Parse_XPathOperators_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"not (((100 div 5 mod 2) > 1) or true ()) and false ()");

            Assert.AreEqual(@"not(((100 div 5 mod 2) > 1) or true()) and false()", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(0, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #2
0
        public void Parse_MultipleVariablesWithMultipleMembers_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(
                @"(listDictionary [ ""Key1"" ] [ 0 ] . Trim() . Length * 2)"
                + @" != "
                + @"(listDictionary[""Key2""][0].Trim().Length + listDictionary[""Key3""][0].Trim().Length)");

            Assert.AreEqual("($Var1 * 2) != ($Var2 + $Var3)", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(3, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual(@"listDictionary[""Key1""][0].Trim().Length", variable1.GetReference(), "variable1.GetReference()");
            Assert.AreEqual(4, variable1.OPathParts.Length, "variable1.OPathParts.Length");

            OPathVariable variable2 = GetVariable("Var2", parseResult.GetVariables());

            Assert.AreEqual(@"listDictionary[""Key2""][0].Trim().Length", variable2.GetReference(), "variable3.GetReference()");
            Assert.AreEqual(4, variable1.OPathParts.Length, "variable2.OPathParts.Length");

            OPathVariable variable3 = GetVariable("Var3", parseResult.GetVariables());

            Assert.AreEqual(@"listDictionary[""Key3""][0].Trim().Length", variable3.GetReference(), "variable3.GetReference()");
            Assert.AreEqual(4, variable1.OPathParts.Length, "variable3.OPathParts.Length");
        }
Beispiel #3
0
        public void Parse_XPathOperatorsMixedCase_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"Not (((100 Div 5 Mod 2) > 1) OR True ()) AND False () AND (String-Length ('AND') > 2)");

            Assert.AreEqual(@"not(((100 div 5 mod 2) > 1) or true()) and false() and (string-length('AND') > 2)", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(0, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #4
0
        public void Parse_XPathFunctions_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"Custom:Processing-Date () > date_time:now ()");

            Assert.AreEqual(@"custom:processing-date() > date_time:now()", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(0, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #5
0
        public void Parse_NonXPathOperators_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"(! (((100/5%2) > 1)||true())&false()==true())|(true()!=false())");

            Assert.AreEqual(@"(not(((100 div 5 mod 2) > 1) or true()) and false()=true()) or (true()!=false())", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(0, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #6
0
        public void Parse_StandardSourceObjects_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"true and false or null");

            Assert.AreEqual(@"$Var1 and $Var2 or $Var3", parseResult.GetXPath(), "parseResult.GetXPath()");

            Assert.AreEqual(3, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #7
0
        public void Parse_NoVariables_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"""Variable's1"" = 'Variable""s2'");

            Assert.AreEqual(@"""Variable's1"" = 'Variable""s2'", parseResult.GetXPath(), "parseResult.GetXPath()");

            Assert.AreEqual(0, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");
        }
Beispiel #8
0
        private static void HandleCurrentPositionInString(OPathParseResult parseResult, char opathChar)
        {
            if (parseResult.IsStringDelimiter(opathChar))
            {
                parseResult.CurrentPosition = OPathParsePosition.InExpression;
            }

            parseResult.AppendToXPath(opathChar);
        }
Beispiel #9
0
 private static void HandleCurrentPositionInStringKey(OPathParseResult parseResult, char opathChar)
 {
     if (parseResult.IsStringDelimiter(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.PostIndexerValue;
     }
     else
     {
         parseResult.AppendToStringKey(opathChar);
     }
 }
Beispiel #10
0
        public void Parse_DuplicateVariables_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("Variable1 = Variable1");

            Assert.AreEqual("$Var1 = $Var1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1", variable1.GetReference(), "variable1.GetReference()");
            Assert.AreEqual(0, variable1.OPathParts.Length, "variable1.OPathParts.Length");
        }
Beispiel #11
0
        private static void HandleFinalPositionInIdentifier(OPathParseResult parseResult)
        {
            string identifier = parseResult.GetAndResetIdentifier();

            if (IsXPathOperator(identifier))
            {
                parseResult.AppendOperatorToXPath(identifier);
            }
            else
            {
                parseResult.AddVariable(identifier);
            }
        }
Beispiel #12
0
        public void Parse_VariableWithPropertyAtEndWithWhitespace_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("1 = Variable1.Property ");

            Assert.AreEqual("1 = $Var1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1.Property", variable1.GetReference(), "variable1.GetReference()");

            Assert.AreEqual(1, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariablePropertyPart(variable1, 0, "Property");
        }
Beispiel #13
0
        public void Parse_VariableWithStringKeyWithWhitespace_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"Variable1[ ""Key"" ] = 1");

            Assert.AreEqual("$Var1 = 1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual(@"Variable1[""Key""]", variable1.GetReference(), "variable1.GetReference()");

            Assert.AreEqual(1, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariableStringKeyPart(variable1, 0, @"Key");
        }
Beispiel #14
0
        public void Parse_VariableWithEmptyMethodAtEnd_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("1 = Variable1.Method()");

            Assert.AreEqual("1 = $Var1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1.Method()", variable1.GetReference(), "variable1.GetReference()");

            Assert.AreEqual(1, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariableMethodPart(variable1, 0, "Method");
        }
Beispiel #15
0
        public void Parse_VariableWithNegativeIntIndexer_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("Variable1[-1] = 1");

            Assert.AreEqual("$Var1 = 1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1", variable1.Name, "variable1.Name");
            Assert.AreEqual("[-1]", variable1.GetOPath(), "variable1.GetOPath()");

            Assert.AreEqual(1, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariableIntIndexPart(variable1, 0, -1);
        }
Beispiel #16
0
        public void Parse_VariableWithMultiplePropertiesWithWhitespace_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("Variable1 . Property1 . Property2 = 1");

            Assert.AreEqual("$Var1 = 1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1.Property1.Property2", variable1.GetReference(), "variable1.GetReference()");

            Assert.AreEqual(2, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariablePropertyPart(variable1, 0, "Property1");
            AssertVariablePropertyPart(variable1, 1, "Property2");
        }
Beispiel #17
0
        public void Parse_VariableWithIntIndexersWithWhitespace_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse("Variable1 [ 1 ] [ 2 ] = 1");

            Assert.AreEqual("$Var1 = 1", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual("Variable1", variable1.Name, "variable1.Name");
            Assert.AreEqual("[1][2]", variable1.GetOPath(), "variable1.GetOPath()");

            Assert.AreEqual(2, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariableIntIndexPart(variable1, 0, 1);
            AssertVariableIntIndexPart(variable1, 1, 2);
        }
Beispiel #18
0
 private static void HandleCurrentPositionInMethodParameters(OPathParseResult parseResult, char opathChar)
 {
     if (IsClosingBracket(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InVariableOPath;
         parseResult.AddOPathMethodPart();
     }
     else if (IsWhitespaceChar(opathChar))
     {
         // Ignore
     }
     else
     {
         throw new OPathException("Non-empty method starting at position " + (parseResult.MemberStartIndex + 1));
     }
 }
Beispiel #19
0
        public void Parse_VariableWithMultipleMembers_ExpectedResult()
        {
            OPathParseResult parseResult = OPathParser.Parse(@"listDictionary[""Key1""][1].Trim().Length != 6");

            Assert.AreEqual("$Var1 != 6", parseResult.GetXPath(), "parseResult.GetXPath()");
            Assert.AreEqual(1, parseResult.GetVariables().Length, "parseResult.GetVariables().Length");

            OPathVariable variable1 = GetVariable("Var1", parseResult.GetVariables());

            Assert.AreEqual(@"listDictionary[""Key1""][1].Trim().Length", variable1.GetReference(), "variable1.GetReference()");

            Assert.AreEqual(4, variable1.OPathParts.Length, "variable1.OPathParts.Length");
            AssertVariableStringKeyPart(variable1, 0, "Key1");
            AssertVariableIntIndexPart(variable1, 1, 1);
            AssertVariableMethodPart(variable1, 2, "Trim");
            AssertVariablePropertyPart(variable1, 3, "Length");
        }
Beispiel #20
0
 private static void HandleCurrentPositionInIntIndexer(OPathParseResult parseResult, char opathChar)
 {
     if (IsNumericChar(opathChar))
     {
         parseResult.AppendToIntIndexer(opathChar);
     }
     else if (IsIndexerEndChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InVariableOPath;
         parseResult.AddOPathIntIndexerPart();
     }
     else if (IsWhitespaceChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.PostIndexerValue;
     }
     else
     {
         throw new OPathException("Invalid character in indexer starting at position " + (parseResult.IndexerStartIndex + 1));
     }
 }
Beispiel #21
0
 private static void HandleCurrentPositionPreIndexerValue(OPathParseResult parseResult, char opathChar,
                                                          int opathCharIndex)
 {
     if (IsWhitespaceChar(opathChar))
     {
         // Ignore
     }
     else if (IsStringDelimiterChar(opathChar))
     {
         parseResult.CurrentPosition        = OPathParsePosition.InStringKey;
         parseResult.CurrentStringDelimiter = opathChar;
         parseResult.StringStartIndex       = opathCharIndex;
     }
     else if (IsNumericStartChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InIntIndexer;
         parseResult.AppendToIntIndexer(opathChar);
     }
     else
     {
         throw new OPathException("Invalid character in indexer starting at position " + (parseResult.IndexerStartIndex + 1));
     }
 }
Beispiel #22
0
 private static void HandleCurrentPositionInMember(OPathParseResult parseResult, char opathChar)
 {
     if (IsMemberChar(opathChar))
     {
         parseResult.AppendToMember(opathChar);
         parseResult.ResetWhitespace();
     }
     else if (IsOpeningBracket(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InMethodParameters;
         parseResult.ResetWhitespace();
     }
     else if (IsMemberPrefixChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InMember;
         parseResult.AddOPathPropertyPart();
         parseResult.ResetWhitespace();
     }
     else if (IsIndexerStartChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.PreIndexerValue;
         parseResult.AddOPathPropertyPart();
         parseResult.ResetWhitespace();
     }
     else if (IsWhitespaceChar(opathChar))
     {
         parseResult.AppendCharToWhitespace();
     }
     else
     {
         parseResult.CurrentPosition = OPathParsePosition.InExpression;
         parseResult.AddOPathPropertyPart();
         parseResult.AddVariable();
         parseResult.AppendToXPath(parseResult.GetAndResetWhitespace());
         parseResult.AppendToXPath(opathChar);
     }
 }
Beispiel #23
0
        private static void HandleCurrentPositionPostIndexerValue(OPathParseResult parseResult, char opathChar)
        {
            if (IsWhitespaceChar(opathChar))
            {
                // Ignore
            }
            else if (IsIndexerEndChar(opathChar))
            {
                parseResult.CurrentPosition = OPathParsePosition.InVariableOPath;

                if (parseResult.IsInIntIndexer())
                {
                    parseResult.AddOPathIntIndexerPart();
                }
                else
                {
                    parseResult.AddOPathStringKeyPart();
                }
            }
            else
            {
                throw new OPathException("Invalid character in indexer starting at position " + (parseResult.IndexerStartIndex + 1));
            }
        }
Beispiel #24
0
 private static void HandleCurrentPositionInExpression(OPathParseResult parseResult, char opathChar,
                                                       char nextOPathChar, int opathCharIndex)
 {
     if (IsWhitespaceChar(opathChar))
     {
         parseResult.AppendWhitespaceCharToXPath();
     }
     else if (IsAlphabeticChar(opathChar))
     {
         parseResult.CurrentPosition = OPathParsePosition.InIdentifier;
         parseResult.AppendToIdentifier(opathChar);
         parseResult.IdentifierStartIndex = opathCharIndex;
     }
     else if (IsStringDelimiterChar(opathChar))
     {
         parseResult.CurrentPosition        = OPathParsePosition.InString;
         parseResult.CurrentStringDelimiter = opathChar;
         parseResult.AppendToXPath(opathChar);
         parseResult.StringStartIndex = opathCharIndex;
     }
     else if (IsOperatorChar(opathChar))
     {
         if (nextOPathChar == opathChar)
         {
             // Ignore multiple operators, such as && and ||
         }
         else
         {
             parseResult.AppendOperatorToXPath(opathChar);
         }
     }
     else if (IsNotChar(opathChar))
     {
         if (IsEqualityChar(nextOPathChar))
         {
             // Allow != through
             parseResult.AppendToXPath(opathChar);
         }
         else
         {
             parseResult.CurrentPosition = OPathParsePosition.InIdentifier;
             parseResult.AppendToIdentifier("not");
             parseResult.IdentifierStartIndex = opathCharIndex;
         }
     }
     else if (IsEqualityChar(opathChar))
     {
         if (nextOPathChar == opathChar)
         {
             // Ignore multiple equalities
         }
         else
         {
             parseResult.AppendToXPath(opathChar);
         }
     }
     else
     {
         parseResult.AppendToXPath(opathChar);
     }
 }
Beispiel #25
0
        private static void HandleCurrentPositionInIdentifier(OPathParseResult parseResult, char opathChar,
                                                              int opathCharIndex)
        {
            if (IsIdentifierChar(opathChar))
            {
                if (parseResult.HasWhitespace)
                {
                    string identifier = parseResult.GetIdentifier();

                    if (IsStandardSourceObjectIdentifier(identifier))
                    {
                        parseResult.ResetIdentifier();
                        parseResult.AddVariable(identifier);
                        parseResult.AppendWhitespaceCharToXPath();
                    }
                }

                parseResult.AppendToIdentifier(opathChar);
            }
            else if (IsWhitespaceChar(opathChar))
            {
                string identifier = parseResult.GetIdentifier();

                if (IsXPathOperator(identifier))
                {
                    parseResult.CurrentPosition = OPathParsePosition.InExpression;
                    parseResult.ResetIdentifier();
                    parseResult.AppendOperatorToXPath(identifier);
                    parseResult.AppendWhitespaceCharToXPath();
                }
                else
                {
                    parseResult.AppendCharToWhitespace();
                }
            }
            else if (IsOpeningBracket(opathChar))
            {
                parseResult.CurrentPosition = OPathParsePosition.InExpression;
                string identifier = parseResult.GetAndResetIdentifier();
                parseResult.AppendFunctionToXPath(identifier);
                parseResult.AppendToXPath(opathChar);
                parseResult.ResetWhitespace();
            }
            else if (IsMemberPrefixChar(opathChar))
            {
                parseResult.CurrentPosition     = OPathParsePosition.InMember;
                parseResult.CurrentVariableName = parseResult.GetAndResetIdentifier();
                parseResult.MemberStartIndex    = opathCharIndex;
                parseResult.ResetWhitespace();
            }
            else if (IsIndexerStartChar(opathChar))
            {
                parseResult.CurrentPosition     = OPathParsePosition.PreIndexerValue;
                parseResult.CurrentVariableName = parseResult.GetAndResetIdentifier();
                parseResult.IndexerStartIndex   = opathCharIndex;
                parseResult.ResetWhitespace();
            }
            else if (IsStringDelimiterChar(opathChar))
            {
                parseResult.CurrentPosition        = OPathParsePosition.InString;
                parseResult.CurrentStringDelimiter = opathChar;
                parseResult.AppendToXPath(opathChar);
                parseResult.StringStartIndex = opathCharIndex;
            }
            else
            {
                parseResult.CurrentPosition = OPathParsePosition.InExpression;

                string identifier = parseResult.GetAndResetIdentifier();

                if (IsXPathOperator(identifier))
                {
                    parseResult.AppendOperatorToXPath(identifier);
                }
                else
                {
                    parseResult.AddVariable(identifier);
                }

                parseResult.AppendToXPath(parseResult.GetAndResetWhitespace());
                parseResult.AppendToXPath(opathChar);
            }
        }
Beispiel #26
0
        public static OPathParseResult Parse(string opath)
        {
            OPathParseResult parseResult = new OPathParseResult();

            char[] opathChars = opath.ToCharArray();

            for (int opathCharIndex = 0; opathCharIndex < opathChars.Length; opathCharIndex++)
            {
                char opathChar = opathChars[opathCharIndex];

                char nextOPathChar = default(char);
                if (opathCharIndex + 1 < opathChars.Length)
                {
                    nextOPathChar = opathChars[opathCharIndex + 1];
                }

                switch (parseResult.CurrentPosition)
                {
                case OPathParsePosition.InExpression:
                    HandleCurrentPositionInExpression(parseResult, opathChar, nextOPathChar, opathCharIndex);
                    break;

                case OPathParsePosition.InString:
                    HandleCurrentPositionInString(parseResult, opathChar);
                    break;

                case OPathParsePosition.InIdentifier:
                    HandleCurrentPositionInIdentifier(parseResult, opathChar, opathCharIndex);
                    break;

                case OPathParsePosition.InMember:
                    HandleCurrentPositionInMember(parseResult, opathChar);
                    break;

                case OPathParsePosition.PreIndexerValue:
                    HandleCurrentPositionPreIndexerValue(parseResult, opathChar, opathCharIndex);
                    break;

                case OPathParsePosition.PostIndexerValue:
                    HandleCurrentPositionPostIndexerValue(parseResult, opathChar);
                    break;

                case OPathParsePosition.InIntIndexer:
                    HandleCurrentPositionInIntIndexer(parseResult, opathChar);
                    break;

                case OPathParsePosition.InStringKey:
                    HandleCurrentPositionInStringKey(parseResult, opathChar);
                    break;

                case OPathParsePosition.InVariableOPath:
                    HandleCurrentPositionInVariableOPath(parseResult, opathChar);
                    break;

                case OPathParsePosition.InMethodParameters:
                    HandleCurrentPositionInMethodParameters(parseResult, opathChar);
                    break;

                default:
                    throw new OPathException("Unhandled current position in expression: " + parseResult.CurrentPosition);
                }
            }

            switch (parseResult.CurrentPosition)
            {
            case OPathParsePosition.InExpression:
                // Nothing to do as we will have nothing buffered
                break;

            case OPathParsePosition.InIdentifier:
                HandleFinalPositionInIdentifier(parseResult);
                break;

            case OPathParsePosition.InMember:
                HandleFinalPositionInMember(parseResult);
                break;

            case OPathParsePosition.InVariableOPath:
                HandleFinalPositionInVariableOPath(parseResult);
                break;

            case OPathParsePosition.InString:
                throw new OPathException("Unclosed string starting at position " + (parseResult.StringStartIndex + 1));

            case OPathParsePosition.PreIndexerValue:
            case OPathParsePosition.PostIndexerValue:
            case OPathParsePosition.InIntIndexer:
                throw new OPathException("Unclosed indexer starting at position " + (parseResult.IndexerStartIndex + 1));

            case OPathParsePosition.InStringKey:
                throw new OPathException("Unclosed string key starting at position " + (parseResult.StringStartIndex + 1));

            case OPathParsePosition.InMethodParameters:
                throw new OPathException("Unclosed method starting at position " + (parseResult.MemberStartIndex + 1));

            default:
                throw new OPathException("Unhandled current position at end of expression: " + parseResult.CurrentPosition);
            }

            return(parseResult);
        }
Beispiel #27
0
 private static void HandleFinalPositionInMember(OPathParseResult parseResult)
 {
     parseResult.AddOPathPropertyPart();
     parseResult.AddVariable();
 }
Beispiel #28
0
 private static void HandleFinalPositionInVariableOPath(OPathParseResult parseResult)
 {
     parseResult.AddVariable();
 }