Refactor to the NUnit Fluent API with ReSharper Code Patterns

by Alex Meyer-Gleaves 15 April 2010 - 7:19 PM

ReSharper Logo One of the new features in the recently released ReSharper 5 is Structural Search and Replace. This allows you to perform complex searches on your code, and then make equally complex replacements on the resulting matches. You can read the Introducing ReSharper 5.0: Structural Search and Replace post on the JetBrains blog to quickly bring yourself up to speed.

Solutions are commonly flooded with various code smells that are hard to eliminate right away. ReSharper 5 helps you get rid of them in existing code, but more than that, it allows you to create custom inspections to remove poor coding artifacts on a recurring basis.

Specifically, ReSharper helps configure custom, sharable code patterns, search for them, replace them, include them in code analysis, and even use quick-fixes for regular code maintenance! Building patterns and enforcing good practices has never been this easy. Corporate and team policies, custom frameworks, favorite open source libraries and tools — structured patterns are able to cover them all.

The coolest part of all this is being able to save the Structural Searches and include them alongside the built-in code inspections. NUnit is my preferred unit testing framework and I really like the fluent API it offers for making assertions. This seemed like the perfect opportunity to test out Structural Search by creating some code patterns that would find usages of the regular API and convert them to the new fluent API.

I basically wanted to find assertions that look this this.

object foo = new object();
object nullFoo = null;

Assert.AreEqual(1, 1);
Assert.AreNotEqual(1, 2);
Assert.AreNotSame(1, 2);
Assert.AreSame(foo, foo);
Assert.Contains(1, new List<int> {1, 2, 3});
Assert.False(false);
Assert.Greater(2, 1);
Assert.GreaterOrEqual(2, 1);
Assert.IsAssignableFrom(typeof(int), 1);
Assert.IsNotAssignableFrom(typeof(int), false);
Assert.IsEmpty(new List<int>());
Assert.IsFalse(false);
Assert.IsInstanceOf(typeof(int), 1);
Assert.IsNaN(double.NaN);
Assert.IsNotEmpty(new List<int> {1});
Assert.IsNotInstanceOf(typeof(int), false);
Assert.IsNotNull(foo);
Assert.IsNotNullOrEmpty("Foo");
Assert.IsNull(nullFoo);
Assert.IsNullOrEmpty(string.Empty);
Assert.IsTrue(true);
Assert.Less(1, 2);
Assert.LessOrEqual(1, 2);
Assert.NotNull(new object());
Assert.Null(nullFoo);
Assert.ReferenceEquals(foo, foo);
Assert.True(true);

And make them look like this instead.

object foo = new object();
object nullFoo = null;

Assert.That(1, Is.EqualTo(1));
Assert.That(2, Is.Not.EqualTo(1));
Assert.That(2, Is.Not.SameAs(1));
Assert.That(foo, Is.SameAs(foo));
Assert.That(new List<int> {1, 2, 3}, Contains.Item(1));
Assert.That(false, Is.False);
Assert.That(2, Is.GreaterThan(1));
Assert.That(2, Is.GreaterThanOrEqualTo(1));
Assert.That(1, Is.AssignableFrom<int>());
Assert.That(false, Is.Not.AssignableFrom<int>());
Assert.That(new List<int>(), Is.Empty);
Assert.That(false, Is.False);
Assert.That(1, Is.InstanceOf<int>());
Assert.That(double.NaN, Is.NaN);
Assert.That(new List<int> {1}, Is.Not.Empty);
Assert.That(false, Is.Not.InstanceOf<int>());
Assert.That(foo, Is.Not.Null);
Assert.That("Foo", Is.Not.Null.Or.Empty);
Assert.That(nullFoo, Is.Null);
Assert.That(string.Empty, Is.Null.Or.Empty);
Assert.That(true, Is.True);
Assert.That(1, Is.LessThan(2));
Assert.That(1, Is.LessThanOrEqualTo(2));
Assert.That(new object(), Is.Not.Null);
Assert.That(nullFoo, Is.Null);
Assert.That(foo, Is.SameAs(foo));
Assert.That(true, Is.True);

