24 lokakuuta, 2014

Part 3 Testing the WorkBench


Basic testing

Testing if the 'strict mode' is really on:

When in 'strict mode', the value of this inside any function is 'undefined',  not the global (window Object) as it used to be before strict mode. 
This means that we can test if the mode is on easily inside any basic function. 
If this is true (global window object) strict mode is not on. If undefined = false, it is on.
Try following code in your workbench:

var verify = function verify(test, msg) {
    if (!test) { 
       put("Debugger:\n" + msg); 
     }; // if 
}; // verify = self made unit test


var strictMode=function (){ // will return Boolean
        return !this;
} // strictMode(); 


verify(strictMode(), "strict mode: " + strictMode()); 
    // testing strict mode function with unit test


There is small unit test function verify readymade, you can use it like this:

var testCondition = false; // any boolean test here
var messageToUnitTest = "Hello: Unit Test -test" 
  // any message here, just to tell you what is wrong:
verify(testCondition, messageToUnitTest);

You will get alert:

Err: 
Error: Debug: Hello: Unit Test -test
Url: 
 file:///Users/who/Sites/js/barebones/main.js
Line: 21 / 34

Alert will tell you your message, (after the word "Debug:") plus: 
1: Type of the error, if there is one. Usually just Error:
2. URL of the file where the code is hidden.
3. Where, on which line / character the error happens

If test is ok, unit test will not say or do anything, it will be happy and you can forget it. Usually this is what to expect. No windows popping all the time, only if something is wrong.

Leave many lines of
verify(testCondition, messageToUnitTest);
into your code, and if something unexpected happens, you know that one of them will warn you. For example if strict mode is not on anymore, that line will save you immediately and you don't have to wonder hours when something just isn't the way it should be... but what...

This is the way to go. First make a test, then make actual code and you will have tested code all over the place. If you then go and change the old code, your tests will run and hopefully notice if something nasty is about to happen. You shall notice it right away on the spot, and so you can fix it in seconds, not next week or someday.

You should also test that your testbench -functions work properly. Spend time in basics and make them the foundation of everything else.

Try. Let's comment the 'strict' - text out for a moment, like this:

var xyz = function () { 
// 'use strict'; // this is wrapper start ...

Now you should see some warnings concerning about 

  Err: 'strict mode': false... 

Go ahead and check it out, just remember to remove the comments back after testing...
Now we have testbench to code, we can start.

Next page is about building the minimum app and some 
basics of OOP JavaScript in strict mode. Nervous? Now the real thing starts!