コード例 #1
0
        public void TestEvaluateCancel()
        {
            // Arrange
              var aFixture = new Fixture();

              var expressionContent = aFixture.Create<string>();
              var expression = new Expression("not", expressionContent);

              var evaluator = new Mock<IExpressionEvaluator>();
              var registry = Mock.Of<IEvaluatorSelector>(x => x.GetEvaluator(It.IsAny<Expression>()) == evaluator.Object);
              evaluator
            .Setup(x => x.Evaluate(It.Is<Expression>(e => e.ToString() == expressionContent),
                               It.IsAny<RenderingContext>(),
                               It.IsAny<TalesModel>()))
            .Returns(new ExpressionResult(ZptConstants.CancellationToken));

              var model = new TalesModel(registry);

              var sut = new NotExpressionEvaluator();

              // Act
              var result = sut.Evaluate(expression, Mock.Of<RenderingContext>(), model);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.AreEqual(true, result.Value, "Correct result");
        }
コード例 #2
0
        /// <summary>
        /// Create an expression from the content of another expression.
        /// </summary>
        /// <param name="expression">The expression from which to create another expression.</param>
        public Expression Create(Expression expression)
        {
            if(expression == null)
              {
            throw new ArgumentNullException(nameof(expression));
              }

              return Create(expression.Content);
        }
コード例 #3
0
        public void TestSimpleString(string supplied, string expected)
        {
            // Arrange
              var expression = new Expression("string", supplied);

              // Act
              var result = _sut.Evaluate(expression, Mock.Of<RenderingContext>(), _model);

              // Assert
              Assert.AreEqual(expected, result.Value);
        }
コード例 #4
0
        public void Create_FromExpression()
        {
            // Arrange
              var expression = new Expression("foo", "bar:baz");

              // Act
              var result = _sut.Create(expression);

              // Assert
              Assert.AreEqual("bar", result.Prefix);
              Assert.AreEqual("baz", result.Content);
        }
コード例 #5
0
        public void TestEvaluate_Five()
        {
            // Arrange
              var expression = new Expression(null, "bar/?prop/ToString");
              object expectedResult = _autofixture.Create<DateTime>(), propName = "Day";
              _model.Setup(x => x.TryGetRootObject("foo", _element, out expectedResult)).Returns(true);
              _model.Setup(x => x.TryGetRootObject("prop", _element, out propName)).Returns(true);

              // Act
              _sut.Evaluate(expression, _element, _model.Object);

              // Assert (by observing an exception)
        }
コード例 #6
0
        public void GetEvaluator_for_expression_without_prefix_returns_default()
        {
            // Arrange
              var expression = new Expression(null, _autofixture.Create<string>());

              _registry.Setup(x => x.GetDefaultEvaluator()).Returns(_evaluator);

              // Act
              var result = _sut.GetEvaluator(expression);

              // Assert
              Assert.AreSame(_evaluator, result, "Evaluator returned");
              _registry.Verify(x => x.GetDefaultEvaluator(), Times.Once());
        }
コード例 #7
0
        public void GetEvaluator_for_expression_uses_prefix()
        {
            // Arrange
              var expression = new Expression("foo", _autofixture.Create<string>());

              _registry.Setup(x => x.GetEvaluator("foo")).Returns(_evaluator);

              // Act
              var result = _sut.GetEvaluator(expression);

              // Assert
              Assert.AreSame(_evaluator, result, "Evaluator returned");
              _registry.Verify(x => x.GetEvaluator("foo"), Times.Once());
        }
コード例 #8
0
        public void TestEvaluate_Four()
        {
            // Arrange
              var expression = new Expression(null, "foo/?prop/ToString");
              object expectedResult = _autofixture.Create<DateTime>(), propName = "Day";
              _model.Setup(x => x.TryGetRootObject("foo", _element, out expectedResult)).Returns(true);
              _model.Setup(x => x.TryGetRootObject("prop", _element, out propName)).Returns(true);

              // Act
              var result = _sut.Evaluate(expression, _element, _model.Object);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.AreEqual(((DateTime) expectedResult).Day.ToString(), result.Value, "Result value");
        }
コード例 #9
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)
        {
            object result;

              try
              {
            result = _host.Evaluate(expression.Content,  model.GetAllDefinitions());
              }
              catch(Exception ex)
              {
            string message = String.Format(Resources.ExceptionMessages.EvaluationFailureFormat, expression.Content);
            throw new ModelEvaluationException(message, ex);
              }

              return new ExpressionResult(result);
        }
コード例 #10
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 trimmedContent = expression.Content.TrimStart();
              var result = model.Evaluate(ExpressionCreator.Create(trimmedContent), context);
              bool booleanResult = this.CoerceToBoolean(result);

              return new ExpressionResult(!booleanResult);
        }
