public void NestedClassGeneratesExpectedSequencePoints() { TypeDefinition type = _testModule.GetType( $"{nameof(CSharpCodeGenerationTestsNamespace)}.{nameof(CSharpCodeGenerationTestsNamespace.Outer)}/{nameof(CSharpCodeGenerationTestsNamespace.Outer.Nested)}"); MethodDefinition ctor = type.GetConstructors().Single(); var generator = new AutoDIBuild::AutoDI.Build.CodeGen.CSharpCodeGenerator(_outputDirectory); var ctorGenerator = generator.Method(ctor); ctorGenerator.Append("//Comment", Instruction.Create(OpCodes.Nop)); generator.Save(); Assert.AreEqual(1, ctor.DebugInformation.SequencePoints.Count); SequencePoint first = ctor.DebugInformation.SequencePoints[0]; Assert.AreEqual(10, first.StartLine); Assert.AreEqual(10, first.EndLine); Assert.AreEqual(17, first.StartColumn); Assert.AreEqual(26, first.EndColumn); }
public void ConstructorWithSingleParameterGeneratesExpectedSequencePoints() { TypeDefinition type = _testModule.GetType( $"{nameof(CSharpCodeGenerationTestsNamespace)}.{nameof(CSharpCodeGenerationTestsNamespace.Class1)}"); MethodDefinition ctor = type.GetConstructors().Single(); var generator = new AutoDIBuild::AutoDI.Build.CodeGen.CSharpCodeGenerator(_outputDirectory); var ctorGenerator = generator.Method(ctor); ctorGenerator.Append("if (foo == null)", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine + "{" + Environment.NewLine); ctorGenerator.Append(" foo = GlobalDI.GetService<IService>();", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); ctorGenerator.Append("}", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); generator.Save(); Assert.AreEqual(3, ctor.DebugInformation.SequencePoints.Count); SequencePoint first = ctor.DebugInformation.SequencePoints[0]; Assert.AreEqual(8, first.StartLine); Assert.AreEqual(8, first.EndLine); Assert.AreEqual(13, first.StartColumn); Assert.AreEqual(29, first.EndColumn); SequencePoint second = ctor.DebugInformation.SequencePoints[1]; Assert.AreEqual(10, second.StartLine); Assert.AreEqual(10, second.EndLine); Assert.AreEqual(17, second.StartColumn); Assert.AreEqual(55, second.EndColumn); SequencePoint third = ctor.DebugInformation.SequencePoints[2]; Assert.AreEqual(11, third.StartLine); Assert.AreEqual(11, third.EndLine); Assert.AreEqual(13, third.StartColumn); Assert.AreEqual(14, third.EndColumn); }
public void CanGenerateClassWithMethodInjection() { TypeDefinition type = _testModule.GetType( $"{nameof(CSharpCodeGenerationTestsNamespace)}.{nameof(CSharpCodeGenerationTestsNamespace.Class4)}"); MethodDefinition method = type.GetMethods().Single(); var generator = new AutoDIBuild::AutoDI.Build.CodeGen.CSharpCodeGenerator(_outputDirectory); var ctorGenerator = generator.Method(method); ctorGenerator.Append("if (foo == null)", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine + "{" + Environment.NewLine); ctorGenerator.Append(" foo = GlobalDI.GetService<IService>();", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); ctorGenerator.Append("}", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); generator.Save(); string result = File.ReadAllText(Directory.EnumerateFiles(_outputDirectory).Single()); string expected = "namespace " + nameof(CSharpCodeGenerationTestsNamespace) + Environment.NewLine + "{" + Environment.NewLine + " public class " + nameof(CSharpCodeGenerationTestsNamespace.Class4) + Environment.NewLine + " {" + Environment.NewLine + " //Generated by AutoDI" + Environment.NewLine + $" public System.Int32 {nameof(CSharpCodeGenerationTestsNamespace.Class4.DoStuff)}([AutoDI.DependencyAttribute]{nameof(CSharpCodeGenerationTestsNamespace)}.{nameof(CSharpCodeGenerationTestsNamespace.IService)} foo = null)" + Environment.NewLine + " {" + Environment.NewLine + " if (foo == null)" + Environment.NewLine + " {" + Environment.NewLine + " foo = GlobalDI.GetService<IService>();" + Environment.NewLine + " }" + Environment.NewLine + " }" + Environment.NewLine + " }" + Environment.NewLine + "}" + Environment.NewLine; Assert.AreEqual(expected, result); }
public void CanGenerateClassWithPropertyInjection() { TypeDefinition type = _testModule.GetType( $"{nameof(CSharpCodeGenerationTestsNamespace)}.{nameof(CSharpCodeGenerationTestsNamespace.Class3)}"); MethodDefinition ctor = type.GetConstructors().Single(); var generator = new AutoDIBuild::AutoDI.Build.CodeGen.CSharpCodeGenerator(_outputDirectory); var ctorGenerator = generator.Method(ctor); ctorGenerator.Append("if (Service == null)", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine + "{" + Environment.NewLine); ctorGenerator.Append(" Service = GlobalDI.GetService<IService>();", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); ctorGenerator.Append("}", Instruction.Create(OpCodes.Nop)); ctorGenerator.Append(Environment.NewLine); generator.Save(); string result = File.ReadAllText(Directory.EnumerateFiles(_outputDirectory).Single()); string expected = "namespace " + nameof(CSharpCodeGenerationTestsNamespace) + Environment.NewLine + "{" + Environment.NewLine + " public class " + nameof(CSharpCodeGenerationTestsNamespace.Class3) + Environment.NewLine + " {" + Environment.NewLine + " //Generated by AutoDI" + Environment.NewLine + $" public {nameof(CSharpCodeGenerationTestsNamespace.Class3)}()" + Environment.NewLine + " {" + Environment.NewLine + " if (Service == null)" + Environment.NewLine + " {" + Environment.NewLine + " Service = GlobalDI.GetService<IService>();" + Environment.NewLine + " }" + Environment.NewLine + " }" + Environment.NewLine + " }" + Environment.NewLine + "}" + Environment.NewLine; Assert.AreEqual(expected, result); }