Compiling .NET projects in Sublime Text

Feb 7, 2013

The subject of this post is the compilation of .NET projects and solutions in Sublime Text using the Build Systems feature.

Sublime Text supports custom Build Systems.

By adding MSBuild as a new Build System, it is possible to build Visual Studio projects and solutions without the Visual Studio IDE installed.

Adding the Source Code

The following steps require a folder with MSBuild project files. In order to be pragmatic, the custom Build System is going to compile the source code of AutoFixture.

Go to File menu, click Open Folder... and select the root folder of the project to be compiled.

Adding the new Build System

Go to Tools menu, Build System, and click New Build System...

Paste the following code to a file:

{
    "cmd": ["C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe", "AutoFixture.sln"],
    "working_dir": "${project_path:${folder:${file_path}}}\\Src"
}

Save the file as AutoFixture.sublime-build.

Go to Tools menu, Build System, and select AutoFixture.

The above steps are per MBSuild project file. Repeat the above steps by creating a Build System per MSBuild project file.

Running the Build

Go to Tools menu, and select Build. Alternatively, use the Ctrl+B command.

The compiler detects a missing semicolon.

In the above screenshot, on line 38 a semicolon has been intentionally removed for the demo. After running a build, the compiler detects the missing semicolon.

What about the Tests?

Currently, the easiest way to run the tests is by using a command-line interface version of the test runner.

On my machine, attempting to add a new Build System for the tests resulted in high memory usage. This has been tested while Sublime Text 3 was in beta (Build 3010).

Commit to Revision Control with Care

Jan 1, 2013

We should always commit to revision control with care.

Often, developers provide commit messages that are incomplete, lowercase, with orthographic permutations. Even worse, snapshots can contain code that doesn’t even compile.

In a signed document the rules and regulations can be altered and then another document is going to be created and signed, but the original document will always remain signed.

As signatures in documents can't be erased so do recorded snapshots, at least it is not always an easy task.

After a snapshot has been recorded it acts like a signature for the developer – should we ever put our signature roughly?

Empty ASP.NET Web API Project Template

Nov 2, 2012

Usually, every time you create a new Web API project you:

  1. Delete almost all the generated code.
  2. Uninstall NuGet packages that you don’t need (yet, or at all).
  3. Remove unused assembly references.
  4. Manually edit the Web.config file(s) to remove the elements that point to the assemblies which have been removed.

Solution

Use the Empty ASP.NET Web API Project Template. The Visual Studio extension can be downloaded from here.

It will add a new project template Empty Web API which includes the following:

Files

  • Properties\AssemblyInfo.cs
  • favicon.ico
  • Global.asax
  • Web.config

Assemblies

  • System
  • System.Core
  • System.Configuration
  • System.Net.Http
  • System.Web
  • System.Web.Abstractions
  • System.Web.ApplicationServices
  • System.Web.Mvc
  • System.Web.Routing
  • System.Xml

NuGet packages

  • Microsoft.AspNet.WebApi.Client
  • Microsoft.AspNet.WebApi.Core
  • Microsoft.AspNet.WebApi.WebHost
  • Microsoft.AspNet.Providers.Core
  • Microsoft.Net.Http
  • Newtonsoft.Json

Remarks

A favicon.ico file is included because the browser requests it so it’s better not to respond with a 404 Not Found.

Update (2013/10/06):

You may install Newtonsoft.Json through NuGet:

PM> Install-Package Newtonsoft.Json

Depending on the configuration, the Newtonsoft.Json package is included because it is can be lazy loaded inside the System.Net.Http.Formatting.JsonMediaTypeFormatter type.

Exercising the SUT asynchronously

Oct 28, 2012

In unit testing, there are times were the SUT has to be exercised asynchronously.

How can we wait for the exercise to complete execution?

  • An instance of the SUT can be created on the main thread.
  • The main (waiting) thread spins in user mode while starting the exercise of the SUT asynchronously.
  • Once the result has been received, the execution continues on the main thread.
  • The assertion takes place.

The SpinWait synchronization type contains a method named SpinUntil which works perfect for the described scenario.

// Fixture setup
var sut = new ObjectLocalStorage();
sut.Set(@object, expected);
object result = null;

// Exercise system
new Task(() => result = sut.Get(@object)).Start();
SpinWait.SpinUntil(() => result != null);

// Verify outcome
Assert.Equal(expected, result);
// Teardown
  • Once the Task has been created it is immediately scheduled for execution by calling the Start method.
  • As long as the unit test runs fast, the waiting thread spins in user mode, which is a good thing.

There is also a SpinUntil overload accepting a TimeSpan timeout.

Truly Constrained Non-Deterministic Numbers in AutoFixture

Oct 8, 2012

Numbers in AutoFixture are currently created using a strictly monotonically increasing sequence.

var fixture = new Fixture();

var i = fixture.CreateAnonymous<int>();
// Prints -> 1
var l = fixture.CreateAnonymous<long>();
// Prints -> 2
var f = fixture.CreateAnonymous<float>();
// Prints -> 3.0

Starting with version 2.13.0, by applying a specific customization numbers can also be created using a constrained non-deterministic sequence. The new customization is called RandomNumericSequenceCustomization.

var fixture = new Fixture()
    .Customize(new RandomNumericSequenceCustomization());

var i = fixture.CreateAnonymous<int>();
// Prints -> 122
var l = fixture.CreateAnonymous<long>();
// Prints -> 38
var f = fixture.CreateAnonymous<float>();
// Prints -> 147.0

Once the customization has been applied to a Fixture instance subsequent requests for numeric types will yield random non-repeatable numbers in the range of [1, 255]. When requesting more than 255 numbers the range is automatically changed to [256, 32767] and so on.

The default ranges are [1, 255], [256, 32767], and [32768, 2147483647].

Supplying a custom range

To supply a custom range, customize an instance of the Fixture class with an instance of the RandomNumericSequenceGenerator and pass to its constructor a sequence of integer numbers (e.g. -100, 100, 255).

The sequence must be two positive or negative numbers optionally followed by a series of greater numbers.

var fixture = new Fixture();
fixture.Customizations.Add(
    new RandomNumericSequenceGenerator(-100, 100, 255));

var i = fixture.CreateAnonymous<int>();
// Prints -> -95
var l = fixture.CreateAnonymous<long>();
// Prints -> 47
var f = fixture.CreateAnonymous<float>();
// Prints -> -82.0

After applying the customization, numbers are now created in the range of [-100, 100]. However, when requesting more numbers than the range size the range is automatically changed to [101, 255].

subscribe via RSS