コード例 #11
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));
              }

              string source = expression.Content, output;
              var escapeSequenceIndices = this.FindAndUnescapePlaceholders(source, out output);
              output = this.ApplyPlaceholderReplacements(output, escapeSequenceIndices, context, model);

              return new ExpressionResult(output);
        }
コード例 #12
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 allDefinitions = model.GetAllDefinitions();

              var csharpExpression = _expressionService.GetExpression(expressionText, allDefinitions.Keys);

              return new ExpressionResult(csharpExpression.Evaluate(allDefinitions));
        }
コード例 #13
0
        public void TestWithEscapedDollarsAndReplacements()
        {
            // Arrange
              string content = "Hello $$$name, how are $$$$ you this fine $$${current_state/time_of_day}?";
              var expression = new Expression("string", content);

              _pathEvaluator
            .Setup(x => x.Evaluate(It.Is<Expression>(exp => exp.ToString() == "name"),
                               It.IsAny<RenderingContext>(),
                               It.IsAny<TalesModel>()))
            .Returns(new ExpressionResult("Fred Bloggs"));
              _pathEvaluator
            .Setup(x => x.Evaluate(It.Is<Expression>(exp => exp.ToString() == "current_state/time_of_day"),
                               It.IsAny<RenderingContext>(),
                               It.IsAny<TalesModel>()))
            .Returns(new ExpressionResult("evening"));

              // Act
              var result = _sut.Evaluate(expression, Mock.Of<RenderingContext>(), _model);

              // Assert
              Assert.AreEqual("Hello $Fred Bloggs, how are $$ you this fine $evening?", result.Value);
        }
コード例 #14
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);
        }
コード例 #15
0
        public void TestEvaluate_One()
        {
            // Arrange
              var expression = new Expression(null, "foo/ToString");
              object expectedResult = _autofixture.Create<int>();
              _model.Setup(x => x.TryGetRootObject("foo", _element, out expectedResult)).Returns(true);

              // Act
              var result = _sut.Evaluate(expression, _element, _model.Object);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.AreEqual(expectedResult.ToString(), result.Value, "Result value");
        }
コード例 #16
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 abstract ExpressionResult Evaluate(Expression expression, IRenderingContext context, ITalesModel model);
コード例 #17
0
ファイル: TalesModel.cs プロジェクト: csf-dev/ZPT-Sharp
        /// <summary>
        /// Evaluate the specified TALES expression and return the result.
        /// </summary>
        /// <param name="talesExpression">The TALES expression to evaluate.</param>
        /// <param name="context">The rendering context for which we are evaluating a result.</param>
        public virtual ExpressionResult Evaluate(Expression talesExpression, IRenderingContext context)
        {
            if(talesExpression == null)
              {
            throw new ArgumentNullException(nameof(talesExpression));
              }
              if(context == null)
              {
            throw new ArgumentNullException(nameof(context));
              }

              var evaluator = EvaluatorRegistry.GetEvaluator(talesExpression);
              var output = evaluator.Evaluate(talesExpression, context, this);

              ZptConstants.TraceSource.TraceEvent(System.Diagnostics.TraceEventType.Verbose,
                                          4,
                                          Resources.LogMessageFormats.ExpressionEvaluated,
                                          talesExpression.ToString(),
                                          output.Value?? "<null>",
                                          nameof(TalesModel),
                                          nameof(Evaluate));

              return output;
        }
コード例 #18
0
        public void TestEvaluateTalesConvertible(bool conversionValue, bool expectedResult)
        {
            // Arrange
              var aFixture = new Fixture();

              var expressionContent = aFixture.Create<string>();
              var expression = new Expression("not", expressionContent);

              var evaluator = new Mock<IExpressionEvaluator>();
              var registry = Mock.Of<IEvaluatorSelector>(x => x.GetEvaluator(It.IsAny<Expression>()) == evaluator.Object);
              var convertible = Mock.Of<ITalesConvertible>(x => x.AsBoolean() == conversionValue);
              evaluator
            .Setup(x => x.Evaluate(It.Is<Expression>(e => e.ToString() == expressionContent),
                               It.IsAny<RenderingContext>(),
                               It.IsAny<TalesModel>()))
            .Returns(new ExpressionResult(convertible));

              var model = new TalesModel(registry);

              var sut = new NotExpressionEvaluator();

              // Act
              var result = sut.Evaluate(expression, Mock.Of<RenderingContext>(), model);

              // Assert
              Assert.NotNull(result, "Result nullability");
              Assert.AreEqual(expectedResult, result.Value, "Correct result");
        }