예제 #1
0
        public void Given_source_file_has_syntax_error_when_compiling_should_throw()
        {
            var compilerOptions = new CompilerOptions
            {
                ReferencedAssemblies = GetReferencedAssemblies(),
                OutputFile           = TargetDllFile
            };

            // Replace contents of first file with invalid source code.
            _fakeSourceFiles.First().Value.Contents = Encoding.UTF8.GetBytes("Invalid C# code");

            _fileSystem.GetFile(TargetDllFile).Should().BeNull("before compiling the target DLL assembly should not exist");

            // Act
            Action act = () => _sut.CompileCode(_fakeSourceFiles.Keys, compilerOptions);

            // Assert
            act.Should().Throw <CompileException>();

            MockFileData targetFileData = _fileSystem.GetFile(TargetDllFile);

            if (targetFileData != null)
            {
                targetFileData.Should().NotBeNull("after compiling the target DLL assembly file is created");
                targetFileData.Contents.Should().BeEmpty("the contents should however be empty");
            }
        }
예제 #2
0
        public void Given_valid_source_files_and_options_when_compiling_should_build_assembly(bool loadAssemblyImmediately)
        {
            var compilerOptions = new CompilerOptions
            {
                ReferencedAssemblies = GetReferencedAssemblies(),
                OutputFile           = TargetDllFile,
                IgnoreCompilerErrors = new [] { "CS0042" }
            };

            // Act
            // ReSharper disable once RedundantArgumentDefaultValue
            Assembly result = _sut.CompileCode(_fakeSourceFiles.Keys, compilerOptions, loadAssemblyImmediately);

            // Assert
            MockFileData targetFileData = _fileSystem.GetFile(TargetDllFile);

            targetFileData.Should().NotBeNull("an assembly DLL should be generated");
            targetFileData.Contents.Should().NotBeEmpty("the assembly DLL contains IL");

            string       pdbFile     = _fileSystem.Path.Combine(_fileSystem.Path.GetDirectoryName(TargetDllFile), _fileSystem.Path.GetFileNameWithoutExtension(TargetDllFile) + ".pdb");
            MockFileData pdbFileData = _fileSystem.GetFile(pdbFile);

            pdbFileData.Should().NotBeNull("a PDB should be generated");
            pdbFileData.Contents.Should().NotBeEmpty();

            if (loadAssemblyImmediately)
            {
                result.Should().NotBeNull("'loadAssembly' was set to true");
            }
            else
            {
                result.Should().BeNull("'loadAssembly' was set to false");

                Func <Assembly> tryAssembly = () => Assembly.Load(targetFileData.Contents);
                result = tryAssembly.Should().NotThrow("the assembly should successfully be loaded").Which;
            }

            result.GetName().Name.Should().Be("output");
            result.ExportedTypes.Should().Contain(t => _typesToCompile.Contains(t.Name));
        }