Play that $H!T back!

Why hello there, it’s me, the one and only mediocre coder at best. So I’m back, but this time, with something a little different, something I have actually never seen before in my life. So this ladies and gentlemen, will be a first for me, lets go through it together!

So recently, I was given the task to write a test case for ECMAScript’s reverse() function. I know, huh? Shocking! But believe it or not, this was actually a lot easier than I thought it would be. It was actually kinda fun. Rather than just talk about it, let me show you.

Where it all began

Take a look at this! Now at first you might not know what to think, or you might be a little confused, but don’t worry, I was too! That my dear reader is what happens when you call array.reverse() on a function in JavaScript. Look at all those steps it takes, and all those checks it makes. To be quite frank, I am still very lost on what those things actually do. Like, I understand the English of it, I really do, but what does any of it actually mean? For instance, what does “3. Let len be ToLength(Get(O, “length”)).”, honestly, what does this even mean? I’ve been looking at it for a while now, but I just couldn’t seem to figure it out. But nonetheless, I had some tests to write, and I needed a way to write them.

Thankfully, my professor was kind enough to point me in the right direction. Now I can point you there too! Click me! There you have a Github repo that has quite a number of tests already written for the array, prototype and reverse tests. After reading a few of them, I managed to grasp the idea of how tests for the reverse() function was done.

My own test

With a bit of luck and a lot of reading, I had the follow

// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
info: >
The elements of the array are rearranged so as to reverse their order.
The object is returned as the result of the call
esid: sec-array.prototype.reverse
description: Checking this algorithm, elements are objects and primitives
var x = ["1", "NaN", undefined, "Jimmy", "Abdul"];
// x[0] = true;
// x[2] = Infinity;
// x[4] = undefined;
// x[5] = undefined;
// x[8] = "NaN";
// x[9] = "-1";
var reverse = x.reverse();
// CHECK#1
if (reverse !== x) {
$ERROR('#1: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse() === x. Actual: ' + (reverse));
// CHECK#2
if (x[0] !== "Abdul") {
$ERROR('#2: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse(); x[0] === "Abdul". Actual: ' + (x[0]));
// CHECK#3
if (x[1] !== "Jimmy") {
$ERROR('#3: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse(); x[1] === "Jimmy". Actual: ' + (x[1]));
// CHECK#4
if (x[2] !== undefined) {
$ERROR('#4: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse(); x[2] === undefined. Actual: ' + (x[2]));
// CHECK#5
if (x[3] !== "NaN") {
$ERROR('#5: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse(); x[3] === "NaN". Actual: ' + (x[3]));
// CHECK#6
if (x[4] !== "1") {
$ERROR('#6: x = [ "1", "NaN", undefined, "Jimmy"]; x[0] = 1; x[1] = NaN; x[2] = undefined; x[3] = Jimmy; x.reverse(); x[4] === "1". Actual: ' + (x[4]));

As you can see, it’s a very simple test, really. Have an array, reverse it, and see if the reversed array is actually reversed!

Now you may be wondering, why did I split up all the tests like that? I could have done it all in one line, or better yet, the first line itself would have probably been enough. And to that, I say, “Yes, you’re right. However, that doesn’t provide easy debugability (yes I know it’s not a word!)” See, what I mean by that is this, it’s easier in the long run to debug this test or any part of this code by splitting it up into multiple parts. Think of it as object oriented programming. You create smaller bits of your bigger program to work with one another, and if one bit breaks, you know exactly where to look to try to fix it. So quite frankly, I like this idea of testing, I think it’s a really good approach and though it requires a lot more time, I think the headache it saves you in the long run makes it worth it.


And yes, in case you’re wondering, my tests did pass!



Now though this wasn’t a big project like the previous things I’ve worked on, I honestly did enjoy it and I learnt something new, which is always the aim here! What I ended up learning was how top level programmers write tests for things that seem so simple. I mean look at the function, all it does is reverse an array, it’s really nothing fancy. Yet when you look at the code that is written to make sure that said functionality works and intended, it really opens your mind to different ways you can view code, different ways you can write the same code, and different ways you can present the same information!

I’d like to thank you for taking the time to read this far. If you have any questions, please don’t hesitate to leave them in the comments section!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s