public void CheckUsings() { TestUnit[] actualUnits = TestsGenerator.GenerateTests(sourceCode); string[] actual1 = CSharpSyntaxTree.ParseText(actualUnits[0].sourceCode). GetRoot(). DescendantNodes().OfType <UsingDirectiveSyntax>(). Select(use => use.Name.ToString()). ToArray(); string[] actual2 = CSharpSyntaxTree.ParseText(actualUnits[1].sourceCode). GetRoot(). DescendantNodes().OfType <UsingDirectiveSyntax>(). Select(use => use.Name.ToString()). ToArray(); string[] expected1 = { "CustomNamespace1", "System", "System.Collections.Generic", "System.Text" }; string[] expected2 = { "CustomNamespace2", "System", "System.Collections.Generic", "System.Text" }; Assert.AreEqual(expected1[0], actual1[0]); Assert.AreEqual(expected1[1], actual1[1]); Assert.AreEqual(expected1[2], actual1[2]); Assert.AreEqual(expected1[3], actual1[3]); Assert.AreEqual(expected2[0], actual2[0]); Assert.AreEqual(expected2[1], actual2[1]); Assert.AreEqual(expected2[2], actual2[2]); Assert.AreEqual(expected2[3], actual2[3]); }
public void CheckFileCount() { var actual = TestsGenerator.GenerateTests(sourceCode).Length; var expected = 2; Assert.That(actual, Is.EqualTo(expected)); }
public Task Generate(List <string> inputFiles, string outputPath, int maxReadText, int maxGenerateTest, int maxWriteText) { var ExecReaxOption = new ExecutionDataflowBlockOptions(); ExecReaxOption.MaxDegreeOfParallelism = maxReadText; var ExecGenerateOption = new ExecutionDataflowBlockOptions(); ExecGenerateOption.MaxDegreeOfParallelism = maxGenerateTest; var ExecWriteOption = new ExecutionDataflowBlockOptions(); ExecWriteOption.MaxDegreeOfParallelism = maxWriteText; var readText = new TransformBlock <string, string>(async textReadPath => await ReadTextAsync(textReadPath), ExecReaxOption); var generateTests = new TransformManyBlock <string, Tests>(textCode => TestsGenerator.GenerateTests(textCode), ExecGenerateOption); var writeText = new ActionBlock <Tests>(async test => await WriteTextAsync(Path.Combine(outputPath, test.FileName), test.TestCode), ExecWriteOption); var linkOptions = new DataflowLinkOptions(); linkOptions.PropagateCompletion = true; readText.LinkTo(generateTests, linkOptions); generateTests.LinkTo(writeText, linkOptions); foreach (string file in inputFiles) { readText.Post(file); } readText.Complete(); return(writeText.Completion); }
public void CheckMethodsGeneration() { TestUnit[] actualUnits = TestsGenerator.GenerateTests(sourceCode); int[] actual1 = CSharpSyntaxTree.ParseText(actualUnits[0].sourceCode). GetRoot(). DescendantNodes().OfType <MethodDeclarationSyntax>(). Select(m => m.Body.Statements.Count). ToArray(); int[] actual2 = CSharpSyntaxTree.ParseText(actualUnits[1].sourceCode). GetRoot(). DescendantNodes().OfType <MethodDeclarationSyntax>(). Select(m => m.Body.Statements.Count). ToArray(); int[] expected1 = { 4, 2, 5 }; int[] expected2 = { 1, 4, 4 }; Assert.AreEqual(expected1[0], actual1[0]); Assert.AreEqual(expected1[1], actual1[1]); Assert.AreEqual(expected1[2], actual1[2]); Assert.AreEqual(expected2[0], actual2[0]); Assert.AreEqual(expected2[1], actual2[1]); Assert.AreEqual(expected2[2], actual2[2]); }
public void TestAbstactClass() { string AbstactClassTest = @"namespace AbsractName { abstract class Person { public string Name { get; set; } public Person(string name) { Name = name; } public void Display() { Console.WriteLine(Name); } } }"; Tests[] tests = TestsGenerator.GenerateTests(AbstactClassTest); Assert.AreEqual(tests.Length, 0); }
public void TestSimleClass() { string SimpleClassTest = @"namespace SimpleNamespace { class SimpleClass { private void PrivateMeth() { } public void VoidMeth(int k, float b) { } public char CharMeth(string s) { return 'F'; } } }"; Tests[] tests = TestsGenerator.GenerateTests(SimpleClassTest); string ExpectredText = @"using NUnit.Framework; using Moq; using SimpleNamespace; namespace SimpleNamespace.Test { [TestFixture] public class SimpleClassTests { private SimpleClass _SimpleClassUnderTest; [SetUp] public void SetUp() { _SimpleClassUnderTest = new SimpleClass(); } [Test] public void VoidMethTest() { int k = default; float b = default; _SimpleClassUnderTest.VoidMeth(k, b); Assert.Fail(" + "\"autogenerated\"" + @"); } [Test] public void CharMethTest() { string s = default; char actual = _SimpleClassUnderTest.CharMeth(s); char expected = default; Assert.That(actual, Is.EqualTo(expected)); Assert.Fail(" + "\"autogenerated\"" + @"); } } }"; Assert.AreEqual(tests.Length, 1); Assert.AreEqual(ExpectredText, tests[0].TestCode); }
public void TestStaticClass() { string StaticClassTest = @"namespace StaticName { static class StaticClass { public static void VoidMeth(bool Bool, int k) { } public static bool BoolMeth(string s) { return true; } } }"; Tests[] tests = TestsGenerator.GenerateTests(StaticClassTest); string ExpectredText = @"using NUnit.Framework; using Moq; using StaticName; namespace StaticName.Test { [TestFixture] public class StaticClassTests { [SetUp] public void SetUp() { } [Test] public void VoidMethTest() { bool Bool = default; int k = default; StaticClass.VoidMeth(Bool, k); Assert.Fail(" + "\"autogenerated\"" + @"); } [Test] public void BoolMethTest() { string s = default; bool actual = StaticClass.BoolMeth(s); bool expected = default; Assert.That(actual, Is.EqualTo(expected)); Assert.Fail(" + "\"autogenerated\"" + @"); } } }"; Assert.AreEqual(tests.Length, 1); Assert.AreEqual(ExpectredText, tests[0].TestCode); }
public void CheckNamespaceGeneration() { var actualUnits = TestsGenerator.GenerateTests(sourceCode); var actual1 = CSharpSyntaxTree.ParseText(actualUnits[0].sourceCode).GetRoot().DescendantNodes() .OfType <NamespaceDeclarationSyntax>().Select(space => space.Name.ToString()).ToArray(); var actual2 = CSharpSyntaxTree.ParseText(actualUnits[1].sourceCode).GetRoot().DescendantNodes() .OfType <NamespaceDeclarationSyntax>().Select(space => space.Name.ToString()).ToArray(); string[] expected1 = { "Custom1UnitTests" }; string[] expected2 = { "Custom2UnitTests" }; Assert.AreEqual(expected1[0], actual1[0]); Assert.AreEqual(expected2[0], actual2[0]); }
public void CheckClassGeneration() { var actualUnits = TestsGenerator.GenerateTests(sourceCode); var actual1 = CSharpSyntaxTree.ParseText(actualUnits[0].sourceCode).GetRoot().DescendantNodes() .OfType <ClassDeclarationSyntax>().First().Identifier.ValueText; var actual2 = CSharpSyntaxTree.ParseText(actualUnits[1].sourceCode).GetRoot().DescendantNodes() .OfType <ClassDeclarationSyntax>().First().Identifier.ValueText; var expected1 = "Custom1Tests"; var expected2 = "Custom2Tests"; Assert.AreEqual(expected1, actual1); Assert.AreEqual(expected2, actual2); }
public void CheckPrivateFieldsGeneration() { var actualUnits = TestsGenerator.GenerateTests(sourceCode); var actual1 = CSharpSyntaxTree.ParseText(actualUnits[0].sourceCode).GetRoot().DescendantNodes() .OfType <FieldDeclarationSyntax>().Select(f => f.Declaration.Variables[0].Identifier.ValueText) .ToArray(); var actual2 = CSharpSyntaxTree.ParseText(actualUnits[1].sourceCode).GetRoot().DescendantNodes() .OfType <FieldDeclarationSyntax>().Select(f => f.Declaration.Variables[0].Identifier.ValueText) .ToArray(); string[] expected1 = { "_Custom1Instance", "_cDependency" }; string[] expected2 = { "_Custom2Instance" }; Assert.AreEqual(expected1[0], actual1[0]); Assert.AreEqual(expected1[1], actual1[1]); Assert.AreEqual(expected2[0], actual2[0]); }
public Task Generate(string destFolder, string[] filenames, int maxPipelineTasks) { var execOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = maxPipelineTasks }; var linkOptions = new DataflowLinkOptions { PropagateCompletion = true }; Directory.CreateDirectory(destFolder); var loadFile = new TransformBlock <string, string> ( async path => await File.ReadAllTextAsync(path), execOptions ); var generateTests = new TransformManyBlock <string, TestUnit> ( async sourceCode => await Task.Run(() => TestsGenerator.GenerateTests(sourceCode)), execOptions ); var writeFile = new ActionBlock <TestUnit> ( async filesContent => { await File.WriteAllTextAsync(destFolder + '\\' + filesContent.filename + ".cs", filesContent.sourceCode); }, execOptions ); loadFile.LinkTo(generateTests, linkOptions); generateTests.LinkTo(writeFile, linkOptions); foreach (var filename in filenames) { loadFile.Post(filename); } loadFile.Complete(); return(writeFile.Completion); }
public void Initialize() { string path = "TestClass.cs"; int countOfReadThreads = 2; int countOfProcessThreads = 3; int countOfWriteThreads = 4; var сonfig = new Config(countOfReadThreads, countOfProcessThreads, countOfWriteThreads); var generator = new TestsGenerator(сonfig); string sourceCode; using (StreamReader strmReader = new StreamReader(path)) { sourceCode = strmReader.ReadToEnd(); } var parcer = new SourceCodeParcer(); parcedClasses = parcer.Parce(sourceCode); generatedTests = generator.GenerateTests(sourceCode); }
public void TestManyClass() { string ManyClassTest = @"namespace InterfaceName { class TestClass { } interface ITestInt { } interface IMoreInt { } class ClassWithInter { public ClassWithInter(int i, ITestInt testInt) { } private void PrivateMeth() { } public void VoidMeth(int k, float b, ITestInt test) { } public char CharMeth(TestClass test) { return 'F'; } } class ClassWithTwoInter { public ClassWithTwoInter(int i, ITestInt testInt, IMoreInt moreInt) { } public char CharMeth(TestClass test) { return 'F'; } } }"; Tests[] tests = TestsGenerator.GenerateTests(ManyClassTest); Assert.AreEqual(tests.Length, 2); }
public void TestInterfaceClass() { string InterfaceClassTest = @" namespace InterfaceName { class TestClass { } interface ITestInt { } interface IMoreInt { } class ClassWithInter { public ClassWithInter(int i, ITestInt testInt) { } private void PrivateMeth() { } public void VoidMeth(int k, float b, ITestInt test) { } public char CharMeth(TestClass test) { return 'F'; } } }"; Tests[] tests = TestsGenerator.GenerateTests(InterfaceClassTest); string ExpectredText = @"using NUnit.Framework; using Moq; using InterfaceName; namespace InterfaceName.Test { [TestFixture] public class ClassWithInterTests { private ClassWithInter _ClassWithInterUnderTest; private Mock<ITestInt> _testIntDependency; [SetUp] public void SetUp() { int i = default; _testIntDependency = new Mock<ITestInt>(); _ClassWithInterUnderTest = new ClassWithInter(i, _testIntDependency.Object); } [Test] public void VoidMethTest() { int k = default; float b = default; _ClassWithInterUnderTest.VoidMeth(k, b, _testDependency.Object); Assert.Fail(" + "\"autogenerated\"" + @"); } [Test] public void CharMethTest() { TestClass test = default; char actual = _ClassWithInterUnderTest.CharMeth(test); char expected = default; Assert.That(actual, Is.EqualTo(expected)); Assert.Fail(" + "\"autogenerated\"" + @"); } } }"; Assert.AreEqual(tests.Length, 1); Assert.AreEqual(ExpectredText, tests[0].TestCode); }