public static SUnit TranslateMethodCall(Statement statement) { var expressions = statement.GetExpressions(); // Give an empty SUnit if statement has no expressions. if (expressions.Count() == 0) { return new SUnit(SUnitType.SingleMethodCall, "", "", "", new List<string>(), "void"); } // Build a minimal method context and declaration node required by SWUM. var exp = expressions.First(); string type = exp.ResolveType().ToString(); MethodContext mc = new MethodContext(type); MethodDeclarationNode mdn = new MethodDeclarationNode(exp.ToString(), mc); // Apply the SWUM to our statement var swumRule = SetupBaseVerbRule(); swumRule.InClass(mdn); swumRule.ConstructSwum(mdn); // Build and return SUnit from the SWUM SUnit sunit = new SUnit(); sunit.action = GetAction(mdn); sunit.theme = GetTheme(mdn); sunit.args = GetArgs(mdn); return sunit; }
public void TestTextGenerationForSingleMethodCall() { // addUser( username, password); SUnit sunit = new SUnit(SUnitType.SingleMethodCall, "add", "user", null, new List<string> { "username", "password" }, "void"); var sentence = TextGenerator.GenerateText(sunit); // Allow vaguer assertion and print output Assert.AreEqual("Add user given username, password.", sentence); Console.WriteLine(sentence); }
private static SUnit TranslateAssignment(Statement statement) { // action = "Assign" // define left-hand-side (lhs) // theme = right hand side var fieldRule = SetupFieldRule(); // var equalsSign = statement.GetDescendants<OperatorUse>() // .Where(o => o.Text.Equals("=")).First(); // var lhs = equalsSign.GetSiblingsBeforeSelf<VariableUse>().First(); var assignExpression = (VariableDeclaration) statement.GetExpressions().First(); var lhs = assignExpression.Name; var lhsFieldContext = new FieldContext(assignExpression.VariableType.ToString(), false, ""); var lhsDecNode = new FieldDeclarationNode(lhs.ToString(), lhsFieldContext); fieldRule.InClass(lhsDecNode); fieldRule.ConstructSwum(lhsDecNode); var rhsString = ""; var rhsAction = ""; var rhsTheme = ""; Expression rhs = new Expression(); if (assignExpression.Initializer != null) { rhs = assignExpression.Initializer; } if (rhs is VariableUse) { var rhsFieldContext = new FieldContext(rhs.ResolveType().First().ToString(), false, ""); var rhsDecNode = new FieldDeclarationNode(rhs.ToString(), lhsFieldContext); fieldRule.InClass(rhsDecNode); fieldRule.ConstructSwum(rhsDecNode); rhsAction = "Assign"; rhsString = rhsDecNode.ToPlainString(); } else if (rhs is MethodCall) { string type = rhs.ResolveType().ToString(); MethodContext mc = new MethodContext(type); MethodDeclarationNode mdn = new MethodDeclarationNode(rhs.ToString(), mc); var swumRule = SetupBaseVerbRule(); swumRule.InClass(mdn); swumRule.ConstructSwum(mdn); rhsAction = mdn.Action.ToPlainString(); rhsTheme = mdn.Action.ToPlainString(); rhsString = mdn.ToPlainString(); } else { rhsString = rhs.ToString(); } var sunit = new SUnit(); sunit.type = SUnitType.Assignment; sunit.action = rhsString; //sunit.lhs = lhsDecNode.ToPlainString(); sunit.lhs = lhs.ToString(); sunit.theme = rhsString; return sunit; }
public static String GenerateText(Swummary.SUnit sunit) { String sentence = ""; if (sunit.type == SUnitType.SingleMethodCall) { sentence += sunit.action + " " + sunit.theme; if (sunit.args != null && sunit.args.Count() > 0) { sentence += " given " + string.Join(", ", sunit.args); } if (sunit.hasReturnType) { sentence += " and get " + sunit.returnType; } } else if (sunit.type == SUnitType.Assignment) { sentence += sunit.action + " " + sunit.theme; if (sunit.args != null) { sentence += " given "; foreach (String arg in sunit.args) { sentence += " " + arg + ","; } } if (sunit.hasReturnType) { sentence += " and get " + sunit.returnType + "."; } sentence += " Assign to " + sunit.lhs; } else if (sunit.type == SUnitType.Return) { sentence += sunit.action + " " + sunit.theme; } else { sentence += sunit.action + " " + sunit.theme + " "; foreach (String arg in sunit.args) { sentence += " " + arg; } if (sunit.hasReturnType) { sentence += " and get " + sunit.returnType; } } // Begin with uppercase and end with period. sentence = char.ToUpper(sentence[0]) + sentence.Substring(1); sentence += "."; return(sentence); }