예제 #1
0
        public void Generate_EmptyClass_ReturnsNoTestClasses()
        {
            var sourceCode = File.ReadAllText(EMPTY_CLASS_PATH);

            var tests = generator.Generate(sourceCode);

            Assert.AreEqual(0, tests.Count);
        }
예제 #2
0
        static void Main(string[] args)
        {
            int readingTasksCount = Convert.ToInt32(args[0]);
            int writingTasksCount = Convert.ToInt32(args[1]);

            TestGeneratorConfig config = new TestGeneratorConfig(readingTasksCount, writingTasksCount);

            string        outputDirectory = args[2];
            List <string> paths           = new List <string>();

            for (int i = 3; i < args.Length; i++)
            {
                paths.Add(args[i]);
            }

            CodeReader reader = new CodeReader();
            CodeWriter writer = new CodeWriter(outputDirectory);

            TestsGenerator generator = new TestsGenerator(config);

            generator.Generate(reader, writer, paths).Wait();

            Console.WriteLine("Done!");

            Console.ReadLine();
        }
        public Task Generate(int maxDegreeOfParallelism, string resultPath, params string[] files)
        {
            var execOptions = new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            };

            var openFile      = new TransformBlock <string, string>(async path => await File.ReadAllTextAsync(path), execOptions);
            var generateTests = new TransformManyBlock <string, TestUnit>(file => TestsGenerator.Generate(file), execOptions);
            var writeFile     = new ActionBlock <TestUnit>(
                async testUnit => await File.WriteAllTextAsync(
                    Path.Combine(resultPath, testUnit.Name) + ".cs", testUnit.Test),
                execOptions);


            var linkOptions = new DataflowLinkOptions {
                PropagateCompletion = true
            };

            openFile.LinkTo(generateTests, linkOptions);
            generateTests.LinkTo(writeFile, linkOptions);


            foreach (string path in files)
            {
                openFile.Post(path);
            }
            openFile.Complete();

            return(writeFile.Completion);
        }
예제 #4
0
        private static IEnumerable <string> GetGeneratedFilesPaths(string targetDirectoryPath, TestsGeneratorRestrictions restrictions, IEnumerable <string> filePaths)
        {
            List <ConsumerResult <string> > generatedFilePaths = new List <ConsumerResult <string> >();

            try
            {
                var fileSourceCodeProvider = new FileSourceCodeProvider(filePaths);
                var fileConsumer           = new FileConsumer(targetDirectoryPath);

                var testsGenerator = new TestsGenerator(restrictions);
                generatedFilePaths = testsGenerator.Generate(fileSourceCodeProvider, fileConsumer).ToList();
            }
            catch (AggregateException aggregateException)
            {
                Console.WriteLine(ErrorPromt);
                foreach (Exception exception in aggregateException.InnerExceptions)
                {
                    Console.WriteLine(exception.Message);
                }
            }
            catch (ArgumentOutOfRangeException argOutOfRangeException)
            {
                Console.WriteLine(ErrorPromt);
                Console.WriteLine(argOutOfRangeException.Message);
            }

            return(generatedFilePaths.Select(x => x.Result).Cast <string>());
        }
        public void SetUp()
        {
            _readingTasksCount = 3;
            _writingTasksCount = 3;
            _config            = new TestGeneratorConfig(_readingTasksCount, _writingTasksCount);

            _outputDirectory = "./";

            _paths = new List <string>();
            _paths.Add("./AnotherClass.csSource");
            _paths.Add("./SomeClass.csSource");

            _reader = new CodeReader();
            _writer = new CodeWriter(_outputDirectory);

            _generator = new TestsGenerator(_config);
            _generator.Generate(_reader, _writer, _paths).Wait();

            _programmText = File.ReadAllText("./SomeClassTests.cs");
            SyntaxTree syntaxTree;

            syntaxTree = CSharpSyntaxTree.ParseText(_programmText);

            _compilationUnitSyntax = syntaxTree.GetCompilationUnitRoot();
        }
        public void Init()
        {
            outputDirectory = Path.Combine("..", "..", "TestDirectoryOutput");
            inputDirectory  = Path.Combine("..", "..", "TestDirectoryInput");

            writer    = new TestWriter(outputDirectory);
            reader    = new TestReader();
            generator = new TestsGenerator(1, 1, 1, writer, reader);
            generator.Generate(Directory.GetFiles(inputDirectory).ToList()).Wait();

            generatedUnit = ParseCompilationUnit(File.ReadAllText(Path.Combine(outputDirectory, "TestClasses.cs")));
        }
