Mini JS Tester

by lid0

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<div id="test_debug_console"></div>

JavaScript

/**
Author: Lidlanca
A simple Unit Test

Usage

Create a Unit
---------------------------------
var unit1 = Test.it( "hello world" ).name( "test if world says hello" );


Supported tests 
---------------------------------
assert(expected_value)
property_assert(expected_value,property)
contains(expected_value)
be_kind_of(kind)

methods
---------------------------------
setFlag()
set a flag to a given value, can be set in the call chain.

Supported Flags
STOP_ON_FAIL    if on will skip any test call if there is ANY failur on the unit
                the flag can be turn on/off any where in the chain and it can be set multiple times.
                skipped tests will be logged

default:        off


chain tests
---------------------------------
unit1.assert("hello world").contains("hello").unit1.debug();


Todo:
---------------------------------
imporve output format.

**/

TestUnit = function (val){
    this.value = val;
    this._name = "";
    this.errors = 0;
    this.skipped = 0;
    this.msg = [];

    this._stopOnfailur = false; 
    this.flags = {"STOP_ON_FAIL":'_stopOnfailur'};
    
    this.setFlag = function(f_name,value){
        if ( this.flags.hasOwnProperty(f_name) ){
            this[ this.flags[f_name] ] = value;
        } else {
            console.warn("Unit:", this.name, "none existing flag" , f_name);
        }
        return this;
    }
    
    this.beforeTest = function(){
        if( this.errors > 0 && this._stopOnfailur){
            this.skipped += 1;
            return false;
        } else {
            return true;
        }
    }
    this.assert = function (expected_value){
        if (!this.beforeTest()) return this;
        if (this.value == expected_value) {
           
        } else {
            this.errors += 1;
            this.msg.push( "assert: given \'" + this.value  + "\' expected '" + expected_value + "'" );
            
        }
        return this
    }
    this.equal = function (expected_value){
        if...