jeudi 29 septembre 2011

Ayende tax calculation challenge

I had some fun doing this little challenge from ayende, it's a good exercise to practice TDD. Here is my solution in C#.
The tests
public class TaxCalculatorTests
	public void Tax_for_0()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(0);
		Assert.That(tax, Is.EqualTo(0));
	public void Tax_for_5000()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(5000);
		Assert.That(tax, Is.EqualTo(500));
	public void Tax_for_5800()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(5800);
		Assert.That(tax, Is.EqualTo(609.2).Within(0.001));

	public void Tax_for_9000()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(9000);
		Assert.That(tax, Is.EqualTo(1087.8).Within(0.001));

	public void Tax_for_15000()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(15000);
		Assert.That(tax, Is.EqualTo(2532.9).Within(0.001));

	public void Tax_for_50000()
		var calculator = new TaxCalculator();
		var tax = calculator.TaxFor(50000);
		Assert.That(tax, Is.EqualTo(15068.1).Within(0.001));
And the implementation
public class TaxCalculator
	private readonly Tuple<decimal, decimal>[] slices = 
		new Tuple<decimal, decimal>(40230, 0.45M),
		new Tuple<decimal, decimal>(21240, 0.33M),
		new Tuple<decimal, decimal>(14070, 0.30M),
		new Tuple<decimal, decimal>(8660, 0.23M),
		new Tuple<decimal, decimal>(5070, 0.14M),
		new Tuple<decimal, decimal>(0, 0.10M),
	public decimal TaxFor(decimal sum)
		if (sum == 0)
			return 0;
		var tax = 0M;
		foreach (var slice in slices)
			if (sum > slice.Item1)
				var amount = sum - slice.Item1;
				tax += amount * slice.Item2;
				sum -= amount;
		return tax;

mardi 20 septembre 2011

ReSharper test runner with MSTest

The current project I'm working on at my job uses MSTest for testing (and I don't like it!). Anyway, I had an error while running the test suite in Resharper test runner while there were none in Visual Studio test runner.

Actually, the ReSharper test runner create a specific directory where it copies all assemblies under test and runs the test from this location. One of my tests had to load all assemblies in the executing assembly path and some of them were missing. The problem comes from the fact that by default ReSharper is configured to shallow-copy assemblies being tested.

What you need to do is go to Resharper -> Options.

From left side pane select tools -> Unit Test

Uncheck Shallow-copy assemblies being tested

And now, all my tests pass as expected!

dimanche 18 septembre 2011

Bad URI error with heroku on windows

I wanted to push my little rails app to Heroku but after asking for my credentials, the heroku command failed with the following message:
C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:156:in `split': bad URI(is not URI?): (URI::InvalidURIError)
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:174:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/1.9.1/uri/common.rb:628:in `parse'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:99:in `net_http_class'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:142:in `transmit'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/resource.rb:67:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:554:in `process'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:536:in `post'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/client.rb:35:in `auth'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:96:in `ask_for_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:129:in `ask_for_and_save_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:71:in `get_credentials'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/auth.rb:19:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command/auth.rb:12:in `login'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/lib/heroku/command.rb:114:in `run'
        from C:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/heroku-2.7.0/bin/heroku:14:in `'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `load'
        from C:/RailsInstaller/Ruby1.9.2/bin/heroku:19:in `
Actually, the problem comes once again from my proxy. My environment variable for the proxy was set to HTTP_PROXY=host:port while the Heroku gem expects HTTP_PROXY=http://host:port
I took me one hour to find this, I'd rather read be more carefully the error messages...

vendredi 16 septembre 2011

How to setup github access through corporate firewall

I had troubles cloning and pushing to my github account. My company has a firewall and all internet accesses are through the corporate firewall. Hopefully, I found an easy way to get access to my account with the following steps. I'm using msysgit 1.7.6 on a Windows 7 machine.
Configure the github global property http.proxy:
git config --global http.proxy http://host:port
Access your git repository through the http protocol. For public repositories it's
git clone
And for private repositories, use the following command (you'll be prompted for your password):
git clone
If you want to push your changes to your github account, you have to change the URL for your remotes and use the same format as specified above.

lundi 10 janvier 2011

Debian autotest setup

I spent some time learning Ruby on Rails these days and I had some trouble setting up autotest on my Debian box.
I finally managed to make it work thanks to these instructions.
$ sudo gem install ZenTest
$ sudo gem install autotest-rails
$ sudo apt-get install libnotify-bin
$ sudo gem install test_notifier
And then add require test_notifier/runner/autotest" to the file ~/.autotest