예제 #7
0
        static void Main(string[] args)
        {
            string        sourceDirectory;
            string        targetDirectory;
            int           countOfReadThreads;
            int           countOfWriteThreads;
            int           countOfProcessThreads;
            List <string> inputFiles = new List <string>();

            //Entering source folder path
            Console.WriteLine("Enter the source folder path:");
            sourceDirectory = Console.ReadLine();

            //Entering destination folder path
            Console.WriteLine("Enter the target folder path:");
            targetDirectory = Console.ReadLine();

            //Entering conveyor parameteres
            Console.WriteLine("Enter the count of read threads:");
            countOfReadThreads = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the count of process threads:");
            countOfProcessThreads = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the count of write threads:");
            countOfWriteThreads = Convert.ToInt32(Console.ReadLine());

            try
            {
                string[] fileEntries = Directory.GetFiles(sourceDirectory);
                foreach (string fileName in fileEntries)
                {
                    inputFiles.Add(fileName);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("Directory not found");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }


            var config = new Config(countOfReadThreads, countOfProcessThreads, countOfWriteThreads);

            var generator = new TestsGenerator(config);

            generator.Generate(inputFiles, targetDirectory).Wait();

            Console.WriteLine("Generation ended");
            Console.ReadKey();
        }
예제 #8
0
        public void EmptyClassTest()
        {
            string testStr = @" namespace CustomNamespace
                                {
                                    public class Custom
                                    {

                                    }

                                }";

            TestUnit[] tests = TestsGenerator.Generate(testStr);
            Assert.AreEqual(0, tests.Count());
        }
예제 #9
0
        public void GenerateTest()
        {
            int prevCountofFiles = Directory.GetFiles(resultPath).Length;

            testsGenerator.Generate(asyncWriter).Wait();
            int currentCountOfFiles = Directory.GetFiles(resultPath).Length;
            int expectedCount       = prevCountofFiles + files.Count;

            Assert.AreEqual(expectedCount, currentCountOfFiles);
            foreach (string filePath in files)
            {
                string pathToResFile = resultPath + "\\" + Path.GetFileNameWithoutExtension(filePath) + "Tests.cs";
                File.Delete(pathToResFile);
            }
        }
예제 #10
0
        public void SetUp()
        {
            _fullPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var fileSourceCodeProvider = new FileSourceCodeProvider(new List <string> {
                _fullPath + @"\TracerUse.csnotcompilable"
            });
            var fileConsumer = new FileConsumer(_fullPath + @"\GeneratedTests\");

            _testsGenerator = new TestsGenerator();
            _testsGenerator.Generate(fileSourceCodeProvider, fileConsumer);

            string     generatedTestText = File.ReadAllText(_fullPath + @"\GeneratedTests\TracerUseTests.cs");
            SyntaxTree syntaxTree        = CSharpSyntaxTree.ParseText(generatedTestText);

            _testCompilationUnit = syntaxTree.GetCompilationUnitRoot();
        }
예제 #11
0
        static void Main(string[] args)
        {
            int           readingLimit    = 5;
            int           writingLimit    = 5;
            int           processingLimit = 10;
            string        workPath        = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\TestsFiles\\";
            List <string> pathes          = new List <string>
            {
                workPath + "MyClass.cs",
                workPath + "Tracer.cs"
            };

            TestGeneratorConfig config    = new TestGeneratorConfig(readingLimit, processingLimit, writingLimit);
            TestsGenerator      generator = new TestsGenerator(config);

            generator.Generate(pathes, workPath + "GeneratedTests").Wait();
        }
예제 #12
0
        static void Main(string[] args)
        {
            int           readingLimit    = 3;
            int           writingLimit    = 3;
            int           processingLimit = 8;
            string        workPath        = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\TestsFilesForTesting\\";
            List <string> pathes          = new List <string>();

            pathes.Add(workPath + "BaseGenerator.cs");
            pathes.Add(workPath + "TestsGenerator.cs");

            TestsGeneratorConfig config    = new TestsGeneratorConfig(readingLimit, processingLimit, writingLimit);
            TestsGenerator       generator = new TestsGenerator(config);

            generator.Generate(pathes, workPath + "GeneratedTests").Wait();
            Console.WriteLine("Complete");
        }
예제 #13
0
        public void SetUp()
        {
            int                  readingLimit = 3, writingLimit = 3, processingLimit = 8;
            string               sourceCode;
            string               workPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "\\TestsFilesForTesting\\";
            SyntaxTree           codeTree;
            List <string>        pathes = new List <string>();
            TestsGeneratorConfig config;
            TestsGenerator       generator;

            pathes.Add(workPath + "TestsGenerator.cs");
            config    = new TestsGeneratorConfig(readingLimit, processingLimit, writingLimit);
            generator = new TestsGenerator(config);
            generator.Generate(pathes, workPath + "GeneratedTests").Wait();

            sourceCode = File.ReadAllText(workPath + "GeneratedTests\\TestsGeneratorTest.dat");
            codeTree   = CSharpSyntaxTree.ParseText(sourceCode);
            _root      = codeTree.GetCompilationUnitRoot();
        }
예제 #14
0
        private static void GenerateTestsAndWriteToConsole(TestsGeneratorRestrictions restrictions, IEnumerable <string> filePaths)
        {
            try
            {
                var fileSourceCodeProvider = new FileSourceCodeProvider(filePaths);
                var consoleConsumer        = new ConsoleConsumer();

                var testsGenerator = new TestsGenerator(restrictions);
                testsGenerator.Generate(fileSourceCodeProvider, consoleConsumer);
            }
            catch (AggregateException aggregateException)
            {
                Console.WriteLine(ErrorPromt);
                foreach (Exception exception in aggregateException.InnerExceptions)
                {
                    Console.WriteLine(exception.Message);
                }
            }
            catch (ArgumentOutOfRangeException argOutOfRangeException)
            {
                Console.WriteLine(ErrorPromt);
                Console.WriteLine(argOutOfRangeException.Message);
            }
        }
예제 #15
0
        public void ClassWithDependencyTest()
        {
            string testStr = @"
                                using System;
                                using System.Collections.Generic;
                                using System.Text;

                                namespace CustomNamespace1
                                {

                                    public interface IFoo
                                    {

                                    }

                                    public class Custom1
                                    {
                                        public void Method1()
                                        {

                                        }

                                        public int Method2(int arg)
                                        {
                                            return 42;
                                        }

                                        public Custom1(int a, string b, IFoo c)
                                        {

                                        }
                                    }
                                }";

            string expected = @"using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Moq;
using CustomNamespace1;

namespace CustomNamespace1.Tests
{
    public class Custom1Tests
    {
        private Custom1 _Custom1UnderTest;
        private Mock<IFoo> _c_dependency;
        [SetUp]
        public void SetUp()
        {
            int a = default;
            string b = default;
            _c_dependency = new Mock<IFoo>();
            _Custom1UnderTest = new Custom1(a, b, _c_dependency.Object);
        }

        [Test]
        public void Method1Test()
        {
            _Custom1UnderTest.Method1();
            Assert.Fail(" + "\"Autogenerated\"" + @");
        }

        [Test]
        public void Method2Test()
        {
            int arg = default;
            int actual = _Custom1UnderTest.Method2(arg);
            int expected = default;
            Assert.That(actual, Is.EqualTo(expected));
            Assert.Fail(" + "\"Autogenerated\"" + @");
        }
    }
}";

            TestUnit[] tests = TestsGenerator.Generate(testStr);


            Assert.AreEqual(1, tests.Count());
            Assert.AreEqual(expected, tests[0].Test);
        }
예제 #16
0
        public void ClassWithoutDependecyTest()
        {
            string testStr = @" 
                                using System;
                                using System.Collections.Generic;
                                using System.Text;
                                namespace CustomNamespace2
                                {
                                    public class Custom2
                                    {
                                        public string Method1()
                                        {
                                            return null;
                                        }

                                        public void Method2(int arg, char b)
                                        {

                                        }
                                    }
                                }";

            string expected = @"using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Moq;
using CustomNamespace2;

namespace CustomNamespace2.Tests
{
    public class Custom2Tests
    {
        private Custom2 _Custom2UnderTest;
        [SetUp]
        public void SetUp()
        {
            _Custom2UnderTest = new Custom2();
        }

        [Test]
        public void Method1Test()
        {
            string actual = _Custom2UnderTest.Method1();
            string expected = default;
            Assert.That(actual, Is.EqualTo(expected));
            Assert.Fail(" + "\"Autogenerated\"" + @");
        }

        [Test]
        public void Method2Test()
        {
            int arg = default;
            char b = default;
            _Custom2UnderTest.Method2(arg, b);
            Assert.Fail(" + "\"Autogenerated\"" + @");
        }
    }
}";

            TestUnit[] tests = TestsGenerator.Generate(testStr);

            Assert.AreEqual(1, tests.Count());
            Assert.AreEqual(expected, tests[0].Test);
        }