예제 #1
0
        public void Evaluate_NullSourceObjects_ExpectedResult()
        {
            bool actualValue =
                (bool)OPathNavigator.Evaluate(null, OPathExpression.Compile("true()"));

            Assert.AreEqual(true, actualValue, "true()");
        }
예제 #2
0
 public void Evaluate_ExpressionMethodException_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"ExceptionThrower\.Method\(\).*Method exception"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"ExceptionThrower.Method() = null"));
     });
 }
예제 #3
0
 public void Evaluate_SingleValuePropertyException_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"ExceptionThrower\.Property.*Property exception"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"ExceptionThrower.Property"));
     });
 }
예제 #4
0
 public void Evaluate_NullReferenceInObjectPath_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"NullObject.*null"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"NullObject.Property = null"));
     });
 }
예제 #5
0
 public void Evaluate_UnclosedString_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"Unclosed string"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"NonEmptyString = X"""));
     });
 }
예제 #6
0
 public void Evaluate_UnclosedIndexer_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"Invalid character in indexer"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"NonEmptyIntList[0 = 0"));
     });
 }
예제 #7
0
 public void Evaluate_ArrayIndexOutOfBounds_ExpectedExpectation()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"EmptyIntArray\[616\].*EmptyIntArray.*616.*index out of range"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile("EmptyIntArray[616] = 0"));
     });
 }
예제 #8
0
 public void Evaluate_NonExistantStringIndex_ExpectedExpectation()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"EmptyDictionary\[""Lucifer""\].*EmptyDictionary.*Lucifer.*key not found"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"EmptyDictionary[""Lucifer""] = 0"));
     });
 }
예제 #9
0
 public void Evaluate_NonExistantIntIndexer_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"EmptyString\[0\].*EmptyString.*System\.String.*int indexer"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"EmptyString[0] = 0"));
     });
 }
예제 #10
0
 public void Evaluate_UnspecifiedObject_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"UnspecifiedObject"),
                   () =>
     {
         OPathNavigator.Evaluate(new OPathDocument(), OPathExpression.Compile("UnspecifiedObject = 0"));
     });
 }
예제 #11
0
 public void Evaluate_UnclosedBracket_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"\(true\(\) = false\(\).*has an invalid token"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument, OPathExpression.Compile(@"(true() = false()"));
     });
 }
예제 #12
0
 public void Evaluate_NonExistantEmptyMethod_ExpectedException()
 {
     Assert.Throws(Is.TypeOf <OPathException>()
                   .And.Message.Matches(@"EmptyString\.NonExistantEmptyMethod().*EmptyString.*System\.String.*NonExistantEmptyMethod"),
                   () =>
     {
         OPathNavigator.Evaluate(m_OPathDocument,
                                 OPathExpression.Compile("EmptyString.NonExistantEmptyMethod() = 0"));
     });
 }
예제 #13
0
        private void AssertExpressionEquals(object expectedValue, string expression,
                                            OPathOptions opathOptions, object defaultValue)
        {
            OPathExpression opathExpression = OPathExpression.Compile(expression);

            object actualValue =
                OPathNavigator.Evaluate(m_OPathDocument, opathExpression, opathOptions, defaultValue,
                                        (string message) => Console.WriteLine(message));

            Assert.AreEqual(expectedValue, actualValue, expression);
        }
예제 #14
0
        public void LoggingExample()
        {
            var expression = OPathExpression.Compile("StringArray[0].Length > Int");

            var document = new OPathDocument {
                { "StringArray", new[] { "String" } }, { "Int", 0 }
            };

            var navigator = OPathNavigator.CreateNavigator(document);

            var result = navigator.Evaluate(expression, OPathOptions.Defaults, null, Console.WriteLine);

            Console.WriteLine(result);
        }
예제 #15
0
        public void HelloOPathNETExample()
        {
            var expression = OPathExpression.Compile("MyVariable = 'OPath.NET'");

            var document = new OPathDocument {
                { "MyVariable", "OPath.NET" }
            };

            var navigator = OPathNavigator.CreateNavigator(document);

            var result = navigator.Evaluate(expression);

            Console.WriteLine(result);
        }
예제 #16
0
        public void CustomFunctionExample()
        {
            var expression = OPathExpression.Compile("my-prefix:my-function(String, Int)");

            var document = new OPathDocument {
                { "String", "String" }, { "Int", 616 }
            };

            var navigator = OPathNavigator.CreateNavigator(document);

            navigator.RegisterCustomFunction("my-prefix", "my-function",
                                             args => string.Join(",", Array.ConvertAll(args, o => o + "")));

            var result = navigator.Evaluate(expression);

            Console.WriteLine(result);
        }
예제 #17
0
        public void TestOPathPerformance()
        {
            HiPerfTimer rawTimer = new HiPerfTimer();

            rawTimer.Start();

            for (int i = 0; i < EVALUATION_COUNT; i++)
            {
                OPathExpression opathExpression = OPathExpression.Compile(EXPRESSION);

                OPathDocument opathDocument = new OPathDocument();
                opathDocument.Add("listDictionary", m_ListDictionary);

                OPathNavigator opathNavigator = OPathNavigator.CreateNavigator(opathDocument);

                opathNavigator.Evaluate(opathExpression);
            }

            rawTimer.Stop();

            Console.WriteLine("Uncompiled OPath performance {0} evaluations in {1:0.000} millis",
                              EVALUATION_COUNT, (rawTimer.Duration * 1000));

            HiPerfTimer cachedTimer = new HiPerfTimer();

            cachedTimer.Start();

            OPathExpression cachedOPathExpression = OPathExpression.Compile(EXPRESSION);

            for (int i = 0; i < EVALUATION_COUNT; i++)
            {
                OPathDocument opathDocument = new OPathDocument();
                opathDocument.Add("listDictionary", m_ListDictionary);

                OPathNavigator opathNavigator = OPathNavigator.CreateNavigator(opathDocument);

                opathNavigator.Evaluate(cachedOPathExpression);
            }

            cachedTimer.Stop();

            Console.WriteLine("Compiled OPath performance {0} evaluations in {1:0.000} millis",
                              EVALUATION_COUNT, (cachedTimer.Duration * 1000));
        }
예제 #18
0
        public void OPathOptionsExample()
        {
            var expression = OPathExpression.Compile("MyVariable.Length = 0");

            var document = new OPathDocument {
                { "MyVariable", null }
            };

            var navigator = OPathNavigator.CreateNavigator(document);

            try
            {
                var result = navigator.Evaluate(expression);

                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                var result = navigator.Evaluate(expression, OPathOptions.ReturnDefaultForNull);

                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            try
            {
                var result = navigator.Evaluate(expression, OPathOptions.ReturnDefaultForNull, 0);

                Console.WriteLine(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }