Thursday, 17 December 2009

The "http://www." key

About 16 hours ago (according to Twitter) I saw the following tweet from Scott Hanselman:

"I need a keyboard with an "http://www." button." -- shanselman

As 'tis the season, I thought I'd oblige. :)

  1. If you haven't done so already, download and install the wonderful AutoHotKey.
  2. Create a file called "theHttpKey.ahk". On your desktop is fine for now.
  3. Enter the following text into the file and save it:
    #h::
    Send http://www.
    return
    
  4. Double-click the file to load it (you'll see a little "H" in the tray on your taskbar, which is the default icon for loaded AutoHotKey files).
  5. Press Win + h whenever you want to type "http://www.".
  6. Enjoy your new HTTP key! :) (or ... Profit! for the meme addicted).

If you want this hotkey available all the time, just drag the file or a shortcut to it to your startup folder. If you want to stop the hotkey from working, right click on the icon in the tray and click exit.

So that's how easy it is to map Win + h (or #h in AutoHotKey parlance) to send some text. Or you can always choose a new key combination if you don't like Win + h.

If you are not using AutoHotKey you really owe it to yourself to try it. It is just so easy to automate those little things you keep wishing "Wouldn't it be nice if...", and this can give you a surprisingly large productivity boost. AutoHotKey can do lots of advanced stuff too (moving windows, adding intellisense to prompts, creating forms etc.), but even without delving right into it you can easily create some useful little mappings (like helping with BDD test naming or simulating Emacs key-bindings).

Enjoy!

Wednesday, 16 December 2009

Mocking delegates with Rhino Mocks

I recently found out that Rhino Mocks can mock delegates. Not that I didn't think it couldn't, it just never occurred to me to try. But it turns out to be quite nice for stubbing delegate calls or for checking they were called. Say we have the following class:

public class SomeClass {
    private readonly Func<int, int> _mapper;

    public SomeClass(Func<int, int> mapper) {
        _mapper = mapper;
    }

    public int DoSomething(int toThisInt) {
        return _mapper(toThisInt);
    }
}

Here's an example of testing that a simple Func<int, int> delegate is called correctly by our class under test without using Rhino Mocks:

[Test]
public void ManualFakeDelegate() {
    //Arrange
    var wasCalled = false;
    var intMapped = 0;
    var expectedResult = 1234;
    Func<int, int> fakeMapper = i => {
                        wasCalled = true;
                        intMapped = i;
                        return expectedResult;
                    };
    var someClass = new SomeClass(fakeMapper);

    //Act
    var result = someClass.DoSomething(10);

    //Assert
    Assert.True(wasCalled);
    Assert.AreEqual(intMapped, 10);
    Assert.AreEqual(expectedResult, result);
}

Now using Rhino Mocks:

[Test]
public void MockingDelegates() {
    var stubMapper = MockRepository.GenerateStub<Func<int, int>>();
    var expectedResult = 1234;
    stubMapper.Stub(x => x(10)).Return(expectedResult);            
    var someClass = new SomeClass(stubMapper);

    var result = someClass.DoSomething(10);

    Assert.AreEqual(expectedResult, result);
}

We no longer have to maintain all that test state to assert against, nor do we need to create any fake delegate implementation. We can also use stubMapper.AssertWasCalled(x => x(10)), but in this case that is already tested implicitly through the stubbed call and assertion.

In the first case, we could move some more advanced logic into our fakeMapper to do more advanced parameter checking and avoid some of the extra state and assertions, but that is potentially error-prone and we get it for free with our mocking library.

As an aside, you can obviously also mock Action or Func, but the wasCalled/fakeMapper approach is probably easier in that case.

Not something I'll need very often, but nice to know for those rare occasions were I do.

Saturday, 12 December 2009

3 years of Davesquared

Wow, it feels like only a year ago I was celebrating two years of writing my blog. :P That's right, I've now been slaving over my little corner of the intraweb for 3 years. From humble beginnings as a naive, new blogger with virtually no readers, the intervening three years have seen me grow into a naive, old blogger with virtually no readers.

In all seriousness though, thanks to everyone that has subscribed and/or stopped by for a visit. I really appreciate it. An extra big thanks goes to those who have taken the time to leave comments.

If you'd like to give me a Happy Blogsday present, I'd love to get a comment from you so I can see who's crazy enough to subscribe to my ramblings. That or an iPhone -- completely up to you.

I'm looking forward to posting more ramblings in the year ahead.

Best regards,
David

Some davesquared trivia

  • 297 posts
  • I've written all my posts for the last 2 years in Notepad++
  • My most visited post last year was on the SOLID principles of OO design
  • Apparently I've had 2,885 visits from dialup users
  • 49% of visitors use FireFox, 37% use IE, 8% use Chrome
  • 11 visits from Android OS
  • Google accounts for 99.19% of visitors from search engines. Bing is 4th at 0.07%.
  • Most obscure search term used: "find 2nd to last item prolog"
  • dave^2 = -1 is meant to translate to "i is dave", but most of my colleagues insist it means I am imaginary. I prefer to think of myself as complex yet real-componently challenged. ;)

My favourite posts this year