Switch Visual Studio Settings for Different Solutions

Switching between projects with different formatting styles can be a real pain. I have finally found a solution if you are using Visual Studio and a partial solution for most other platforms.

If you add two configuration files to the root of the solution, they will automatically configure your editor to the coding standards of the solution when you open it and restore your settings to your defaults when you close.

Editor Config

This file defines spacing and tabs for the files in the solution. There are plugins available for pretty much every editor and IDE out there including Visual Studio, Vi, Emacs and even Notepad++.

For more information, got to http://editorconfig.org/. The Visual Studio extension is on GitHub, and can be installed from the Visual Studio Gallery.

Rebracer

Rebracer sets all of the code formatting rules for the project when you open the solution and restores your defaults when you close. To change the settings you simply need to open edit the Visual Studio options and the changes will automatically be saved out to the Rebracer.xml file.

Rebracer is also on GitHub, and can be installed from the Visual Studio Gallery.

Read More

NUnit 3.0 – Values Attribute for Enums

To celebrate the second alpha release of NUnit 3.0, I am going to write a few posts on some of the new features that we’ve added.

Today’s feature is a simple one, but one of my favorites. With older versions of NUnit, if you wanted to run a test against every value of an enum, you would either have to write code to iterate through the values, or add them as TestCases and risk missing values added to the enum later. With the 3.0 release, you can now write a test method like the following which will be run with every enum value. The [Values] attribute on an enum parameter will automatically expand to all possible Enum values.

[Test]
public void MyEnumTest([Values]MyEnumType myEnumArgument)
{
    //...
}

It is probably less useful, but we also added the same support for Boolean values. Add the [Values] attribute to a bool and the method will be run with true and false.

[Test]
public void MyBoolTest([Values]bool value)
{
    //...
}

Read More

Visual Studio 2013 Modifies NUnit Project Files

I’ve seen this several times now and an issue was just reported against the NUnit Visual Studio adapter, so I thought I would document my findings on this.

If you run non-Microsoft unit tests in Visual Studio using a test adapter, Visual Studio 2013 will modify the project file and add the following lines.

<ItemGroup>
  <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>

This happens for NUnit, XUnit and other 3rd Party test adapters. This behavior is a new feature of Visual Studio 2013.

In this issue on Connect, Microsoft says it is intentional and provides what I see as a less than ideal workaround.

Posted by Microsoft on 10/24/2013 at 7:38 AM

This behavior is intentional.

To support third-party test frameworks, like NUnit and XUnit, Visual Studio 2012 loaded Test Explorer on solution open, regardless of whether it contained test projects. This added seconds of delay to startup and solution open scenarios for all users, majority of whom don’t use tests.

In Visual Studio 2013, we changed it so that Test Explorer package is loaded only when the solution contains one or more test projects. Test projects are identified in two different ways. Projects created from one of the built-in unit test project templates are identified using project type GUIDs. Other types of projects, such as Class Library project with XUnit or NUnit tests, are identified by Test Explorer during first test discovery and “tagged” with the Service/item.

You can avoid Visual Studio automatically adding the Service/item to your test projects by creating them from any of the built-in unit test project templates. In other words, select the “Unit Test Project” template instead of the “Class Library” template in the New Project dialog.

If you want to change existing projects from a “Class Library” to a “Unit Test Project”, edit the project file and add the following to the first PropertyGroup

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TestProjectType>UnitTest</TestProjectType>

This isn’t much different than the Service being added, but maybe it is more correct because it is a test project?

Unfortunately, this doesn’t work if anyone is using Xamarin studio since the Unit Test Project type is not supported.

Read More

C# 6.0 – Declaration Expressions

This is another post in the series on the new features that are coming in C# 6.0.

This is a complex name for a simple concept. Until now, if you ever wanted to use an out parameter in a method, you had to declare it before using it.

We’ve all written code like this,

int i;
if (Int32.TryParse(value, out i))
{
    // Use i here
}

Now you can write the much simpler,

if (Int32.TryParse(value, out var i))
{
    Assert.That(i, Is.EqualTo(expected));
}

There are a couple of things to note here. First is that i is only in scope within the if statement. You cannot use it outside the if statement. Second, notice that I used var and that the type was correctly infered as int.

It is also useful for casting. You no longer have to cast an object as something, then check if it is null. You can now do it all within the if statement.

if ((string str = obj as string) != null )
{
    Console.WriteLine(str);
}

It can also be useful for quickly supplying default args for a TryParse.

int x = Int32.TryParse(value, out var i) ? i : -1;

Sweet and simple…

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read More

C# 6.0 – Null-Conditional Operators

This is another post in the series on the new features that are coming in C# 6.0.

This is one of my favourite new features. Have you ever written code like this?

if (line != null && line.Product != null)
{
    total += line.Product.Price * line.Quantity;
}

C# 6.0 introduces the ?. null-conditional operator that short circuits and returns null if anything in the chain is null. This combines really well with the null-coalescing operator allowing you to rewrite the above as follows.

total += line?.Product?.Price ?? 0 * line?.Quantity ?? 0;

This also works with indexers,

var firstProduct = OrderLines?[0].Product;	// Will return null if OrderLines is null

It also works with methods. This will be really handy for triggering events. Instead of writing,

if (OrderChanged != null)
{
    OrderChanged(this, EventArgs.Empty);
}

You will now be able to write.

OrderChanged?.Invoke(this, EventArgs.Empty);

If you want to check out the code for this series of posts, it is on GitHub at https://github.com/rprouse/CSharp60.

Read More