Exemplo n.º 1
0
        public void It_Should_Find_Elements_In_Its_Array()
        {
            // Arrange
            var cycleTag = new CycleTag();
            cycleTag.CycleList.Add(new TreeNode<LiquidExpression>(new LiquidExpression { Expression = new StringValue("A")}));
            cycleTag.CycleList.Add(new TreeNode<LiquidExpression>(new LiquidExpression { Expression = new StringValue("B") }));
            cycleTag.CycleList.Add(new TreeNode<LiquidExpression>(new LiquidExpression { Expression = new StringValue("C") }));

            // Assert
            Assert.That(((StringValue) cycleTag.ElementAt(0).Data.Expression).StringVal, Is.EqualTo("A"));
            Assert.That(((StringValue) cycleTag.ElementAt(1).Data.Expression).StringVal, Is.EqualTo("B"));
      }
Exemplo n.º 2
0
        /// <summary>
        /// Side effect: state is managed in the _counters dictionary.
        /// </summary>
        /// <returns></returns>
        private String GetNextCycleText(String groupName, CycleTag cycleTag)
        {

            int currentIndex;
            // Create a like dictionary key entry to keep track of this declaration.  THis takes the variable
            // names (not the eval-ed variables) or literals and concatenates them together.
            var key = "cycle_" + groupName + "_" + String.Join("|", cycleTag.CycleList.Select(x => x.Data.Expression.ToString()));
            

            while (true)
            {
                currentIndex = _counters.GetOrAdd(key, 0);
                var newindex = (currentIndex + 1) % cycleTag.Length;

                // fails if updated concurrently by someone else.
                if (_counters.TryUpdate(key, newindex, currentIndex))
                {
                    break;
                }
            }

            String result = "";
            var currentElement = cycleTag.ElementAt(currentIndex);
            LiquidExpressionEvaluator.Eval(currentElement, _templateContext)
                .WhenSuccess(x => result = ValueCaster.RenderAsString(LiquidExpressionEvaluator.Eval(currentElement, _templateContext).SuccessResult.Value))
                .WhenError(err => result = FormatErrors(new List<LiquidError> {err}));

            return result;

        }
Exemplo n.º 3
0
 public void Visit(CycleTag cycleTag)
 {
     _result += cycleTag.ToString();
 }
Exemplo n.º 4
0
        public void Visit(CycleTag cycleTag)
        {
            String groupName = null;
            if (cycleTag.GroupNameExpressionTree != null)
            {
                LiquidError error = null;
                // figure out the group name if any, otherwise use null.
                LiquidExpressionEvaluator.Eval(cycleTag.GroupNameExpressionTree, _templateContext)
                    .WhenSuccess(x => groupName = x.HasValue ? ValueCaster.RenderAsString(x.Value) : null)
                    .WhenError(x => error = x);
                if (error!=null)
                {
                    RenderError(error);
                    return;
                } 

            }
            AppendTextToCurrentAccumulator(GetNextCycleText(groupName, cycleTag));
        }