/// <summary> /// Compiles the <paramref name="compilation" /> with the S# compiler and returns the resulting assembly that has been /// loaded into the app domain. /// </summary> /// <param name="compilation">The compilation that should be compiled.</param> /// <param name="output">The output that should be used to write test output.</param> public static Assembly CompileSafetySharp(Compilation compilation, TestTraceOutput output) { using (var workspace = new AdhocWorkspace()) { var project = workspace .AddProject(compilation.AssemblyName, LanguageNames.CSharp) .AddMetadataReferences(compilation.References) .WithCompilationOptions(compilation.Options); foreach (var syntaxTree in compilation.SyntaxTrees) { project = project.AddDocument(syntaxTree.FilePath, syntaxTree.GetRoot().GetText(Encoding.UTF8)).Project; } var errorReporter = new TestErrorReporter(output); var compiler = new Compiler(errorReporter); try { var assembly = compiler.Compile(project); output.Trace("{0}", SyntaxTreesToString(compiler.Compilation)); return(assembly); } catch (CompilationException e) { throw new TestException("{0}\n\n{1}", e.Message, SyntaxTreesToString(compiler.Compilation)); } } }
/// <summary> /// Executes the tests of the object. /// </summary> /// <param name="output">The output that should be used to write test output.</param> public void Test(TestTraceOutput output) { Output = output; Seal(); Metadata.Model.ShouldBe(this); Check(); }
/// <summary> /// Executes the tests of the object. /// </summary> /// <param name="output">The output that should be used to write test output.</param> public void Test(TestTraceOutput output) { Output = output; MetadataBuilder.FinalizeMetadata(); Metadata.Component.ShouldBe(this); Check(); }
public TestErrorReporter(TestTraceOutput output) { _output = output; }
/// <summary> /// Initializes a new instance. /// </summary> /// <param name="output">The stream that should be used to write the test output.</param> public Tests(ITestOutputHelper output = null) { Output = new TestTraceOutput(output); }
/// <summary> /// Compiles the <paramref name="code" />, instantiates all non-abstract classes implementing /// <see cref="ModelBase" />, and returns them. /// </summary> /// <param name="code">The code that should be compiled.</param> /// <param name="output">The output that should be used to write test output.</param> public static IEnumerable <ModelBase> GetModelBases(string code, TestTraceOutput output) { var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, path: ".", encoding: Encoding.UTF8); return(GetModelBases(syntaxTree, output)); }
/// <summary> /// Compiles the <paramref name="syntaxTree" />, instantiates all non-abstract classes implementing /// <see cref="ModelBase" />, and returns them. /// </summary> /// <param name="syntaxTree">The syntax tree that should be compiled.</param> /// <param name="output">The output that should be used to write test output.</param> protected static IEnumerable <ModelBase> GetModelBases(SyntaxTree syntaxTree, TestTraceOutput output) { var compilation = CreateCompilation(syntaxTree); var semanticModel = compilation.GetSemanticModel(syntaxTree); var testableTypes = syntaxTree .Descendants <ClassDeclarationSyntax>() .Select(declaration => declaration.GetTypeSymbol(semanticModel)) .Where(symbol => !symbol.IsGenericType && !symbol.IsAbstract && symbol.ContainingType == null) .Where(symbol => symbol.IsDerivedFrom(semanticModel.GetTypeSymbol <ModelBase>())) .Select(symbol => symbol.ToDisplayString()) .ToArray(); if (testableTypes.Length == 0) { throw new TestException("Unable to find any ModelBase declarations."); } var assembly = CompileSafetySharp(compilation, output); return(testableTypes.Select(testableType => (ModelBase)Activator.CreateInstance(assembly.GetType(testableType)))); }
/// <summary> /// Compiles the <paramref name="code" />, instantiates all non-abstract classes implementing /// <see cref="ITestableObject" />, and returns them. /// </summary> /// <param name="code">The code that should be compiled.</param> /// <param name="output">The output that should be used to write test output.</param> public static IEnumerable <ITestableObject> GetTestableObjects(string code, TestTraceOutput output) { var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, encoding: Encoding.UTF8); return(GetTestableObjects(syntaxTree, output)); }
public static SafetySharpRuntimeModel CreateRuntimeModel(TestTraceOutput output) { return(SafetySharpRuntimeModel.Create(CreateEmptyModel(output))); }
public static ModelBase CreateEmptyModel(TestTraceOutput output) { return(Tests.GetModelBases(EmptyTestModel, output).First()); }
/// <summary> /// Compiles the <paramref name="compilation" /> with the S# compiler and returns the resulting assembly that has been /// loaded into the app domain. /// </summary> /// <param name="compilation">The compilation that should be compiled.</param> /// <param name="output">The output that should be used to write test output.</param> public static Assembly CompileSafetySharp(Compilation compilation, TestTraceOutput output) { using (var workspace = new AdhocWorkspace()) { var project = workspace .AddProject(compilation.AssemblyName, LanguageNames.CSharp) .AddMetadataReferences(compilation.References) .WithCompilationOptions(compilation.Options); foreach (var syntaxTree in compilation.SyntaxTrees) project = project.AddDocument(syntaxTree.FilePath, syntaxTree.GetRoot().GetText(Encoding.UTF8)).Project; var errorReporter = new TestErrorReporter(output); var compiler = new Compiler(errorReporter); try { var assembly = compiler.Compile(project); output.Trace("{0}", SyntaxTreesToString(compiler.Compilation)); return assembly; } catch (CompilationException e) { throw new TestException("{0}\n\n{1}", e.Message, SyntaxTreesToString(compiler.Compilation)); } } }
public CSharpErrorReporter(TestTraceOutput output) { _output = output; }
/// <summary> /// Executes the tests of the object. /// </summary> /// <param name="output">The output that should be used to write test output.</param> public void Test(TestTraceOutput output) { Output = output; Check(); }
/// <summary> /// Executes the tests of the object. /// </summary> /// <param name="output">The output that should be used to write test output.</param> /// <param name="args">The arguments passed to the test object.</param> public void Test(TestTraceOutput output, object[] args) { Output = output; Check(); }
internal TestTraceOutputTextWriterAdapter(TestTraceOutput adaptTo, bool trace) { _adaptTo = adaptTo; _trace = trace; }