public void GetTemplateTest()
        {
            testClassTemplate = templateGenerator.GetTemplate(sourceCode);
            Assert.IsNotNull(testClassTemplate);
            generatedRoot = CSharpSyntaxTree.ParseText(testClassTemplate.InnerText).GetRoot();
            List <ClassDeclarationSyntax> generatedClasses = new List <ClassDeclarationSyntax>(generatedRoot.DescendantNodes().OfType <ClassDeclarationSyntax>());
            List <ClassDeclarationSyntax> sourceClasses    = new List <ClassDeclarationSyntax>(sourceRoot.DescendantNodes().OfType <ClassDeclarationSyntax>());

            Assert.AreEqual(sourceClasses.Count, generatedClasses.Count);

            for (int i = 0; i < sourceClasses.Count; i++)
            {
                ClassDeclarationSyntax sourceClass    = sourceClasses[i];
                ClassDeclarationSyntax generatedClass = generatedClasses[i];

                List <MethodDeclarationSyntax> sourceMethods = new List <MethodDeclarationSyntax>(
                    sourceClass.DescendantNodes().OfType <MethodDeclarationSyntax>()
                    .Where(method => method.Modifiers.
                           Any(modifer => modifer.ToString() == "public")));
                List <MethodDeclarationSyntax> generatedMethods = new List <MethodDeclarationSyntax>(
                    generatedClass.DescendantNodes().OfType <MethodDeclarationSyntax>()
                    .Where(method => method.Modifiers.
                           Any(modifer => modifer.ToString() == "public")));
                Assert.AreEqual(sourceMethods.Count, generatedMethods.Count);

                for (int j = 0; j < sourceMethods.Count; j++)
                {
                    MethodDeclarationSyntax sourceMethod    = sourceMethods[j];
                    MethodDeclarationSyntax generatedMethod = generatedMethods[j];
                    Assert.IsTrue(generatedMethod.Identifier.ToString().Contains(sourceMethod.Identifier.ToString()));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new assembly, using the specified info.
        /// </summary>
        /// <param name="info">The info object.</param>
        public void CreateAssembly(TestAssemblyInfo info)
        {
            _errors.Clear();
            _warnings.Clear();

            // create output stream for test source
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream)
            {
                AutoFlush = true
            };
            var compilation = new TestCompilation(_sourceFiles, ReferencePaths);

            // write test classes
            foreach (var model in compilation.TemplateModels)
            {
                var tmpl = new TestClassTemplate {
                    Model = model
                };
                writer.WriteLine(tmpl.TransformText());
            }

            // write initializer class
            var initTmpl = new InitClassTemplate()
            {
                ProjectDir = info.ProjectDir
            };

            writer.WriteLine(initTmpl.TransformText());

            // create codeDom objects
            var codeProvider = new CSharpCodeProvider();
            var options      = new CompilerParameters
            {
                GenerateExecutable      = false,
                GenerateInMemory        = false,
                OutputAssembly          = info.OutputPath,
                IncludeDebugInformation = true
            };

            options.ReferencedAssemblies.AddRange(ReferencePaths);

            stream.Seek(0, SeekOrigin.Begin);
            var reader  = new StreamReader(stream);
            var fullSrc = reader.ReadToEnd();
            var result  = codeProvider.CompileAssemblyFromSource(options, new[] { fullSrc });

            // log errors and warnings to IDE/console
            foreach (var item in result.Errors.OfType <CompilerError>())
            {
                if (item.IsWarning)
                {
                    _warnings.Add(item);
                }
                else
                {
                    _errors.Add(item);
                }
            }
        }
 public void SetUp()
 {
     pathToFile      = Environment.CurrentDirectory + "\\ResultTests\\TestForWriterAndReader.cs";
     pathToDirectory = Environment.CurrentDirectory + "\\ResultTests";
     asyncReader     = new AsyncFileReader();
     asyncWriter     = new AsyncFileWriter(pathToDirectory);
     fileName        = "TestForWriterAndReader.cs";
     testString      = "Write and read test!";
     template        = new TestClassTemplate(fileName, testString);
 }
Exemplo n.º 4
0
        public IContentFile BuildFile()
        {
            var template = new TestClassTemplate {
                CodeClass = Context.CodeClass,
                TestClass = Context.TestClass,

                Symbol = Context.Symbol,
            };

            template.UsingNamespaces
            .Add(Context.CodeClass.Namespace.Levels.First())
            .Except(Context.TestClass.Namespace);

            return(new CSharpFile(Context.TestClass, template.TransformText()));
        }
            public void ShouldCallPropertySetterForAllExpectedProperties()
            {
                // arrange
                var propertySetterMock = new Mock <IPropertySetter>();
                var sut = new DynamicBuilder <TestClass>(
                    propertySetter: propertySetterMock.Object);

                int expectedValue      = 1;
                var template           = new TestClassTemplate(expectedValue);
                var expectedProperties = new[]
                {
                    nameof(TestClass.PublicGetPublicSet),
                    nameof(TestClass.PublicGetInternalSet),
                    nameof(TestClass.PublicGetProtectedSet),
                    nameof(TestClass.PublicGetPrivateSet),
                    nameof(TestClass.PublicReadOnlyAutoProperty),
                    nameof(TestClass.PublicGetOfPrivateField),
                    nameof(TestClass.InternalGetInternalSet),
                    nameof(TestClass.InternalGetPrivateSet),
                    nameof(TestClass.ProtectedInternalGetProtectedInternalSet),
                    nameof(TestClass.ProtectedInternalGetInternalSet),
                    nameof(TestClass.ProtectedInternalGetPrivateSet)
                }
                .Select(propName => typeof(TestClass).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));

                // act
                sut.OverwriteWithTemplate(template);
                sut.Build();

                // assert
                foreach (var expectedProperty in expectedProperties)
                {
                    propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), expectedProperty, expectedValue), Times.Once());
                }
                propertySetterMock.Verify(e => e.SetProperty(It.IsAny <object>(), It.IsAny <PropertyInfo>(), It.IsAny <object>()), Times.Exactly(expectedProperties.Count()));
            }