Search code examples
c#unit-testingparsingtestability

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. I'm starting out with just the 4 provided in the preceding link.

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)
  • Code Contracts (not what I need; this does not help identify problems)
  • Pex (ditto)
  • NRefactory (Might be interesting, but documentation and usage information is nonexistant and the demos are broken, even with gtk/mono installed on windows)
  • CSharpCodeProvider (The .Parse method returns a 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 (e.g., a File object has a Namespace property with a collection of Classes, each Class has collections of Members, Constructors, and Methods, etc). 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.


Solution

  • If you can use prerelease code, you might want to check out roslyn, i.e. "compiler as a service":

    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. These APIs allow tools and end-users to share in the wealth of information the compilers have about code.

    Mind you, however, that interpreting what you get (a syntax tree) might still be a lot of work.