Monday, September 10, 2012

CoffeeScript keeps crashing on me

I am a big fan of CoffeeScript, I think it is the syntax that Javascript should have. The problem I have is this, the compiler keeps crashing on me. I generally set it up with the "--watch" command so that it will keep track of my files and recompile as I go, which is nice. Except when it crashes and I spend 5 minutes wondering why my new code is not showing up.

I had originally thought to write an erlang app to fix this, but then I realized that I need something much simpler, a shell script will do:

(yea I suck as shell scripts) But it does the job. It runs the compiler and if it should exit well then bash will just restart it without bothering me.

When in doubt have the computer do the boring and repetitive stuff!

Sunday, September 9, 2012

Mixed in with all the other programming stuff I am doing I am going to be teaching my Step Daughter to code. So we will be working threw "How to Design Programs" which should be fun  so expect to see some scheme here intermixed with the CoffeeScript, Erlang and whatever else runs across my desk. Look for it under tag "scheme"

Thursday, September 6, 2012

A Mock Sync For Backbone.js

If you want to do unit tests on a backbone.js application it would be really nice if you could replace the default sync with one that will not make an AJAX call but will return prebuilt data so you can test the application.

This is very easy to do. What you need to do is to replace the default Backbone.Sync object with one that will pull prebuilt data. This is what I have been using so far. It just takes an existing table of mock data and when a request comes in uses the URI as a key to retrieve the data and resets the data for the collection.

Obviously this only works for reading data, for Update, Delete etc things get a little more complex but there is no reason to not be able to expand this approach.




Wednesday, September 5, 2012

Running QUnit on Save with PhantomJS


I have started doing TDD in my CoffeeScript using QUnit. This is pretty amazing really and it has been making me pretty happy. However there is still the very repetitive cycle of save, switch to a browser run the tests and switch back to emacs. What would be amazing is if I could get emacs to do everything for me. After all I should be able to program my system to do the boring repetitive stuff 

It turns out that this is not even that hard, but it does take several parts. 

First of all we need a browser to run the tests. We could use Selenium (which I rather like actually for other things) but even better would be to use PhantomJS which is a headless browser.

What PhantomJS is is a browser based on WebKit (which is also used by Chrome and Safari) that does not display anything. But you can interact with it via JavaScript. So what I want to do is launch my web page (which is on a local web server) then have QUnit run and return the results. Thankfully the PhantomJS folks have a script do just that in the form of a QUnit Driver. (They also have one for Jasmine if you like that).

So now I can call PhantomJS like this:

phantomjs run-qunit.js http://localhost:8080/test.html

I can have it load up my page and return the results of qunit:

'waitFor()' finished in 407ms.
Tests completed in 222 milliseconds.
80 tests of 95 passed, 15 failed.

It gives me a result like that. Which is nice, it tells me exactly what I want to know, if everything passed or not. So what I want is for emacs to run it when I save a file, to do this I run this bit of emacs lisp. 



What I would really like for future is to change it so that if everything passes it will give me a line
that says "203 tests pass" or the like and if all tests do not pass then it should open a buffer to tell me exactly which tests failed. (Maybe even try to figure out which file the failing code is in to bring me to the right spot to fix it )