Nothing particularly difficult in this task other than a fair dose of pounding on the keyboard. After creating the first couple of patterns using the Pattern Catalogue dialog in Visual Studio, I exported the patterns to an XML file and continued my editing from there. Working directly with the XML in your favourite editor will certainly save you some time when making edits to your patterns in bulk. The end result was the list of code patterns below.

Pattern Catalogue

Now ReSharper will include these code patterns into its code inspections, and show the usual visual indicators for the Suggestion severity level.

Visual Indicators

JetBrains have given me yet another reason to love ReSharper. You can find an XML export of the code patterns attached.

NUnit Fluent API Code Patterns.xml (12.61 kb)

Tags: ,

Development Tools

ReSharper 5.0 Beta available

by Alex Meyer-Gleaves 24 December 2009 - 5:24 AM

ReSharper JetBrains has provided developers with an early Christmas present releasing the ReSharper 5.0 Beta on Christmas Eve. It looks there are plenty of new features to take for a test drive.

ReSharper 5.0 Beta introduces a great web development feature set; code analysis extended with call tracking, value tracking, and foreach-to-LINQ transformations; project-level refactorings; and a lot more enjoyable features.

The Beta version will work with both Visual Studio 2010 and Visual Studio 2008, although according to this post from JetBrains there are a number of known issues causing problems with support for Visual Studio 2010 Beta 2.

However, keep in mind that Visual Studio 2010 Beta 2 has a number of known issues that in certain scenarios prevent ReSharper from working well. Don’t worry too much though: it doesn’t mean your Visual Studio crashes every time you press Alt+Enter! JetBrains and Microsoft engineers are aware of the problems and working together to solve them by the time Visual Studio 2010 goes RTM.

Head on over to the What's New in ReSharper 5.0 Beta page for a download link and more information on the new features.

Tags:

Development Tools

ReSharper 4.5.1 Maintenance Release

by Alex Meyer-Gleaves 26 July 2009 - 5:22 PM

image JetBrains have made a 4.5.1 maintenance release of ReSharper available for download. It is a free upgrade from any 4.x version, and the Release Notes indicate that a large number of bugs have been addressed. Despite some issues I had with the Beta version I happily found the 4.5 release to be stable. I have been regularly testing the nightly builds and think that 4.5.1 will be a stable and reliable release.

A couple of highlights for me are that the default Naming Style rules have certainly been improved, with more options available for both private and non-private accessible members. Also, method names with an underscore are no longer considered erroneous, which was a problem for auto-generated event handlers in Visual Studio. It was also a problem if your unit test naming convention involves the use of underscores as a separator, like this one recommended by Roy Osherove.

Tags:

Development Tools

JetBrains ReSharper 4.5 Beta Uninstalled

by Alex Meyer-Gleaves 18 March 2009 - 3:30 PM

It felt like the Beta was offering performance improvements but in the end has been completely unusable for me. I have experienced Visual Studio hangs or excessive CPU consumption after performing builds, during debugging and at various points when navigating around the solution. After rolling back to version 4.1 I haven’t had any problems. Fingers crossed that the quality increases dramatically before the final release.

Tags:

Development Tools

JetBrains ReSharper 4.5 Beta

by Alex Meyer-Gleaves 18 March 2009 - 6:50 AM

The ReSharper 4.5 Beta is now available for download. I have installed the new version and look forward to seeing how much of a performance improvement there is. The claimed improvements are outlined below.

 ReSharper Speedometer

I didn’t have any problems with the installer which is always nice as the installer is technically a Beta version as well. The old version was removed and the new installed without issue.

 ReSharper Setup Completed

Time to take her for a test drive...

Tags:

Development Tools

About the author

Alex Meyer-Gleaves I'm a Technical Architect living in Australia (that island like continent in the southern hemisphere). I love Microsoft .NET and C#. I hate early mornings, slow drivers and Lotus Notes.

Twitter

Google Shared

 

Month List

Recent Comments

Comment RSS

Links

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010