【发布时间】:2017-04-29 08:36:03
【问题描述】:
背景
我正在使用 StackExchange.Precompilation 在 C# 中实现面向方面的编程。 See my repository on GitHub.
基本思想是客户端代码将能够在成员上放置自定义属性,并且预编译器将对具有这些属性的任何成员执行语法转换。一个简单的例子是我创建的NonNullAttribute。当NonNullAttribute 放在参数p 上时,预编译器会插入
if (Object.Equals(p, null)) throw new ArgumentNullException(nameof(p));
在方法体的开头。
诊断很棒...
我想让错误地使用这些属性变得困难。我发现的最好方法(除了直观的设计)是为无效或不合逻辑的属性使用创建编译时Diagnostics。
例如,NonNullAttribute 对于值类型的成员没有意义。 (即使对于可为空的值类型,因为如果您想保证它们不为空,则应使用不可为空的类型。)创建Diagnostic 是通知用户此错误的好方法,而不会崩溃构建就像一个例外。
...但是我该如何测试它们呢?
诊断是突出显示错误的好方法,但我还想确保我的诊断创建代码没有错误。我希望能够设置一个可以像这样预编译代码示例的单元测试
public class TestClass {
public void ShouldCreateDiagnostic([NonNull] int n) { }
}
并确认已创建正确的诊断(或在某些情况下未创建任何诊断)。
熟悉 StackExchange.Precompilation 的人可以给我一些指导吗?
解决方案:
@m0sa 给出的答案非常有帮助。实现有很多细节,所以这里是单元测试的实际样子(使用 NUnit 3)。注意using static 对应SyntaxFactory,这消除了语法树构造中的很多混乱。
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NUnit.Framework;
using StackExchange.Precompilation;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace MyPrecompiler.Tests {
[TestFixture]
public class NonNull_CompilationDiagnosticsTest {
[Test]
public void NonNullAttribute_CreatesDiagnosticIfAppliedToValueTypeParameter() {
var context = new BeforeCompileContext {
Compilation = TestCompilation_NonNullOnValueTypeParameter(),
Diagnostics = new List<Diagnostic>()
};
ICompileModule module = new MyPrecompiler.MyModule();
module.BeforeCompile(context);
var diagnostic = context.Diagnostics.SingleOrDefault();
Assert.NotNull(diagnostic);
Assert.AreEqual("MyPrecompiler: Invalid attribute usage",
diagnostic.Descriptor.Title.ToString()); //Must use ToString() because Title is a LocalizeableString
}
//Make sure there are spaces before the member name, parameter names, and parameter types.
private CSharpCompilation TestCompilation_NonNullOnValueTypeParameter() {
return CreateCompilation(
MethodDeclaration(ParseTypeName("void"), Identifier(" TestMethod"))
.AddParameterListParameters(
Parameter(Identifier(" param1"))
.WithType(ParseTypeName(" int"))
.AddAttributeLists(AttributeList()
.AddAttributes(Attribute(ParseName("NonNull"))))));
}
//Make sure to include Using directives
private CSharpCompilation CreateCompilation(params MemberDeclarationSyntax[] members) {
return CSharpCompilation.Create("TestAssembly")
.AddReferences(References)
.AddSyntaxTrees(CSharpSyntaxTree.Create(CompilationUnit()
.AddUsings(UsingDirective(ParseName(" Traction")))
.AddMembers(ClassDeclaration(Identifier(" TestClass"))
.AddMembers(members))));
}
private string runtimePath = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\";
private MetadataReference[] References =>
new[] {
MetadataReference.CreateFromFile(runtimePath + "mscorlib.dll"),
MetadataReference.CreateFromFile(runtimePath + "System.dll"),
MetadataReference.CreateFromFile(runtimePath + "System.Core.dll"),
MetadataReference.CreateFromFile(typeof(NonNullAttribute).Assembly.Location)
};
}
}
【问题讨论】:
标签: c# unit-testing roslyn precompile