CodeAnalysis in VS2010

One of the tools I love to use on project is CodeAnalysis, but one key feature sorely lacking in its current incantation is custom rules. They're there and you can write them...but nothing's publicly documented and they're pretty much near impossible to test from a unit testing framework perspective. Recently I saw this post about CodeAnalysis in VS2010, and I chimed in with my questions:

OK, 2 questions :) -

1) Are the rules APIs public? IOW is it now much easier to create custom rules?

2) Are custom rules testable under unit testing frameworks? Right now it's basically "impossible" (from what I've been able to gather) - the best way to test custom rules is to run them under VS or FxCop manually, which is less than ideal. Has this changed?

Thanks,

Jason Bock

Here's the response: 

The rule APIs are not available as a public SDK in the CTP release. We are still working on these and hoping to get them in for RTM, but it's going to be tight. Having a good unit testing story for rules is also part of our goal with the public SDK.

Tom

I don't want to read too much into this...but I was hoping for a better answer. No matter what framework/toolset/library/parser you're writing to wow .NET developers, having automated code reviews via tools is important, if not key, and not having a clear, simple way to do this with the tool that come in VS is somewhat disheartening [1]. I truly hope the SDK finally becomes public and unit testing custom rules becomes a nice story.

[1] And yes, I know about NDepend. Great tool. Not putting it down in the slightest. This post is just about the state of CodeAnalysis.

* Posted at 11.03.2008 10:31:30 AM (Last Update: 11.05.2008 09:11:01 AM) | 3 comments | Link | RSS *

Comments

# Gendarme, from Sebastien Pouliot at 11.03.2008 08:32:41 PM

Well I was a bit surprised by the answer too. Anyway if you feel like writing your own rule then I suggest you (shameless plug) to look at Gendarme. Developer documentation could be better but, heh, you got 180 samples to look/learn from :-)

# Sr Software Engineer, from Jeff LeBert at 11.04.2008 08:57:19 PM

Unit testing FxCop rules isn't that hard. I've build a bunch of custom FxCop rules mostly for the application frameworks we build and use at work. I've put the non-work specific ones out on CodePlex in the JslFxCop project.

I've got a RuleTestBase class the has a bunch of methods in it like RunAssemblyRule, RunFieldRule, RunMethodRule. One of the RunMethodRule takes an action delegate so you can write tests as in the following code:

[TestMethod]
public void Finds_standard_event_handlers()
{
var problems = RunMethodRule(
() =>
{
Control control = new Control();
control.TextChanged += this.EmptyHandler;
});

Assert.AreEqual(1, problems.Count);
Problem problem = problems[0];
Assert.AreEqual(90, problem.Certainty);
Assert.AreEqual(FixCategories.NonBreaking, problem.FixCategory);
Assert.AreEqual(MessageLevel.Warning, problem.MessageLevel);
Assert.AreEqual("Remove the empty event handler.", problem.Resolution.ToString());
}

Let me know if you you want any help.

Gendarme looks pretty good also. It's nice to have a choice. :-)

# Huh, from Jason Bock at 11.05.2008 09:10:09 AM

I've never had any success getting custom rules unit tests. I'll have to take a look at what you did in more detail.

Add a Comment

(*) = Required field
Name (*):

E-Mail (*):

Web Site:

Title (*):

Comments (*):

Enter the code you see (*)



Quote
"To know that you do not know is the best. To pretend to know when you do not know is disease." Lao Tzu
Twitter History
follow me on Twitter
Blog History