public void Goto()
{
CodeNamespace ns = new CodeNamespace("NS");
ns.Imports.Add(new CodeNamespaceImport("System"));
CodeTypeDeclaration class1 = new CodeTypeDeclaration();
class1.Name = "Test";
class1.IsClass = true;
ns.Types.Add(class1);
// create first method to test gotos that jump ahead to a defined label with statement
var cmm = new CodeMemberMethod();
cmm.Name = "FirstMethod";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
CodeParameterDeclarationExpression param = new CodeParameterDeclarationExpression(typeof(int), "i");
cmm.Parameters.Add(param);
CodeConditionStatement condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
new CodeGotoStatement("comehere"));
cmm.Statements.Add(condstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(6)));
cmm.Statements.Add(new CodeLabeledStatement("comehere",
new CodeMethodReturnStatement(new CodePrimitiveExpression(7))));
class1.Members.Add(cmm);
// create second method to test gotos that jump ahead to a defined label without a statement attached to it
cmm = new CodeMemberMethod();
cmm.Name = "SecondMethod";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
param = new CodeParameterDeclarationExpression(typeof(int), "i");
cmm.Parameters.Add(param);
condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
new CodeGotoStatement("comehere"));
cmm.Statements.Add(condstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(6)));
cmm.Statements.Add(new CodeLabeledStatement("comehere"));
cmm.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(7)));
class1.Members.Add(cmm);
// create third method to test gotos that jump to a previously defined label
cmm = new CodeMemberMethod();
cmm.Name = "ThirdMethod";
cmm.ReturnType = new CodeTypeReference(typeof(int));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
param = new CodeParameterDeclarationExpression(typeof(int), "i");
cmm.Parameters.Add(param);
CodeAssignStatement assignmt = new CodeAssignStatement(new CodeVariableReferenceExpression("i"),
new CodeBinaryOperatorExpression
(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.Add,
new CodePrimitiveExpression(5)));
cmm.Statements.Add(new CodeLabeledStatement("label", assignmt));
condstmt = new CodeConditionStatement(new CodeBinaryOperatorExpression(
new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.LessThan, new CodePrimitiveExpression(1)),
new CodeGotoStatement("label"));
cmm.Statements.Add(condstmt);
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("i")));
class1.Members.Add(cmm);
AssertEqual(ns,
@"namespace NS {
using System;
public class Test {
public static int FirstMethod(int i) {
if ((i < 1)) {
goto comehere;
}
return 6;
comehere:
return 7;
}
public static int SecondMethod(int i) {
if ((i < 1)) {
goto comehere;
}
return 6;
comehere:
return 7;
}
public static int ThirdMethod(int i) {
label:
i = (i + 5);
if ((i < 1)) {
goto label;
}
return i;
}
}
}");
}