Archive for January, 2012

Faking Interfaces in unit tests for C++

Suppose you are writing a unit test for a class ClassUnderTest which interacts with another class AnnoyingBigClassWithLotsOfUndesireableStuffForAUnitTest (let’s call it ABCWLOUSFAUT for short). Now if you want to fake/mock ABCWLOUSFAUT without any special mocking frameworks, you may think there is a way to do it without actually having to factor out a common interface from ABCWLOUSFAUT for testability. Simply move out ABCWLOUSFAUT into a separate file and then use the header guard of this file, so you make sure that you don’t load the definition of ABCWLOUSFAUT when loading ClassUnderTest. Then just declare your own class with the name ABCWLOUSFAUT and make it how you want it for your test. In other words:


#define ABCWLOUSFAUT_H // make sure that we won't include ABCWLOUSFAUT 
                       // when we include ClassUnderTest

// our own definition of ABCWLOUSFAUT

class ABCWLOUSFAUT

{

// the minimum stuff we need to test ClassUnderTest

};

#include "class_under_test.h"

// your tests

DON’T do it!! What actually happens is that you include the header of ABCWLOUSFAUT in the .cpp file of ClassUnderTest and the mock definition of ABCWLOUSFAUT in the test. What you will get is that everything compiles and (if you are lucky) you will get a segfault because the ABCWLOUSFAUT class you have instantiated in memory is differently made up than the ABCWLOUSFAUT class that ClassUnderTest expects to use.

Thursday, January 26th, 2012 Uncategorized No Comments

Perform an action on a range of lines in vim

The basics

To perform an action for each line of the file in vim you can use the :%norm command.
For example, to append a dot to the end of each line you can use:

:%norm A.

To do the same to a range of lines (e.g. line 1-3) you can use:

:1,3 norm A.

Inserting special characters

In this way it is also possible to do execute a sequence of commands. Some commands such as insertions however, may require keys such as Escape and Enter. Such keystrokes can be inserted with ctrl+v followed by the keystroke.

Operations that change the number of lines

You may be tempted to do operations that change the number of lines.

For example, when you have three functions:

a()
b()
c()

And you want to turn each line into something like:

a()
{
}

You may think that using the following command will do that:

:1-3 norm A^M{^M}^M

It doesn’t, using it will give you something like:

a()
{
  {
    {
    }

  }

}

b()
c()

To get the correct result, you can use the g command which will execute a certain command for all marked lines that match a regular expression:

:1,3 g/^/norm A^M{^M}^M 

This gives the expected result:

a()
{
}

b()
{
}

c()
{
}

The reason why this works is that first all lines matching the expression are marked. (in this case ^ matches all lines) After marking, the command is executed for each marked line.

Sources

http://stackoverflow.com/questions/594448/how-can-i-add-to-the-end-of-each-line-in-vim
http://stackoverflow.com/questions/1678911/escape-character-in-vim-command

Thursday, January 26th, 2012 Uncategorized No Comments

Working with large bazaar repositories on slow filesystems

Bazaar is often able to handle even relatively large repositories within a reasonable amount of time when working on a local filesystem.

Mostly a repository can be kept on a local filesystem, but when working with virtual machines, it is sometimes desirable to have a build system inside the virtual machine and do versioning operations from the outside to benefit from up-to-date clients and graphical interfaces. When working on locations such as network shares, the communication overhead and lack of caching can be a real performance killer, especially when working with many small files.

When you only need work on a part of the tree you can use the view functionality of bazaar

$ cd Project
$ bzr view ./MyComponentA ./MyComponentB
Using 'my' view: Project/MyComponentA Project/MyComponentB

Now all versioning operations are restricted to the directories in your view and all files outside your current view are ignored which can give you a tremendous performance boost depending on how large your view is.

$ bzr st
Ignoring files outside view. View is Project/MyComponentA Project/MyComponentB
added:
  Project/MyComponentA/...
  Project/MyComponentB/...
  ...
modified:
  Project/MyComponentA/...
  Project/MyComponentB/...
  ...
Wednesday, January 25th, 2012 Uncategorized No Comments

Toggling HTML element visibility

Showing and hiding HTML elements is often done with div element. Many examples show toggling the style.display property between block and none. However, some types of elements such as table rows (tr elements) cannot be contained by a div. It is possible to put table into a tbody element though.

When toggling visibility of a tbody, be careful not to use the block display style, or you will get incorrect rendering. The default display style for these elements is table-row-group. When writing a function for toggling element visibility, it is not convenient to specify the display style explicitly every time. Instead, it is possible to revert to the default rendering style by setting the display style to the empty string (”).

Below you can see an example function to toggle visibility of HTML elements.

function toggleVisibility(toggleId) {
  var element = document.getElementById(toggleId);
  if(element.style.display == "none") {
    element.style.display = ''; // Display element with default rendering type
  }
  else {
    element.style.display = "none"; 
  }
} 
Saturday, January 7th, 2012 Uncategorized No Comments

Window snapping in Windows XP

WinSplit Revolution is an interesting alternative to the window snapping feature found in new versions of Windows and Gnome.
Especially when running Windows XP, this app can be very useful if you are used to window snapping.

Friday, January 6th, 2012 Uncategorized No Comments

SSHFS for Windows

Dokan SSHFS is a nice windows alternative to sshfs and FUSE.

To install Dokan SSHFA, first install the library and then unpack the SSHFS package. When starting the SSHFS tool you will be presented a user interface to set up your credentials and remote path.

Friday, January 6th, 2012 Uncategorized No Comments

Tampering with POST data to test Web user interfaces

Testing web user interfaces sometimes involves playing around with POST data sent to a web server. The Tamper Data addon for firefox allows you to interactively change data.

Just click “Start Tampering” before sending a request such as pushing a button and you will get an edit box to change the POST data before it is actually sent to the server.

Wednesday, January 4th, 2012 Uncategorized No Comments

Interactive debugging in Python

When programming, it is sometimes useful to interactively examine the situation at a certain breakpoint. One way to do this in python is as follows:

import code
... (some code) ...
code.interact(local=locals()) # Breakpoint where you like to interact with objects
Sunday, January 1st, 2012 Uncategorized No Comments