Ayende @ Rahien

Unnatural acts on source code

Meta tests

I found this test fixture hilarious.

[TestFixture]
public class ValidateNamingConventions
{
    private System.Type[] types;

    [SetUp]
    public void Setup()
    {
        types = typeof(ISearchFactory).Assembly.GetExportedTypes();            
    }

    [Test]
    public void InterfacesStartsWithI()
    {
        foreach (System.Type type in types)
        {
            if(type.IsInterface==false)
                continue;
            Assert.IsTrue(type.Name.StartsWith("I"),type.Name);
        }
    }

    [Test]
    public void FirstLaterOfMethodIsCapitalized()
    {
        foreach (System.Type type in types)
        {
            foreach (MethodInfo method in type.GetMethods())
            {
                Assert.IsTrue(char.IsUpper(method.Name[0]), type.FullName + "." + method.Name);
            }
        } 
    }

    [Test]
    public void AttributesEndWithAttribute()
    {
        foreach (System.Type type in types)
        {
            if (type.IsAssignableFrom(typeof(Attribute)))
                continue;
            Assert.IsTrue(type.Name.EndsWith("Attribute"), type.Name);
     
        }
    }
}

Comments

Casey
10/01/2008 07:01 AM by
Casey

Well I started reading it thinking "how ridiculous" ... but now I'm not so sure ... in the absense of FxCop or NDepend ... it is one way of ensuring coding standards are being applied, though I grant a rather ingenious one :)

ZeusTheTrueGod
10/01/2008 07:17 AM by
ZeusTheTrueGod

Looks like reinventing the weel

May be better put this into PostBuild event?

I prefer Agent Smith plugin for Resharper - it shows me when code convention is broken

Jason Stangroome
10/01/2008 07:27 AM by
Jason Stangroome

Definitely hilarious. Really should be using MbUnit's TestSuite feature or your preferred test framework's equivalent so you get one test passing or failing for each interface, method, and attribute, not just a test that fails on the first problem it finds.

Tobin Harris
10/01/2008 07:32 AM by
Tobin Harris

They forgot to add

[Test]

public void TestNamesAreSmeltCorrectly(){...}

To catch the spelling error in "FirstLaterOfMethodIsCapitalized".

otter
10/01/2008 01:11 PM by
otter

that is very funny, and a very cool idea. however i don't think i would ever want to include a test like that as part of my continuous integration. it doesn't seem like it 'belongs' as a true unit test, perhaps it would be best used as a separate test from your standard unit tests.

Morgan
10/01/2008 06:43 PM by
Morgan

Thats a bit hilarius.

I use Code Style Enforcer for this, it plays nicely with ReSharper. You can customize the rules.

Anyone tried Microsofts StyleCop?

Judah Himango
10/02/2008 01:22 AM by
Judah Himango

I've tried StyleCop. It complained about some things we don't like ("always use 'this.' before accessing members", for example). We had literally several thousand violations. So we didn't pursue it any further.

Morgan
10/02/2008 06:11 AM by
Morgan

I think you can turn off rules in StyleCop, but you cant change a rule that says that private fields should start with m_ when you want only _.

So you have to disable that rule and implement your own custom rule.

Bunter
10/02/2008 01:54 PM by
Bunter

This kind of test is perfect for continuous integration as it will reveal convention mistakes for whole team extremely quickly and well, continuously.

John Rayner
10/02/2008 08:15 PM by
John Rayner
<shrug  We have tests like this to validate some of the conventions we've built up on the project.  For instance, we have an AOP framework in play and so we have a test that validates that constructors are protected and not public.  We have another that checks that properties meant to AOP'd are virtual.
  
  
We've even taken it a step further - for integration tests which connect to the database we setup a transaction at the start of the test and rollback when the test completes, but this is done in a base class.  So we actually have a test that validates that integration tests derive from this base class!!!
  
  
To be honest, I'd rather having a test that fails with a clear message than more subtle problems.
>
Roy Tate
10/08/2008 06:10 PM by
Roy Tate

Function AttributesEndWithAttribute should be corrected ...

if (type.IsAssignableFrom(typeof(Attribute)))

should be

if (type.IsAssignableFrom(typeof(Attribute)) == false)

Comments have been closed on this topic.