Thursday, June 27, 2013

Jasmine: A JavaScript Testing Framework

I have been experimenting more with JavaScript frameworks lately. Being fond of the Test Driven Development methodology, I came across Jasmine. Here is how the site defines it: Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
If you have not already, download Jasmine from http://pivotal.github.io/jasmine/
After you decompress the downloaded file you see this directory structure.

As you can see, the SpecRunner.html file is your user interface that is simply run from a web browser. The spec folder will hold, as you guessed, your specification code—what you want your implementation to do. The src folder will contain the code you are testing.
The JavaScript includes in the SpecRunner.html file need to be set as such:

What I wanted to do was to test how to sort a JavaScript array based on a date element in the array. So let’s start with the spec file. I named my spec DateSortSpec.js and placed it in the spec directory. Here is the DateSortSpec.js:

Next, the source code file,DateSort.js, goes in the scr directory:

OK, with basic structure in place let's do a sanity check and make sure we get a failing test result:

Now let’s get the “should show Health for a Friend title with ascending sort” spec to pass. First, create a function that will sort ascending by date. What I did was to utilize the JavaScript array.sort method passing in a compare function.

Now reload the browser and the spec passes.

Next we will add the spec to sort descending.

Reload the browser and we see the spec fail as we have not implemented any code for the spec.

So, the next step is clear. Implement enough code to pass the spec. Here I again use the JavaScript array.sort method passing in a function for a descending comparator.

Refresh the browser again and bang!

As you can see using Jasmine is pretty straight forward. Setup is a matter of expanding the compressed file. Moreover, if one has a web browser and text editor you a ready to code the specs and source code.

Wednesday, June 26, 2013

Configuring SciTE for JavaScript on Ubuntu 12.04

At work I use the SciTE editor quite a bit. With that stated, at work we use Microsoft Windows XP, soon to move to Windows 7, as the standard operating system. In any event, during a break I created a JavaScript test file to determine the best way to sort via a JavaScript Date.

All well and good at this point. Then I brought the JavaScript file home where my entire house has nothing but Ubuntu as the choice of operating systems for both desktops as well as laptops and netbooks. I quickly realized that I did not have my SciTE editor configured to execute JavaScript files on my Ubuntu desktop. Doing some quick poking around I found that I could use my installation of Node.js for the JavaScript engine. So, here is what I did.

First, I opened the /usr/local/scite/cpp.properties file. I then noted that at line 436 it was looking for the GTK cross platform tookit in the for of: if PLAT_GTK
I added the following config setting below that line:
command.go.*.js=node $(FileNameExt)

I then closed the /usr/local/scite/cpp.properties, open the SciTE editor, opened the JavaScript file, hit the F5 key to run the file and I was good.



Monday, June 24, 2013

What daily apps and devices do you use to help you daily?

For me, my day consists of a daily run, a time of scripture reading and prayer, eating breakfast, getting out the door and to work, and then the normal work day events, and then head back home.


For various steps in the events of the day, I use my Garmin watch, iPod Shuffle, fitbit, desktop computer, and smartphone. With these devices I use apps for the weather, spiritual and physical health, basic communications, and traffic for my daily commute. Here are the day’s events with the devices and the apps that are used.


Wake up:
  • Digital clock for the alarm and my smartphone in bedside mode for the time.


Morning Run:
  • Garmin watch to track time and pace
  • fitbit pedometer to track number of steps
  • Desktop computer, with Ubuntu 12.04 as the operating system, looking at weather.com and runkeeper.com to map and store the day’s run stats
  • Ipod Shuffle with iTunes to download and organize the theology based podcasts and music that I listen too


Reading of scripture and prayer time
  • Desktop computer, smartphone, or Kindle Fire looking my personal prayer app built with Ruby on Rails and housed on heroku.com


Morning communications
  • Desktop computer, smartphone, or Kindle Fire using gmail, Google calendar, facebook, and twitter


Breakfast
  • Record nutrition intake with fitbit.com that has already uploaded the calories burned from the earlier run.


Morning commute to work
  • Desktop computer, smartphone, or Kindle Fire using ohgo.com to see the current status of the traffic on I75 and I471 North bound. From there I determine which is the best route.


Work
  • Desktop PC for software development
  • Smartphone for personal communications
  • Fitbit in which I note time to time the number of steps taken during the day


Lunch
  • Record nutrition intake with fitbit.com.


Evening commute to work
  • Desktop computer or smartphone using ohgo.com to see the current status of the traffic on I75 and I471 South bound. From there I determine which is the best route.


Evening
  • Desktop computer, smartphone, or Kindle Fire using weather.com to note the evening and next morning’s forecast.
  • Fitbit from which the day’s step count is uploaded and the amount of calories burned is compared to the day’s nutrition intake.
  • Desktop computer, smartphone, or Kindle Fire using gmail, Google calendar, facebook, and twitter
  • Roku streaming player, a cloud based video streaming device that interfaces with content providers such as Netflix and/or Amazon Prime, to watch great TV series and movies such as Dr. Who, Star Trek, The Goonies, Shaun the Sheep, etc.

Of course this list does not include other devices and personal tools such as the automobile or other home based appliances that rely on silicon-based technology to function. The pattern that I see is that my day is better organized and runs smoother as the result of the use of these devices/apps. Moreover, I am better informed off my health as well as keeping better track of appointments and obligations.