Sunday 22 July 2012

JavaScript Unit Testing.

(Thanks Mrs Pop for the image, would've asked first but then you wouldn't have seen it in context. If you want I'll take it down?)
My Coding is, if I do say so myself, bloody brilliant. I test and test and test but that testing isn't done in a formal manner and I'm coming to realise that this has got to change. I've been doing the odd bit of unit testing in SalesForce and Andy at work mentioned JsUnit last week when he suggested I write the coding standards for JavaScript, so I guessed I should learn a little about what it means.
I did a little unit testing whilst doing my Java course at the OU so I knew a little bit about the whole Test Driven Development thing. I also use the jQuery JavaScript extensively and I guess that that has already been tested extensively - at least I hope it has. So the only things left to test are the functions I write myself and JsUnit is perfect for that.
For a friend who lives and works in France I'm developing an invoicing system which has to read and write out French currency formats. For those that don't know: the French use a space for a thousand separator and a comma rather than a full stop for the decimal separator, they also have the Euro symbol at the end rather than at the beginning of the amount. So a million Euros and twenty-five Cents would be written out as “1 000 000,25 €". Complicated ehh?
So I've two functions: one which take a float and outputs it as a French formatted currency string and another that takes a French currency string and returns a float.
I won't bore you with the functions, needless to say they involve shed loads of conversions between numbers and string s and then splits etc...
I will bore you with how I tested them using JsUnit though.
I downloaded JsUnit from github and placed the extracted folder on my server. I then created the following file:
<!DOCTYPE HTML>
<html>
  <head>
    <title>Unit Tests for my Functions</title>
    <script src="./resources/jsunit/app/jsUnitCore.js"></script>
    <script src="./resources/js/toEuro.js"></script>
  </head>
  <body>
    <script>
      function testToFloat() {
        assertEquals("0,50 € should result in 0.5", 0.5, toFloat("0,50 €"));
        assertEquals("1 000 000,25 € should result in 1000000.25", 1000000.25, toFloat("1 000 000,25 €"));
      }
      function testToEuro() {
        assertEquals("0.5 should result in '0,50 €'", "0,50 €", toEuro(0.5));
        assertEquals("1000000.25 should result in '1 000 000,25 €'", "1 000 000,25 €", toEuro(1000000.25));
      }
    </script>
  </body>
</html>
I then opened up the included testRunner.html using my browser and pointed it at the file I created above and, after a little playing with the functions, managed to get the tests to pass.
It's an interesting approach to coding TBH but one that I think I could get used to it!
The people behind JsUnit now no longer use it but prefer Jasmine. I've had a wee squint but really don't have time to investigate it further this weekend. I code all week at work and most of the weekend at home and really need to do something else this afternoon. I'm 40% through Gary Gibson's The Thousand Emperors and can't justify buying Neal Asher's Zero Point until I'm done... and browsing for SodaStreams is doing my poor nut in.

Sunday 15 July 2012

Are The Archer's Anti-Traveler?

Caravan Boat image

There's reckoned to be a natural progression on BBC radio, You're supposed to start your listening experience with Radio 1 and then, as you mature, make your way up to Radio 4. I started with Radio 4 and had my choice confirmed for me when a nursing tutor suggested that if I wanted to be informed about what was happening in the world I should at least listen to Today every day.

I don't do this as much as I used to though as I'm no longer a nurse and I prefer to get my news either online via Reader or by listening to the No Agenda Podcast. That's not to say that I've totally given up on Radio 4. I turn it on when I'm cooking or eating and always first thing in the morning for a little fix of News. I even listen to The Archers, not religiously like. Just often enough to keep up with what's happening in Ambridge. I'll get lost every now and then though but I can always rely on my Ma to tell me what's been happening.

As an intermittent listener then I've been keeping abreast of the recent problems on Brookfield Farm (Have a wee squint 'ere: #farmageddon). I'm not totally up there with what's happening but I'm getting the gist and it's a wee bit concerning. David Archer is due to give evidence against a group of people who assaulted Adam Macy and left him in a coma after he tried to stop them stealing something from a farm...

That's all well and good and a reasonable attempt to liven up a show that's described as like real life - but more boring. But I'm not sure it's only me that sees this group of white van driving thugs as being an allegory for the problems faced by rural communities from Irish Travelers (Tony Martin anyone?). I've not heard an Irish twang in the threatening phone calls yet but I'm waiting.

What's more, I'm still not overly sure how I feel about it, that's if it isn't all in my head TBH. I've watched a couple of episodes of My Big Fat Gipsy Wedding, and I'm not sure how I feel about that either (I do watch an awful lot of TV for someone without a TV). I'm just not sure and I vacillate between how I feel about Irish Travelers but I'm conscious that they are in a vulnerable position in terms of the bigotry that's always just bubbling under the surface in the UK.

As an aside: I loved The Riches and would advise everyone to watch it.

As a second generation lapsed Catholic and a Bargee I'm even more conflicted. I'd have thought that the similarity of life-styles between Travelers and Bargees would've meant that we shared something but I think that I might be mistaken. I guess the enemy of your enemy isn't necessarily your friend - not that I see people in houses as the enemy. I've been called a "Water Gypsy" before and, while I was away with the fairies pondering the bucolic lifestyle of a boater with a nice camp-fire going in the twilight in a meadow beside a gentle canal, the chap hastily retracted it thinking I was away with the demons pondering GBH after being so insulted. Before I moved aboard there were some problems with Travelers and the boating community at Midsummer Fair. Even now there is a mass exodus of boats from Midsummer Common when the fair is due. I still remember seeing boats with wooden covers over their windows to protect the glass from projectiles.

This conflict is only exacerbated by a close run in I had with being a New Age Traveler in my youth. If my Ma hadn't put her foot down with such a firm hand I would have been on my way down to Glastonbury in the back of a smelly truck and sleeping in a Bender. The call of the road was loud but the fear of my Ma was ever so much greater ;-).

Interesting ehh? But like some sort of grotesque icing on the cake there is the issue of slavery. The case is interesting in that the people enslaved were "recruited" from the streets... some in the media were outraged but not so much that they could think of ways of providing homes for the homeless. So yeah, I'm conflicted, I'm also aware that one really shouldn't tar every member of a minority with the same brush - á la the "Water Gypsy" comment above. Indeed, so reticent am I to acknowledge membership of any other minority other than Yorkshireman I'm wary of even saying that I'm a Bargee. I know what happens to minorities historically and I don't want to be herded into a cattle-truck any time soon.

So what's to do? We go France at least annually (How very middle-class of us ehh?) and they've taken a rather drastic approach to the issue of Travelers, they've ejected them! I'm not sure that that approach is possible and I'm certainly sure that it's not one I'd suggest. There does seem to be animosity bubbling under the surface though and I'm pondering how long it'll be before Cameron et al decides that their campaign to make the poor the villains in the current economic crisis is a wash-out and decide to alter their focus to a target even more hated by the middle-class of middle England. A minority that is insular and doesn't pay taxes and has little or no respect for the laws-of-the-land perhaps shouldn't be tolerated in that land? That was ironic by the way; I'm more than sensitive to allegations of racism. I guess that's the joy of being a Yorkshireman, we hate everyone equally and don't discriminate, the only exception being that the only people we hate more than everyone else is each other or ourselves!

Is #farmageddon the opening salvo in a pseudo class war? And it could be that the animosity has always been there, I admit. Being a Yorkshireman I only ever heard the word "Pikey" when I was visiting relatives down South in my youth. I had absolutely no idea what the word meant and it was only when I moved down here that I clocked the sheer level of animosity. I guess there must have been reason for the animosity but I'll be blown if I know what the cause was, it seems to be just that way. I hear all sorts of stories but apart from the odd run-in with Traveler youth - no more than run-ins with non-traveler youths TBH - I'm unsure as to the rationale for the animosity - unless it's just the English animosity to the strange?

Goodness me this is a long post! I beg forgiveness but this is an issue that's been percolating for a while and I've got all sorts of conflicted feelings about it. I'm glad to have this forum in which the air them though so I'd welcome your thoughts/comments.

Thursday 5 July 2012

JSRender Rocks!

var newCustomer = $("<div></div>", {"class": "customer row-fluid", "data-id": data.msg}).appendTo($("#customers"));
var newName = $("<div></div>", {"class": "row-fluid thingTop"}).appendTo(newCustomer);
newName.append($("<div></div>", {"class": "span6"}).html("<strong>Name:</strong>"));
newName.append($("<div></div>", {"class": "span6 name", "text": data.name}));
var newCompany = $("<div></div>", {"class": "row-fluid thing"}).appendTo(newCustomer);
newCompany.append($("<div></div>", {"class": "span6"}).html("<strong>Company:</strong>"));
newCompany.append($("<div></div>", {"class": "span6 company", "text": data.company}));
var newStreet = $("<div></div>", {"class": "row-fluid thing"}).appendTo(newCustomer);
newStreet.append($("<div></div>", {"class": "span6"}).html("<strong>Street:</strong>"));
newStreet.append($("<div></div>", {"class": "span6 street", "text": data.street}));
var newTown = $("<div></div>", {"class": "row-fluid thing"}).appendTo(newCustomer);
newTown.append($("<div></div>", {"class": "span6"}).html("<strong>Town:</strong>"));
newTown.append($("<div></div>", {"class": "span6 town", "text": data.town}));
var newPhone = $("<div></div>", {"class": "row-fluid thingBottom"}).appendTo(newCustomer);
newPhone.append($("<div></div>", {"class": "span6"}).html("<strong>Phone:</strong>"));
newPhone.append($("<div></div>", {"class": "span6 phone", "text": data.phone}));

So the above is what I used to do on a mates web application when she entered a new user to her system. I just knew there must be a better way though so I got to looking and looking and looking and saw something about jQuery Templates but then saw that they'd fallen out of favour. I think that that is a shame but they've been replaced with JsRender - which just rocks!

All of the code above ended up being replaced with this:

$("#customers").append(
    $("#addCustomer").render(data)
);

Of course I had to add this to the base page:

<script id="addCustomer" type="text/x-render">
    <div class="row-fluid customer" data-id="{{>id}}">
        <div class="row-fluid thingTop">
            <div class="span6"><strong>Name:</strong></div>
            <div class="span6 name">{{>name}}</div>
        </div>
        <div class="row-fluid thing">
            <div class="span6"><strong>Company:</strong></div>
            <div class="span6 company">{{>company}}</div>
        </div>
        <div class="row-fluid thing">
            <div class="span6"><strong>Street:</strong></div>
            <div class="span6 street">{{>street}}</div>
        </div>
        <div class="row-fluid thing">
            <div class="span6"><strong>Town:</strong></div>
            <div class="span6 town">{{>town}}</div>
        </div>
        <div class="row-fluid thingBottom">
            <div class="span6"><strong>Phone:</strong></div>
            <div class="span6 phone">{{>phone}}</div>
        </div>
    </div>
</script>

But what the hey, at least I'm keeping my markup where it needs to be, on the page. Who cares if it's something of a fudge...? I dare say I will soon, but it'll do for now ;-)

Monday 2 July 2012

Android Emulator for Development with Arcus Global

At work we've been doing a fair bit of Android development of late at work and I was right naffed off with waiting for the Android Emulator to boot up each and every time I needed to test something (I do so wish I could sort out in my head what I need to do in one fell swoop but I generally plan like a loon and then implement a little bit at a time).

I installed BlueStacks and tried that and it was bloody brilliant, but I hadn't checked that it was great before-hand so I had to justify my choice (I'd tried it before you see at home) so I went through a process defined by a colleague and I documented the result on the work blog. I think it's well worth a read...

I hope you like the picture on the blog, it was taken on the way back from getting the boat blacked at the Lazy Otter on the 18th of June, 2012.