Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Unit Test for JavaScript

, 14 Dec 2007 CPOL
How to strengthen your client-side code and improve reliability
unitjs.zip
UnitJS
green.gif
red.gif
//Shortcuts to debuger helper class to maitain Console.Out sintax
function _Out()
{
    this.Write = function(ex, url, lastModified, referrer, args)
    { 
        $dm(ex, url, lastModified, referrer, args);
    }
    this.WriteResult = function(cssClass, ex, url, lastModified, referrer, args)
    { 
        debugHelper.$Write(cssClass, ex, url, lastModified, referrer, args);
    }
}
function _Console()
{
    this.Out = new _Out();
}
var Console = new _Console();

//Main assertion class
function _Assert()
{
}

//Assertion method
_Assert.prototype.That = function(leftArg, iConstraint)
{				
    //Check constraint
	var result = iConstraint.Check(leftArg);
	//Extra information that you might want
    var url = null; //document.URL;
    var lastModified = null; //document.lastModified;
    var referrer = null; //document.referrer;
    var args = null; //arguments;
		
	//Plot the result in the output window
	if( result )
	    Console.Out.WriteResult("UnitJsPass", "Success for left argument: " + leftArg, url, lastModified, referrer, args);
	else
    	Console.Out.WriteResult("UnitJsFailed", "Failed for left argument: " + leftArg, url, lastModified, referrer, args);
}

//Create an instance of the assertion class
var Assert = new _Assert();

//Interface used for assertions
function IConstraint()
{
	this.rightArgumemt = null;
}
IConstraint.prototype.Check = function(leftArg)
{
    //Inherit from this interface and implement your logic check
	return false;
}

//EqualTo functionality
function _EqualTo(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_EqualTo.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_EqualTo.prototype.constructor = _EqualTo;
	        
//Override the Check method
_EqualTo.prototype.Check = function(leftArg)
{
	return leftArg == this.rightArgumemt;
}

//NotEqualTo functionality
function _NotEqualTo(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_NotEqualTo.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_NotEqualTo.prototype.constructor = _NotEqualTo;
	        
//Override the Check method
_NotEqualTo.prototype.Check = function(leftArg)
{
	return leftArg != this.rightArgumemt;
}

//GreaterThan functionality
function _GreaterThan(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_GreaterThan.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_GreaterThan.prototype.constructor = _GreaterThan;
	        
//Override the Check method
_GreaterThan.prototype.Check = function(leftArg)
{
	return leftArg > this.rightArgumemt;
}

//GreaterThanOrEqualTo functionality
function _GreaterThanOrEqualTo(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_GreaterThanOrEqualTo.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_GreaterThanOrEqualTo.prototype.constructor = _GreaterThanOrEqualTo;
	        
//Override the Check method
_GreaterThanOrEqualTo.prototype.Check = function(leftArg)
{
	return leftArg >= this.rightArgumemt;
}

//LessThan functionality
function _LessThan(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_LessThan.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_LessThan.prototype.constructor = _LessThan;
	        
//Override the Check method
_LessThan.prototype.Check = function(leftArg)
{
	return leftArg < this.rightArgumemt;
}

//LessThanOrEqualTo functionality
function _LessThanOrEqualTo(rightArg)
{
	this.rightArgumemt = rightArg;
}
//Javascript inheritance. It is similar to implementing a interface.
_LessThanOrEqualTo.prototype = new IConstraint();
//Javascript hack to maintain the correct constructor
_LessThanOrEqualTo.prototype.constructor = _LessThanOrEqualTo;
	        
//Override the Check method
_LessThanOrEqualTo.prototype.Check = function(leftArg)
{
	return leftArg <= this.rightArgumemt;
}

//Create Is class comparer
function _Is()
{
	this.EqualTo = _EqualTo;
	this.NotEqualTo = _NotEqualTo;
	this.GreaterThan = _GreaterThan;
	this.GreaterThanOrEqualTo = _GreaterThanOrEqualTo;
	this.LessThan = _LessThan;
	this.LessThanOrEqualTo = _LessThanOrEqualTo;
}
var Is = new _Is();

//Text fixture interface
function ITextFixture()
{	            
    this.Description = "Text fixture.";
    this.Category = "none";
    this.StartTime;
    this.Tests = new Array();	            
}

//Set of functions that are performed once prior to executing any of the tests in the fixture
ITextFixture.prototype.TestFixtureSetUp = function(){}
//Set of functions that are performed once after all tests are completed
ITextFixture.prototype.TestFixtureTearDown = function(){}
//Common set of functions that are performed just before each test method is called
ITextFixture.prototype.SetUp = function(){}
//Common set of functions that are performed after each test method is run
ITextFixture.prototype.TearDown = function(){}
//Add a new test case
ITextFixture.prototype.AddTest = function(test)
{
    this.Tests[this.Tests.length] = test;
}
//Clear test cases
ITextFixture.prototype.Clear = function()
{
    this.Tests.length = 0;
}
//Test cases to be run
ITextFixture.prototype.Run = function()
{
    var reName = new RegExp("function (.+)\\(");
    var testFixtureName = this.constructor.toString().match(reName)[1];
    Console.Out.Write("Starting " + testFixtureName);
    Console.Out.Write("Description: "+ this.Description);
    Console.Out.Write("Category: " + this.Category);

    this.TestFixtureSetUp();
    for(i in this.Tests)
    {
        var test = this.Tests[i];
        this.SetUp();
        test();
        this.TearDown();
    }
    this.TestFixtureTearDown();
}

//Unit.js engine class	        
function _UnitJs()
{
    this.TestFixtures = new Array();
}

//Add a test fixture to the collection
_UnitJs.prototype.AddTextFixture = function(testFixture)
{
    this.TestFixtures[this.TestFixtures.length] = testFixture;
}

//Run all fixtures
_UnitJs.prototype.Run = function()
{
    for(i in this.TestFixtures)
    {
        var iTestFix = this.TestFixtures[i];
        iTestFix.Run();
    }
}

//Create instance of unit.js engine
var UnitJs = new _UnitJs();

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Roberto Colnaghi
Software Developer
United States United States
I'm a passionate developer and videogame player.
Been in touch with Objective-C, Javascript, C#, C, Guild Wars 2, Tera and many more.
 
Javascript is one of my favorite languages.
Follow on   Twitter   Google+

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 15 Dec 2007
Article Copyright 2007 by Roberto Colnaghi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid