Ejemplo n.º 1
0
        public void TestNextComponent(string pathString, int numberOfCalls, bool expectedResult)
        {
            // Arrange
              var path = Path.Create(pathString);
              var sut = new PathWalker(path);
              bool result = false;

              // Act
              for(int i = 0; i < numberOfCalls; i++)
              {
            result = sut.NextComponent();
              }

              // Assert
              Assert.AreEqual(expectedResult, result);
        }
Ejemplo n.º 2
0
        public void TestCurrentComponent(string pathString, int component, int expectedPartCount)
        {
            // Arrange
              var path = Path.Create(pathString);
              var sut = new PathWalker(path);
              int result = 0;

              // Act
              for(int i = 0; i < component; i++)
              {
            sut.NextComponent();
            result = sut.CurrentComponent.Parts.Count;
              }

              // Assert
              Assert.AreEqual(expectedPartCount, result);
        }
        /// <summary>
        /// Attempts to get a root object, from which to begin traversal of the path.
        /// </summary>
        /// <returns><c>true</c>, if the root object was retrieved, <c>false</c> otherwise.</returns>
        /// <param name="walker">A TALES path walker.</param>
        /// <param name="element">A ZPT element.</param>
        /// <param name="model">The TALES model.</param>
        /// <param name="result">Exposes the result of this operation.</param>
        protected virtual bool TryGetTraversalRoot(PathWalker walker,
                                               ZptElement element,
                                               TalesModel model,
                                               out object result)
        {
            bool output;

              if(walker.NextPart()
             && model.TryGetLocalRootObject(walker.CurrentPart.Value, element, out result))
              {
            output = true;
              }
              else
              {
            output = false;
            result = null;
              }

              return output;
        }
Ejemplo n.º 4
0
        public void TestCurrentPart(string pathString, int component, int part, string expectedValue)
        {
            // Arrange
              var path = Path.Create(pathString);
              var sut = new PathWalker(path);

              for(int i = 0; i < component; i++)
              {
            sut.NextComponent();
              }

              // Act
              for(int i = 0; i < part; i++)
              {
            sut.NextPart();
              }

              // Assert
              Assert.NotNull(sut.CurrentPart, "Result nullability");
              Assert.AreEqual(expectedValue, sut.CurrentPart.Value, "Result value");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Evaluate the specified expression, for the given element and model.
        /// </summary>
        /// <param name="expression">The expression to evaluate.</param>
        /// <param name="context">The rendering context for the expression being evaluated.</param>
        /// <param name="model">The ZPT model, providing the context for evaluation.</param>
        public override ExpressionResult Evaluate(Expression expression, IRenderingContext context, ITalesModel model)
        {
            if(expression == null)
              {
            throw new ArgumentNullException(nameof(expression));
              }
              if(context == null)
              {
            throw new ArgumentNullException(nameof(context));
              }
              if(model == null)
              {
            throw new ArgumentNullException(nameof(model));
              }

              var expressionText = expression.Content;
              var path = Path.Create(expressionText);
              var walker = new PathWalker(path);
              object output;

              try
              {
            output = this.WalkPath(walker, context, model);
              }
              catch(TraversalException ex)
              {
            string message = String.Format(Resources.ExceptionMessages.CouldNotWalkAnyPathsWithExpression,
                                       expressionText,
                                       context?.Element?.Name);
            throw new ModelEvaluationException(message, ex) {
              ExpressionText = expressionText,
              ElementName = context?.Element?.Name
            };
              }

              return new ExpressionResult(output);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Walks and evaluates a TALES path.
        /// </summary>
        /// <returns>The evaluated value of the path.</returns>
        /// <param name="walker">A TALES path walker, containing a path.</param>
        /// <param name="context">The rendering context for the expression being evaluated.</param>
        /// <param name="model">The ZPT model, providing the context for evaluation.</param>
        private object WalkPath(PathWalker walker, IRenderingContext context, ITalesModel model)
        {
            object output = null;
              bool success = false;

              while(walker.NextComponent() && !success)
              {
            success = this.WalkComponent(walker, context, model, out output);
              }

              if(!success)
              {
            throw new TraversalException(Resources.ExceptionMessages.CouldNotWalkAnyPaths);
              }

              return output;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Walks and evaluates a single TALES path component.
        /// </summary>
        /// <returns>
        /// <c>true</c>, if the path component was successfully evaluated, <c>false</c> otherwise.
        /// </returns>
        /// <param name="walker">A TALES path walker, containing a path.</param>
        /// <param name="context">The rendering context for the expression being evaluated.</param>
        /// <param name="model">The ZPT model, providing the context for evaluation.</param>
        /// <param name="result">Exposes the result of the evaluation.</param>
        private bool WalkComponent(PathWalker walker, IRenderingContext context, ITalesModel model, out object result)
        {
            bool output;

              if(this.TryGetTraversalRoot(walker, context, model, out result))
              {
            object traversalChild;
            output = true;

            while(walker.NextPart())
            {
              string partName;

              if(this.TryGetPartName(walker.CurrentPart, context, model, out partName)
             && ObjectTraverser.Default.Traverse(result, partName, out traversalChild, context))
              {
            result = traversalChild;
              }
              else
              {
            output = false;
            result = null;
            break;
              }
            }
              }
              else
              {
            output = false;
            result = null;
              }

              return output;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Attempts to get a root object, from which to begin traversal of the path.
        /// </summary>
        /// <returns><c>true</c>, if the root object was retrieved, <c>false</c> otherwise.</returns>
        /// <param name="walker">A TALES path walker.</param>
        /// <param name="context">The rendering context.</param>
        /// <param name="model">The TALES model.</param>
        /// <param name="result">Exposes the result of this operation.</param>
        protected virtual bool TryGetTraversalRoot(PathWalker walker,
                                               IRenderingContext context,
                                               ITalesModel model,
                                               out object result)
        {
            bool output;

              if(walker.NextPart()
             && model.TryGetRootObject(walker.CurrentPart.Value, context, out result))
              {
            output = true;
              }
              else
              {
            output = false;
            result = null;
              }

              return output;
        }
Ejemplo n.º 9
0
        public void TestReset()
        {
            // Arrange
              var path = Path.Create("foo/bar|wibble/wobble");
              var sut = new PathWalker(path);

              sut.NextComponent();
              sut.NextComponent();
              sut.NextPart();
              sut.NextPart();

              // Act
              sut.Reset();

              // Assert
              Assert.IsTrue(sut.NextComponent(),              "NextComponent result");
              Assert.IsTrue(sut.NextPart(),                   "NextPart result");
              Assert.AreEqual("foo",  sut.CurrentPart.Value,  "CurrentPart");
        }
Ejemplo n.º 10
0
        public void TestNextPartException()
        {
            // Arrange
              var path = Path.Create("foo/bar");
              var sut = new PathWalker(path);

              // Act
              sut.NextPart();

              // Assert (by observing an exception)
        }