Tuesday, the 30th of November, 2010

Unit testing Cocoa code with MacRuby

Announcing RCRunner, a GUI test runner for MacRuby and Cocoa.

Cocoa unit testing can be a pain. In addition to the usual difficulties of writing tests for user interface heavy code, the Apple sanctioned solution, SenTestingKit, can isn't the greatest testing framework around and the default way of using it rules out debugger.

GHUnit helps somewhat. It's a GUI test runner with additional testing methods. However, with it you are still writing your tests in Objective-C. On the plus side it's the same language you're probably writing your app in. On the minus side Objective-C can be verbose and sometimes, especially when writing test code, brevity would be welcome.

Enter MacRuby. You get the conciseness of Ruby with full access to your Objective-C classes. And Ruby probably has the greatest density of testing frameworks per active programmer among all the languages in popular use today.

There's a nice article about TDD, Objective-C and MacRuby on the MacRuby site. However, the approach taken in it still uses a Xcode build phase script to accomplish testing. That makes debugging hard and you have to hunt through the build logs for your errors.

RCRunner is a separate GUI program you run. You tell it names of Ruby modules and it uses any test cases it finds[1]. You can breakpoint your code and thanks to Ruby, reload the test code. You can inspect errors and log output test by test.

Enjoy.

[1] At the moment it supports only MiniTest but adding support for other frameworks isn't difficult.

@ 20:45 +0200 [ ] archived

Wednesday, the 10th of November, 2010

Using Firefox as Flash playing Safari fallback

If you want to go Flashless on Mac and Safari, it's possible to use Firefox as a fallback, too, not just Chrome. While Firefox does load Plugins from /Library/Internet Plug-Ins and ~/Library/Internet Plug-Ins, it looks in other places too. I just tested and it seems to work fine from ~/Library/Application Support/Firefox/Profiles/<profile name>/plugins and I suspect /Applications/Firefox.app/Contents/MacOS/plugins would work too.

So you can copy Flash Player.plugin, NP-PPC-Dir-Shockwave and flashplayer.xpt to one of the Firefox specific folders and launch Firefox from Safari's Developer menu. Chrome starts up faster, though.

@ 18:11 +0200 [ ] archived

Saturday, the 19th of June, 2010

xibgraph: Interface Builder overviews

When putting together user interfaces with Interface Builder, you connect things together with bindings, actions and outlets and it's good. Understanding the result later on is a completely different matter. It can be time consuming and difficult to browse the objects inside one by one, trying to comprehend the whole. Even more so if you're trying to read someone else's work.

Out of that frustration came xibgraph. It takes a XIB file and outputs the connections contained inside:

Example xibgraph output

At the moment it supports bindings and actions. Outlets are next.

xibgraph supports a couple of different output formats. JSON is supported out of the box and if you install pydot, you get DOT, the format understood by Graphviz and OmniGraffle too.

It hasn't been tested on a particularly wide variety of XIBs, so it's very plausible it will produce wonky results or just outright refuse to work with your files. If so, patches and bug reports are welcome.

xibgraph is MIT licensed and written in Python. It requires PyObjC (it seemed like the easiest way to get XPath support on OS X) and probably Python 2.6. Everything but the DOT support should work without additional requirements on OS X 10.6.

@ 19:17 +0300 [ ] archived

Friday, the 4th of June, 2010

hg-status-sections

I usually use Murky, dvc or some other shell for Mercurial. Not always though, for various reasons, and when running hg status I'm always frustrated when copy and pasting file names. bzr provides neat, non-cluttered lines that can be copied whole to get a file name without a hassle, but hg takes the traditional one-character prefix approach to status display and as a result makes you manually select a part of a line instead of just grabbing a whole line.

So I wrote a small extension to help. Meet hg-status-sections.

@ 20:14 +0300 [ ] archived

Saturday, the 22nd of May, 2010

A better @synthesize

The biggest problem with Objective-C's @synthesize directive for properties is how difficult it's to augment the synthesized code. You often need to add logic to a property setter, but while you're adding it, you're losing the probably correct implementation Apple's code creates for property flags like atomic and retain.

At the moment, when you synthesize a readwrite property called foo you get a setter method called setFoo. If you need to add logic around it, you can either store the value in a private property and add a public property with a different name or use a subclass. Both are a bit of a hassle. Usually I just end up writing my own method, including the logic for implementing the modifiers correctly.

In an ideal world the language would support something like around/before/after methods in CLOS, but those features are rare. There's a simple way @synthesize could make things easier without requiring massive changes to the runtime. It could give you for both the getter and setter two methods. There would be the public methods they create in the current implementation, but there'd also be methods with names like __synthesized_property and __synthesized_setProperty. The public methods would rely on the semi-private methods to actually implement all their logic. Then if you needed to add logic around the accessors, you could override the public methods and call the semi-private ones to get access to the synthesized accessor logic without jumping through hoops or risking getting the implementation wrong.

@ 14:03 +0300 [ ] archived

Monday, the 8th of March, 2010

Block indentation in Emacs

There are several small things Emacs could be doing to make it nicer to write code. One I was missing was making it possible to go with one press of the return key between braces in a C derived language from this:

if (test) { }

to this:

if (test) {
   <-- insertion point here
}

That is, pressing return before the closing parentheses, brace or bracket should move the closing character two lines down, indent it properly, and move the insertion point to the new empty line in the middle and indent it property. The way TextMate does it.

Here are a few of elisp functions to accomplish this:

(defun char-isws (c)
  "Is character c a whitespace character?"
  (or (char-equal c ?\ )
      (char-equal c ?\t)
      (char-equal c ?\n)))

(defun line-next-non-ws ()
  "Return the next non-whitespace character on the current line or nil."
  (let ((cc (char-after)))
    (if (and cc (char-isws cc))
        (save-excursion
          (if (re-search-forward "[^[:space:]]" (save-excursion     (end-of-line) (point)) t)
              (char-before)
            nil))
      cc)))

(defun newline-and-indent-extra-for-closing-paren ()
  "Insert a newline and indent. If the next non-whitespace character is a closing paren, insert two newlines and indent the two  new lines correctly, placing the point on the first of the two new lines."
  (interactive)
  (let ((nc (line-next-non-ws)))
    (if (or (null nc)
            (not (= (char-syntax nc) ?))))
        (newline-and-indent)
      (progn
        (just-one-space)
        (newline-and-indent)
        (newline-and-indent)
        (previous-line)
        (indent-for-tab-command)))))

Now you can bind return to newline-and-indent-extra-for-closing-paren in a suitable language keymap. I've been using this with scala-mode and it works well there.

@ 19:28 +0200 [ ] archived