Skip to content
This repository has been archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
Add unit test for the RoslynCompiler class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesar Blum Silveira committed Oct 14, 2015
1 parent 008a7fd commit b281692
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/Microsoft.Dnx.Compilation.CSharp/RoslynCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ private static CSharpCompilation ApplyProjectInfo(CSharpCompilation compilation,
{
var projectAttributes = new Dictionary<string, string>(StringComparer.Ordinal)
{
["System.Reflection.AssemblyTitleAttribute"] = project.Title,
["System.Reflection.AssemblyDescriptionAttribute"] = project.Description,
["System.Reflection.AssemblyCopyrightAttribute"] = project.Copyright,
["System.Reflection.AssemblyFileVersionAttribute"] = project.AssemblyFileVersion.ToString(),
["System.Reflection.AssemblyVersionAttribute"] = RemovePrereleaseTag(project.Version),
["System.Reflection.AssemblyInformationalVersionAttribute"] = project.Version
[typeof(System.Reflection.AssemblyTitleAttribute).FullName] = project.Title,
[typeof(System.Reflection.AssemblyDescriptionAttribute).FullName] = project.Description,
[typeof(System.Reflection.AssemblyCopyrightAttribute).FullName] = project.Copyright,
[typeof(System.Reflection.AssemblyFileVersionAttribute).FullName] = project.AssemblyFileVersion.ToString(),
[typeof(System.Reflection.AssemblyVersionAttribute).FullName] = RemovePrereleaseTag(project.Version),
[typeof(System.Reflection.AssemblyInformationalVersionAttribute).FullName] = project.Version
};

var assemblyAttributes = compilation.Assembly.GetAttributes()
Expand Down Expand Up @@ -327,7 +327,7 @@ private IList<SyntaxTree> GetSyntaxTrees(CompilationProjectContext project,

foreach (var d in dirs)
{
ctx.Monitor(new FileWriteTimeCacheDependency(d));
ctx?.Monitor(new FileWriteTimeCacheDependency(d));
}

return trees;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.Dnx.Compilation/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Dnx.Compilation.Tests")]
[assembly: InternalsVisibleTo("Microsoft.Dnx.Compilation.CSharp.Tests")]
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-US")]
82 changes: 82 additions & 0 deletions test/Microsoft.Dnx.Compilation.CSharp.Tests/RoslynCompilerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.CodeAnalysis;
using Microsoft.Dnx.Compilation.Caching;
using Microsoft.Dnx.Runtime;
#if DNX451
using Moq;
#endif
using Xunit;

namespace Microsoft.Dnx.Compilation.CSharp.Tests
{
public class RoslynCompilerTest
{
#if DNX451
[Fact]
public void FlowsProjectPropertiesIntoAssembly()
{
const string testName = "Test name";
const string testTitle = "Test title";
const string testDescription = "Test description";
const string testCopyright = "Test copyright";
const string testAssemblyFileVersion = "1.2.3.4";
const string testVersion = "1.2.3-rc1";
const string testFrameworkName = "DNX,Version=v4.5.1";

// Arrange
var compilationProjectContext = new CompilationProjectContext(
new CompilationTarget(testName, new FrameworkName(testFrameworkName), string.Empty, string.Empty),
string.Empty,
string.Empty,
testTitle,
testDescription,
testCopyright,
testVersion,
new Version(testAssemblyFileVersion),
false,
new CompilationFiles(
new List<string> { },
new List<string> { }),
new Mock<ICompilerOptions>().Object);
var compiler = new RoslynCompiler(
new Mock<ICache>().Object,
new Mock<ICacheContextAccessor>().Object,
new Mock<INamedCacheDependencyProvider>().Object,
new Mock<IAssemblyLoadContext>().Object,
new Mock<IFileWatcher>().Object,
new Mock<IApplicationEnvironment>().Object,
new Mock<IServiceProvider>().Object);
var metadataReference = new Mock<IRoslynMetadataReference>();
metadataReference
.Setup(reference => reference.MetadataReference)
.Returns(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

// Act
var compilationContext = compiler.CompileProject(
compilationProjectContext,
new List<IMetadataReference> { metadataReference.Object },
new List<ISourceReference> { },
() => new List<ResourceDescriptor> { });

// Assert
var expectedAttributes = new Dictionary<string, string>
{
[typeof(AssemblyTitleAttribute).FullName] = testTitle,
[typeof(AssemblyDescriptionAttribute).FullName] = testDescription,
[typeof(AssemblyCopyrightAttribute).FullName] = testCopyright,
[typeof(AssemblyFileVersionAttribute).FullName] = testAssemblyFileVersion,
[typeof(AssemblyVersionAttribute).FullName] = testVersion.Substring(0, testVersion.IndexOf('-')),
[typeof(AssemblyInformationalVersionAttribute).FullName] = testVersion,
};
var compilationAttributes = compilationContext.Compilation.Assembly.GetAttributes();

Assert.All(compilationAttributes, compilationAttribute => expectedAttributes[compilationAttribute.AttributeClass.ToString()].Equals(
compilationAttribute.ConstructorArguments.First().Value));
}
#endif
}
}
6 changes: 5 additions & 1 deletion test/Microsoft.Dnx.Compilation.CSharp.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"xunit.runner.aspnet": "2.0.0-aspnet-*"
},
"frameworks": {
"dnx451": { },
"dnx451": {
"dependencies": {
"Moq": "4.2.1312.1622"
}
},
"dnxcore50": { }
},
"commands": {
Expand Down

0 comments on commit b281692

Please sign in to comment.