简体   繁体   English

C#代码解析器以评估可测试性?

[英]Parser for C# code to evaluate testability?

I'm trying to write a program that will allow me to give it a collection of C# code files and analyze them for testability problems . 我正在尝试编写一个程序,该程序将允许我给它一个C#代码文件的集合并分析它们的可测试性问题 I'm starting out with just the 4 provided in the preceding link. 我仅从前面链接中提供的4开始。

The end goal is to create a report of all the files, any testability problems they might have, and specific code annotations on where these problems may be observed (if applicable). 最终目标是创建一个有关所有文件,它们可能存在的任何可测试性问题以及关于可能在何处观察到这些问题的特定代码注释(如果适用)的报告。 What I would like help with is choosing some tools to make the job of parsing the code easier; 我需要帮助的是选择一些工具来简化代码解析工作; I can read everything in as a string but making order out of the code to analyze it is very difficult in that situation. 我可以将所有内容读为字符串,但是在这种情况下很难使代码按顺序进行分析以进行分析。

I have evaluated the following things so far: 到目前为止,我已经评估了以下内容:

  • FxCop (does not function for anything that's not a .dll, a few test projects are web projects with testable logic in their controllers or presenters) FxCop (不适用于非.dll的功能,一些测试项目是在控制器或演示者中具有可测试逻辑的Web项目)
  • Code Contracts (not what I need; this does not help identify problems) 代码合同 (不是我所需要的;这无助于发现问题)
  • Pex (ditto) Pex (同上)
  • NRefactory (Might be interesting, but documentation and usage information is nonexistant and the demos are broken, even with gtk/mono installed on windows) NRefactory (可能很有趣,但是即使在Windows上安装了gtk / mono,也不存在文档和使用信息,并且演示也被破坏了)
  • CSharpCodeProvider (The .Parse method returns a NotImplementedException) CSharpCodeProvider (.Parse方法返回NotImplementedException)

As far as what I'm looking for: 就我要找的东西:

I would at least like to have detection of basic object structures and accessor objects to navigate around in (eg, a File object has a Namespace property with a collection of Classes , each Class has collections of Members , Constructors , and Methods , etc). 我至少想要检测基本对象结构和访问对象以进行导航(例如, File对象具有带一组ClassesNamespace属性,每个Class类具有MemberConstructorsMethods等的集合)。 Obviously if there are tools that can get more and more elaborate and detailed that would be amazing, but just having these things available to allow me to process small, focused strings would be a complete god-send. 显然,如果有工具可以变得越来越精致和详细,那将是惊人的,但是仅通过这些工具使我能够处理小而集中的字符串将是一个完美的选择。

Thanks in advance. 提前致谢。

If you can use prerelease code, you might want to check out roslyn , ie "compiler as a service": 如果可以使用预发布代码,则可能需要签出roslyn ,即“编译器即服务”:

Traditionally, compilers are black boxes – source code goes in one end and object files or assemblies come out the other end. 传统上,编译器是黑匣子–源代码在一端,而目标文件或程序集则在另一端。 The Roslyn [project] changes that model by opening up the Visual Basic and C# compilers as APIs. Roslyn [项目]通过打开Visual Basic和C#编译器作为API来更改模型。 These APIs allow tools and end-users to share in the wealth of information the compilers have about code. 这些API允许工具和最终用户共享编译器有关代码的大量信息。

Mind you, however, that interpreting what you get (a syntax tree) might still be a lot of work. 但是请注意,解释所得到的内容(语法树)可能仍然需要大量工作。

NRefactory 5 offers roughly the same analysis features for C# as Roslyn for C#, "only" the ability to compile to IL is missing. NRefactory 5为C#提供了与Roslyn for C#大致相同的分析功能,“仅”缺少编译为IL的功能。

Not sure what's going on with the GTK-based demo; 不知道基于GTK的演示是怎么回事; so I've fixed the WinForms-based demo and added that back to the solution. 因此,我已经修复了基于WinForms的演示并将其添加到解决方案中。

For parsing C# code, use new CSharpParser().Parse(sourceCode, "filename.cs") . 要解析C#代码,请使用new CSharpParser().Parse(sourceCode, "filename.cs") This will give you the full syntax tree for the file; 这将为您提供文件的完整语法树。 ie a CompilationUnit root node with NamespaceDeclaration s as children. 即,以NamespaceDeclaration s作为子NamespaceDeclarationCompilationUnit根节点。 Use the demo application to see what the tree looks like. 使用演示应用程序查看树的外观。

If you want to detect certain code patterns (your testability issues), you can use NRefactory's pattern matching feature. 如果要检测某些代码模式(您的可测试性问题),则可以使用NRefactory的模式匹配功能。 (see doc/Pattern Matching.html for details) (有关详细信息,请参阅doc/Pattern Matching.html

I think NRefactory with pattern matching might work better than Roslyn for your usecase. 我认为在您的用例中,具有模式匹配的NRefactory可能比Roslyn更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM