private static void WriteToDocument( IDocumentWriter documentWriter, string relativePath, string contents, string?namespaceSuffix = null) { var document = new CSharpDocument(namespaceSuffix); document.AppendLine(contents); documentWriter.WriteDocument( relativePath, document.ToString()); }
public void CanFindMethodsWithExternalVariables() { //Arrange var sourceFolder = @"C:\Users\user1\source\repos\AutoTester\SampleAssembly"; var csFileName = "ClassWithMethodDependentPublicProp.cs"; var className = Path.GetFileNameWithoutExtension(csFileName); var csFilePath = Path.Combine(sourceFolder, csFileName); var compUnit = new CSharpDocument(csFilePath); //Act var methodsWithExtVariables = compUnit.GetMethodsWithExternalVars(className); //Assert Assert.AreEqual(1, methodsWithExtVariables.Count(), "Unexpected number of methods."); Assert.AreEqual("MethodInfluencedByPublicProp", methodsWithExtVariables.First().Identifier.ValueText, "Unexpected method name."); }
private void WriteDocument( ClientGeneratorContext context, CSharpDocument document) { string documentName = $"{document.Name}.{context.Settings.Name}.StrawberryShake.cs"; context.Log.WriteDocument(documentName); var fileName = IOPath.Combine(context.OutputDirectory, documentName); var sourceText = SourceText.From(document.SourceText, Encoding.UTF8); context.FileNames.Add(fileName); context.Execution.AddSource(documentName, sourceText); WriteFile(fileName, document.SourceText); }
public static object Run(string projectFilePath, TestSuite testSuite) { Console.WriteLine("\n##########################"); Console.WriteLine("####### AutoTester #######"); Console.WriteLine("##########################\n"); Console.WriteLine($"Running test suite for project '{projectFilePath}'..."); var project = new Project(projectFilePath); var assemblyName = project.Properties.Where(x => x.Name.Equals("MSBuildProjectName")).First().EvaluatedValue; Assembly assembly = Assembly.ReflectionOnlyLoad(assemblyName); var assemblyTypes = assembly.GetTypes(); var csFiles = project.AllEvaluatedItems.Where(x => x.ItemType.Equals("Compile")).Select(x => x.EvaluatedInclude); var rootFolder = Path.GetDirectoryName(projectFilePath); bool allTestsPassed = true; foreach (var csFileName in csFiles) { var csFilePath = Path.Combine(rootFolder, csFileName); Console.WriteLine($"\nParsing CS document '{csFilePath}'..."); var csDoc = new CSharpDocument(csFilePath); var csDocClasses = csDoc.Classes; if (csDocClasses == null || !csDocClasses.Any()) { PrintWarning($"Warning: file '{csFilePath}' contains no classes."); continue; } foreach (var classDecl in csDocClasses) { var reflectionType = assemblyTypes.Where(x => x.Name.Equals(classDecl.Identifier.ValueText)).FirstOrDefault(); var runType = Type.GetType(reflectionType.AssemblyQualifiedName); Console.WriteLine($"\nClass: '{classDecl.Identifier.ValueText}':"); var methods = classDecl.GetMethods(); if (!methods.Any()) { Console.WriteLine("\tClass contains no methods."); continue; } foreach (var method in methods) { Console.WriteLine($"\tMethod: '{method.Identifier.ValueText}':"); if (!testSuite.TestCases.ContainsKey(method.Identifier.ValueText)) { PrintWarning($"\t\tWarning: no test case defined for {classDecl.Identifier.ValueText}.{method.Identifier.ValueText}"); } else { //apply test case Console.WriteLine($"\t\tApplying test case for {classDecl.Identifier.ValueText}.{method.Identifier.ValueText}..."); var methodsInfo = (runType as TypeInfo).DeclaredMethods; var methodInfo = methodsInfo.Where(x => x.Name.Equals(method.Identifier.ValueText)).FirstOrDefault(); var result = TestMethod(runType, methodInfo, testSuite.TestCases[method.Identifier.ValueText].Cast <ITestCase>()); if (!result) { allTestsPassed = false; } } } } } return(allTestsPassed); }