Пример #1
0
        public void CanGenerateNestedClass()
        {
            TypeDefinition type = _testModule.GetType(
                $"{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.Outer)}/{nameof(CSharpCodeGenrationTestsNamespace.Outer.Nested)}");
            MethodDefinition ctor = type.GetConstructors().Single();

            var generator     = new AutoDIFody::AutoDI.Fody.CodeGen.CSharpCodeGenerator(_outputDirectory);
            var ctorGenerator = generator.Method(ctor);

            ctorGenerator.Append("");

            generator.Save();

            string result = File.ReadAllText(Directory.EnumerateFiles(_outputDirectory).Single());

            string expected =
                "namespace " + nameof(CSharpCodeGenrationTestsNamespace) + Environment.NewLine +
                "{" + Environment.NewLine +
                "    public class " + nameof(CSharpCodeGenrationTestsNamespace.Outer) + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        public class " + nameof(CSharpCodeGenrationTestsNamespace.Outer.Nested) + Environment.NewLine +
                "        {" + Environment.NewLine +
                "            //Generated by AutoDI" + Environment.NewLine +
                $"            public {nameof(CSharpCodeGenrationTestsNamespace.Outer.Nested)}([AutoDI.DependencyAttribute]{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.IService)} foo = null)" + Environment.NewLine +
                "            {" + Environment.NewLine +
                "            }" + Environment.NewLine +
                "        }" + Environment.NewLine +
                "    }" + Environment.NewLine +
                "}" + Environment.NewLine;

            Assert.AreEqual(expected, result);
        }
Пример #2
0
        public void NestedClassGeneratesExpectedSequencePoints()
        {
            TypeDefinition type = _testModule.GetType(
                $"{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.Outer)}/{nameof(CSharpCodeGenrationTestsNamespace.Outer.Nested)}");
            MethodDefinition ctor = type.GetConstructors().Single();

            var generator     = new AutoDIFody::AutoDI.Fody.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);
        }
Пример #3
0
        public void ConstructorWithSingleParameterGeneratesExpectedSequencePoints()
        {
            TypeDefinition type = _testModule.GetType(
                $"{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.Class1)}");
            MethodDefinition ctor = type.GetConstructors().Single();

            var generator     = new AutoDIFody::AutoDI.Fody.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);
        }
Пример #4
0
        public void CanGenerateClassWithMethodInjection()
        {
            TypeDefinition type = _testModule.GetType(
                $"{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.Class4)}");
            MethodDefinition method = type.GetMethods().Single();

            var generator     = new AutoDIFody::AutoDI.Fody.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(CSharpCodeGenrationTestsNamespace) + Environment.NewLine +
                "{" + Environment.NewLine +
                "    public class " + nameof(CSharpCodeGenrationTestsNamespace.Class4) + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        //Generated by AutoDI" + Environment.NewLine +
                $"        public System.Int32 {nameof(CSharpCodeGenrationTestsNamespace.Class4.DoStuff)}([AutoDI.DependencyAttribute]{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.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);
        }
Пример #5
0
        public void CanGenerateClassWithPropertyInjection()
        {
            TypeDefinition type = _testModule.GetType(
                $"{nameof(CSharpCodeGenrationTestsNamespace)}.{nameof(CSharpCodeGenrationTestsNamespace.Class3)}");
            MethodDefinition ctor = type.GetConstructors().Single();

            var generator     = new AutoDIFody::AutoDI.Fody.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(CSharpCodeGenrationTestsNamespace) + Environment.NewLine +
                "{" + Environment.NewLine +
                "    public class " + nameof(CSharpCodeGenrationTestsNamespace.Class3) + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        //Generated by AutoDI" + Environment.NewLine +
                $"        public {nameof(CSharpCodeGenrationTestsNamespace.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);
        }