public void ChainingElseIfNestsProperly() { var condition1 = new ConditionalChunk { Type = ConditionalType.If, Condition = "x == 1" }; condition1.Body.Add(new SendLiteralChunk { Text = "a" }); var condition2 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 2" }; condition2.Body.Add(new SendLiteralChunk { Text = "b" }); var condition3 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 3" }; condition3.Body.Add(new SendLiteralChunk { Text = "c" }); var condition4 = new ConditionalChunk { Type = ConditionalType.Else }; condition4.Body.Add(new SendLiteralChunk { Text = "d" }); var loop = new ForEachChunk { Code = "x in numbers" }; loop.Body.Add(condition1); loop.Body.Add(condition2); loop.Body.Add(condition3); loop.Body.Add(condition4); var chunks = Chunks( loop); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(new StubViewData { { "numbers", new[] { 0, 1, 2, 3, 4, 7, 2, -4 } } }); Assert.AreEqual("dabcddbd", contents); }
protected override void Visit(ForEachChunk chunk) { var forEach = new ForEachInspector(chunk.Code); if (!forEach.Recognized) { _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; _variables.PushScope(); Accept(chunk.Body); _source.WriteLine("pass"); _variables.PopScope(); _source.Indent--; return; } _variables.PushScope(); var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(forEach.VariableName + "Index"); var autoCount = detect.Add(forEach.VariableName + "Count"); var autoIsFirst = detect.Add(forEach.VariableName + "IsFirst"); var autoIsLast = detect.Add(forEach.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } if (autoIndex.Detected) { _variables.Declare(forEach.VariableName + "Index"); _source.Write(forEach.VariableName).WriteLine("Index=0"); } if (autoIsFirst.Detected) { _variables.Declare(forEach.VariableName + "IsFirst"); _source.Write(forEach.VariableName).WriteLine("IsFirst=True"); } if (autoCount.Detected) { _variables.Declare(forEach.VariableName + "Count"); _source .Write(forEach.VariableName).Write("Count=len([0 for ") .Write(chunk.Code).WriteLine("])"); } _variables.Declare(forEach.VariableName); _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; if (autoIsLast.Detected) { _variables.Declare(forEach.VariableName + "IsLast"); _source .Write(forEach.VariableName).Write("IsLast=(") .Write(forEach.VariableName).Write("Index==") .Write(forEach.VariableName).WriteLine("Count - 1)"); } Accept(chunk.Body); if (autoIndex.Detected) { _source .Write(forEach.VariableName).Write("Index=") .Write(forEach.VariableName).WriteLine("Index+1"); } if (autoIsFirst.Detected) { _source.Write(forEach.VariableName).WriteLine("IsFirst=False"); } _source.WriteLine("pass"); _source.Indent--; _variables.PopScope(); }
public void ForEachLoopOverArray() { var loop = new ForEachChunk { Code = "number in numbers" }; loop.Body.Add(new SendExpressionChunk { Code = "number" }); var chunks = Chunks( loop); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(new StubViewData { { "numbers", new[] { 1, 2, 3, 4, 5 } } }); Assert.AreEqual("12345", contents); }