public void It_Should_Dereference_An_Array_Element() { // Arrange IList<IExpressionConstant> objlist = new List<IExpressionConstant> { new StringValue("a string"), NumericValue.Create(123), NumericValue.Create(456m), new BooleanValue(false) }; ArrayValue arrayValue = new ArrayValue(objlist); // Assert var valueAt = arrayValue.ValueAt(0); Assert.That(valueAt.Value, Is.EqualTo(objlist[0].Value)); }
private LiquidExpressionResult DoLookup(ITemplateContext ctx, ArrayValue arrayValue, IExpressionConstant indexProperty) { String propertyNameString = ValueCaster.RenderAsString(indexProperty); int index; if (propertyNameString.ToLower().Equals("first")) { index = 0; } else if (propertyNameString.ToLower().Equals("last")) { index = arrayValue.ArrValue.Count - 1; } else if (propertyNameString.ToLower().Equals("size")) { return LiquidExpressionResult.Success(NumericValue.Create(arrayValue.ArrValue.Count)); } else { var maybeIndexResult = ValueCaster.Cast<IExpressionConstant, NumericValue>(indexProperty); if (maybeIndexResult.IsError || !maybeIndexResult.SuccessResult.HasValue) { return LiquidExpressionResult.Error("invalid array index: " + propertyNameString); } else { index = maybeIndexResult.SuccessValue<NumericValue>().IntValue; } } if (arrayValue.ArrValue.Count == 0) { //return LiquidExpressionResult.Error("array is empty: " + propertyNameString); return LiquidExpressionResult.Success(new None<IExpressionConstant>()); // not an error in Ruby liquid. } var result = arrayValue.ValueAt(index); return LiquidExpressionResult.Success(result); }