public string GenerateTest(SyntaxTree tree, string name) { IndentingWriter = IndentingTextWriter.Get(new StringWriter()); sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("using LanguageModel.Tests.TestGeneration;"); sb.AppendLine("using LanguageService;"); sb.AppendLine("using Xunit;"); sb.AppendLine("namespace LanguageModel.Tests.GeneratedTestFiles"); sb.AppendLine("{"); sb.AppendLine(string.Format(" class {0}", name)); sb.AppendLine(" {"); sb.AppendLine(" [Fact]"); sb.AppendLine(" public void Test(Tester t)"); sb.AppendLine(" {"); using (IndentingWriter.Indent()) { using (IndentingWriter.Indent()) { using (IndentingWriter.Indent()) { GenerateTestStructure(tree.Root); } } } sb.Append(IndentingWriter.ToString()); sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine("}"); return(sb.ToString()); }
private void GenerateTestStructure(SyntaxNodeOrToken syntaxNodeOrToken) { if (syntaxNodeOrToken == null) { return; } //TODO remove is-check once Immutable graph object bug is fixed. if (syntaxNodeOrToken is SyntaxNode) { IndentingWriter.WriteLine("t.N(SyntaxKind." + ((SyntaxNode)syntaxNodeOrToken).Kind + ");"); } else { IndentingWriter.WriteLine("t.N(SyntaxKind." + ((Token)syntaxNodeOrToken).Kind + ");"); } if (!syntaxNodeOrToken.IsLeafNode) { IndentingWriter.WriteLine("{"); foreach (var node in ((SyntaxNode)syntaxNodeOrToken).Children) { using (IndentingWriter.Indent()) { GenerateTestStructure(node); } } IndentingWriter.WriteLine("}"); } }
public static void DisassembleMethod(MethodDefinition method, IndentingWriter writer) { writer.WriteLine(GenerateHeaderLine(method)); writer.WriteLine("{"); using (writer.Indent()) { var first = true; writer.WriteLine($"// Code Size {method.Body.CodeSize,8} (0x{method.Body.CodeSize:X2})"); writer.WriteLine($".maxstack {method.Body.MaxStackSize}"); foreach (var variable in method.Body.Variables) { var line = ""; if (first) { first = false; line = ".locals init ("; } else { line = " "; } line += $"{GetTypeName(variable.VariableType)} V_{variable.Index}"; writer.WriteLine(line); } foreach (var instruction in method.Body.Instructions) { writer.WriteLine(instruction); } } writer.WriteLine("}"); }
private static async Task <int> Execute(string assemblyPath, string typeName, string memberName) { var console = new IndentingWriter(Console.Out); var disassembler = new DisassemblerSession(); var disassembly = await disassembler.LoadAsync(assemblyPath); var type = disassembly.FindType(typeName); if (type == null) { return(Error($"could not find type: {typeName}")); } var member = type.Members.FirstOrDefault(t => t.Name.Equals(memberName)); if (member == null) { return(Error($"could not find member: {memberName} in type {typeName}")); } if (member.MemberType == MemberType.Method) { var method = (MethodDefinition)member.Definition; CILDisassembler.DisassembleMethod(method, console); } else { return(Error($"Member type not supported: {member.MemberType}")); } return(0); }
public void TestWriteMethods() { //WriteValue TextWriter writer = new StringWriter(); SimpleElement se = new SimpleElement("root", "some\ntext"); se.WriteValue(writer, true); Assert.AreEqual(se.GetString(), writer.ToString()); writer = new StringWriter(); IndentingWriter indentedWriter = new IndentingWriter(writer, 2); se.WriteValue(indentedWriter, true); Assert.AreEqual(se.GetString(), writer.ToString()); //WriteXml se = new SimpleElement("root"); writer = new StringWriter(); se.WriteXml(writer, true); Assert.AreEqual("<root/>", writer.ToString()); se.AddAttribute("attr1").SetString("value1"); writer = new StringWriter(); se.WriteXml(writer, true); Assert.AreEqual("<root attr1='value1'/>", writer.ToString()); se.Comment = "comment text"; se.SetString("content"); writer = new StringWriter(); se.WriteXml(writer, true); Assert.AreEqual("<root attr1='value1'><!--" + writer.NewLine + "comment text" + writer.NewLine + "-->content</root>", writer.ToString()); writer = new StringWriter(); se.WriteXml(writer, false); Assert.AreEqual("<root attr1='value1'><!--comment text-->content</root>", writer.ToString()); se.AddElement("child").SetString("child content"); writer = new StringWriter(); se.WriteXml(writer, true); string result = "<root attr1='value1'>" + writer.NewLine + " <!--" + writer.NewLine + " comment text" + writer.NewLine + " -->" + writer.NewLine + "content" + writer.NewLine + " <child>child content</child>" + writer.NewLine + "</root>"; Assert.AreEqual(result, writer.ToString()); Assert.AreEqual(result, se.ToString()); writer = new StringWriter(); se.WriteXml(writer, false); result = "<root attr1='value1'><!--comment text-->content<child>child content</child></root>"; Assert.AreEqual(result, writer.ToString()); }