예제 #1
0
        public void TestMethodNoClass()
        {
            TestGenerator testGenerator = new TestGenerator();
            string        code          = new StreamReader("../../../../TestLibrary/ClassEmpty.cs").ReadToEnd();

            List <ResultOfTestGeneration> listResult = testGenerator.GenerateTests(code);

            Assert.AreEqual(1, listResult.Count);
        }
예제 #2
0
        public void TestMethodUsing()
        {
            TestGenerator testGenerator = new TestGenerator();
            string        code          = new StreamReader("../../../../TestLibrary/Class2.cs").ReadToEnd();

            List <ResultOfTestGeneration> listResult  = testGenerator.GenerateTests(code);
            ResultOfTestGeneration        classResult = listResult[0];
            CompilationUnitSyntax         root        = CSharpSyntaxTree.ParseText(classResult.Code).GetCompilationUnitRoot();

            List <UsingDirectiveSyntax> usings = GetUsings(root.DescendantNodes());
            List <string> usingNames           = GetUsingNames(usings);

            Assert.IsTrue(usingNames.Contains("Microsoft.VisualStudio.TestTools.UnitTesting"));
        }
예제 #3
0
        public void TestMethodClass2Methods()
        {
            TestGenerator testGenerator = new TestGenerator();
            string        code          = new StreamReader("../../../../TestLibrary/Class2.cs").ReadToEnd();

            List <ResultOfTestGeneration> listResult  = testGenerator.GenerateTests(code);
            ResultOfTestGeneration        classResult = listResult[0];
            CompilationUnitSyntax         root        = CSharpSyntaxTree.ParseText(classResult.Code).GetCompilationUnitRoot();

            List <MethodDeclarationSyntax> publicMethods = GetPublicMethods(root.DescendantNodes());

            Assert.AreEqual(2, publicMethods.Count);
            List <string> methodNames = GetMethodNames(publicMethods);

            Assert.IsTrue(methodNames.Contains("FirstMethodTest"));
            Assert.IsTrue(methodNames.Contains("SecondMethodTest"));
        }
예제 #4
0
        public void TestMethodClass2NameAttributeFile()
        {
            TestGenerator testGenerator = new TestGenerator();
            string        code          = new StreamReader("../../../../TestLibrary/Class2.cs").ReadToEnd();

            List <ResultOfTestGeneration> listResult = testGenerator.GenerateTests(code);

            var syntaxTree = CSharpSyntaxTree.ParseText(listResult[0].Code);
            var classes    = syntaxTree.GetRoot().DescendantNodes().OfType <ClassDeclarationSyntax>().ToList();

            Assert.AreEqual(classes.Count, 1);

            foreach (var classDeclaration in classes)
            {
                Assert.AreEqual("Class2UnitTest", classDeclaration.Identifier.Text);
                List <string> methodAttributes = classDeclaration.DescendantNodes().OfType <AttributeSyntax>().
                                                 Select(attribute => attribute.Name.ToString()).ToList();
                Assert.IsTrue(methodAttributes.Contains("TestMethod"));
            }

            Assert.AreEqual(listResult[0].Name, "Class2UnitTest");
        }
예제 #5
0
        public void TestMethodAssertAndAttributes()
        {
            TestGenerator testGenerator = new TestGenerator();
            string        code          = new StreamReader("../../../../TestLibrary/Class3.cs").ReadToEnd();

            List <ResultOfTestGeneration> listResult  = testGenerator.GenerateTests(code);
            ResultOfTestGeneration        classResult = listResult[0];
            CompilationUnitSyntax         root        = CSharpSyntaxTree.ParseText(classResult.Code).GetCompilationUnitRoot();

            List <MethodDeclarationSyntax> publicMethods = GetPublicMethods(root.DescendantNodes());

            Assert.AreEqual(3, publicMethods.Count);
            List <string> methodNames = GetMethodNames(publicMethods);

            foreach (MethodDeclarationSyntax method in publicMethods)
            {
                List <string> identifireNames = method.DescendantNodes().OfType <IdentifierNameSyntax>().
                                                Select(identifire => identifire.Identifier.ToString()).ToList();
                Assert.IsTrue(identifireNames.Contains("Assert"));
                List <string> methodAttributes = method.DescendantNodes().OfType <AttributeSyntax>().
                                                 Select(attribute => attribute.Name.ToString()).ToList();
                Assert.IsTrue(methodAttributes.Contains("TestMethod"));
            }
        }