예제 #1
0
        /// <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));
                }
            }
        }
예제 #2
0
		/// <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();
		}
예제 #3
0
		/// <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();
		}
예제 #4
0
 public TestErrorReporter(TestTraceOutput output)
 {
     _output = output;
 }
예제 #5
0
 /// <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);
 }
예제 #6
0
        /// <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));
        }
예제 #7
0
        /// <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))));
        }
예제 #8
0
        /// <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));
        }
예제 #9
0
 public static SafetySharpRuntimeModel CreateRuntimeModel(TestTraceOutput output)
 {
     return(SafetySharpRuntimeModel.Create(CreateEmptyModel(output)));
 }
예제 #10
0
 public static ModelBase CreateEmptyModel(TestTraceOutput output)
 {
     return(Tests.GetModelBases(EmptyTestModel, output).First());
 }
예제 #11
0
		public TestErrorReporter(TestTraceOutput output)
		{
			_output = output;
		}
예제 #12
0
		/// <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);
		}
예제 #13
0
		/// <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));
				}
			}
		}
예제 #14
0
		public CSharpErrorReporter(TestTraceOutput output)
		{
			_output = output;
		}
예제 #15
0
		/// <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();
		}
예제 #16
0
		/// <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();
		}
예제 #17
0
 internal TestTraceOutputTextWriterAdapter(TestTraceOutput adaptTo, bool trace)
 {
     _adaptTo = adaptTo;
     _trace   = trace;
 }
예제 #18
0
 /// <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();
 }