Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Create cajaunit dir & add files

  • Loading branch information...
commit 61ae756c9776c9a80e4a4e4beb6d07a5b6ca1a2f 1 parent 7a20845
authored November 27, 2011
133  cajaunit/cajaUnit.js
... ...
@@ -0,0 +1,133 @@
  1
+
  2
+var foreach = function(collection, callback){//a convenient loop pattern
  3
+		if(collection && collection.length){//array or node list
  4
+			for(var i = 0; i < collection.length; i++){
  5
+				callback(i, collection[i]);
  6
+			}
  7
+		}else if(collection && collection.hasOwnProperty){//object
  8
+			for(var key in collection){
  9
+				if(collection.hasOwnProperty(key)){
  10
+					callback(key, collection[key]);
  11
+				}
  12
+			}
  13
+		}else{
  14
+			throw('foreach() error: collection (' + collection + ') is neither an array nor an object');
  15
+		}
  16
+	},
  17
+	cajaUnit = function(){
  18
+		//define global settings
  19
+		var settings = {
  20
+			'outputId':'output',//the dom root to append all output to
  21
+			'passColor':'green',//the css color of text for passing test output 
  22
+			'failColor':'red'//the css color of text for failing test output
  23
+		};
  24
+		return {
  25
+			//create a handle for global settings so we can set them if necessary
  26
+			'settings':settings,
  27
+			//define a creation method to generate an executable test suite object
  28
+			'createSuite':function(customSettings){
  29
+				var tests = [],
  30
+					suiteSettings = {},
  31
+					div = document.createElement('div');//the suite's output wrapper
  32
+				//create quasi-unique id for the suite so we can find it through the dom latr
  33
+				suiteSettings.suiteId = ('suite' + Math.random()).replace('.','');
  34
+				//load global settings into local suite settings
  35
+				foreach(cajaUnit.settings, function(name, value){
  36
+					suiteSettings[name] = value;
  37
+				});
  38
+				//load/override local settings w/ custom ones
  39
+				if(customSettings){//foreach throws error on undefined input, so check first
  40
+					foreach(customSettings, function(name, value){
  41
+						suiteSettings[name] = value;
  42
+					});
  43
+				}
  44
+				return{
  45
+					'suiteSettings':suiteSettings,//used only for debugging
  46
+					//define function to add test to suite
  47
+					'addTest':function(fn){
  48
+						//load/override function settings w/ suite settings so we can set stuff in the suite and have it apply to all functions
  49
+						foreach(suiteSettings, function(name, value){
  50
+							fn.testSettings[name] = value;
  51
+						});
  52
+						//we want a suite's test output to append to the suite wrapper, not the global wrapper
  53
+						fn.testSettings.outputId = suiteSettings.suiteId;
  54
+						//add the test
  55
+						tests.push(fn);
  56
+					},
  57
+					//define fn to execute all test in the suite
  58
+					'run':function(){
  59
+						//print the suite name at the top of the suite's output
  60
+						div.appendChild(
  61
+							document.createTextNode(suiteSettings.suiteName));
  62
+						//assign the suite the quasi-unique id
  63
+						div.id = suiteSettings.suiteId;
  64
+						//append the suite root to the page now so the tests run later can find it
  65
+						document.getElementById(suiteSettings.outputId).appendChild(div);
  66
+						//execute the tests
  67
+						foreach(tests, function(index, test){
  68
+							test.run();
  69
+						});
  70
+					}
  71
+				};
  72
+			},
  73
+			//define a creation method to generate an executable test object
  74
+			'createTest':function(customSettings){
  75
+				var testSettings = {};
  76
+				//load default settings from cajaUnit, so we can optionally run a test outside of a suite
  77
+				foreach(cajaUnit.settings, function(name, value){
  78
+					testSettings[name] = value;
  79
+				});
  80
+				//if there are custom settings, load them
  81
+				if(customSettings){
  82
+					foreach(customSettings, function(name, value){
  83
+						testSettings[name] = value;
  84
+					});
  85
+				}
  86
+				return{
  87
+					//create handle to local settings so a suite can override them if necessary
  88
+					'testSettings':testSettings,
  89
+					//define a function to execute a test
  90
+					'run':function(){
  91
+						var div = document.createElement('div'),//the test's output wrapper
  92
+							setUpResults;
  93
+						//if a set up function was defined, run it and capture the output
  94
+	    				if(testSettings.setUp){
  95
+							setUpResults = testSettings.setUp();
  96
+	    				}
  97
+						//this is the core of everything: the test to run.  Pass in the results (may be undefined) of the set up function
  98
+	    				try{
  99
+							testSettings.test(setUpResults);
  100
+							//test passes
  101
+							div.style.color = testSettings.passColor;//you could also define a className here
  102
+	    				}catch(e){//test fails
  103
+	    					div.style.color = testSettings.failColor;console.log(e);
  104
+	    				}
  105
+						//print the test's name
  106
+						div.appendChild(
  107
+							document.createTextNode(testSettings.testName));
  108
+						//if a tear down function was defined, run it.  Pass in the set up results in case we want to edit/destroy them
  109
+	    				if(testSettings.tearDown){
  110
+	    					testSettings.tearDown(setUpResults);
  111
+	    				}
  112
+						//append the output to the parent.  If we're running in a suite, this will be defined by the suite, else by cajaUnit
  113
+						document.getElementById(testSettings.outputId).appendChild(div);
  114
+					}
  115
+				};
  116
+			},
  117
+			'assertTrue':function(val){
  118
+				if(true !== val){
  119
+					throw('');
  120
+				}
  121
+			},
  122
+			'assertFalse':function(val){
  123
+				if(false !== val){
  124
+					throw('');
  125
+				}
  126
+			},
  127
+			'assertEqual':function(val1, val2){
  128
+				if(val1 !== val2){
  129
+					throw('');
  130
+				}
  131
+			}
  132
+		};
  133
+	}();
140  cajaunit/cajaUnit_test.php
... ...
@@ -0,0 +1,140 @@
  1
+<style>
  2
+.pass{
  3
+	color:green;
  4
+}
  5
+.fail{
  6
+	color:red;
  7
+}
  8
+</style>
  9
+<div id="output"><br/></div>
  10
+<script>//js included via php because yap requires js to be inline
  11
+	//<?= file_get_contents('cajaUnit.js') ?>//using js comment to avoid php throwing errors in js lint
  12
+</script>
  13
+
  14
+<script>
  15
+//test the test unit
  16
+//BEGIN: createSuite
  17
+//test output
  18
+// var suite = cajaUnit.createSuite();
  19
+// suite.run();
  20
+// if(document.getElementById('output').innerHTML.match('<br/>[\t\n]*<div class="suite">New Suite</div>')){
  21
+//     document.getElementById('output').innerHTML += '<div class="pass">createSuite() :  test output</div>';
  22
+// }else{
  23
+//     document.getElementById('output').innerHTML += '<div class="fail">createSuite() :  test output</div>';
  24
+// }
  25
+// make sure cajaUnit settings stick through normal operation
  26
+cajaUnit.settings.failClassName = 'failed';
  27
+var suite = cajaUnit.createSuite(),
  28
+	test1 = cajaUnit.createTest();
  29
+suite.addTest(test1);
  30
+if('failed' === cajaUnit.settings.failClassName){
  31
+    document.getElementById('output').innerHTML += '<div class="pass">make sure cajaUnit settings stick through normal operation</div>';
  32
+}else{
  33
+    document.getElementById('output').innerHTML += '<div class="fail">make sure cajaUnit settings stick through normal operation</div>';
  34
+}
  35
+
  36
+// test default settings
  37
+var suite = cajaUnit.createSuite();
  38
+if('output' === suite.suiteSettings.outputId){
  39
+    document.getElementById('output').innerHTML += '<div class="pass">createSuite() :  test default settings</div>';
  40
+}else{
  41
+    document.getElementById('output').innerHTML += '<div class="fail">createSuite() :  test default settings</div>';
  42
+}
  43
+
  44
+//test name prints correctly
  45
+var suite = cajaUnit.createSuite({'suiteName':'test suite'});
  46
+suite.run();
  47
+var div = document.getElementById(suite.suiteSettings.suiteId);
  48
+if('test suite' === div.innerHTML){
  49
+    document.getElementById('output').innerHTML += '<div class="pass">test name prints correctly</div>';
  50
+}else{
  51
+    document.getElementById('output').innerHTML += '<div class="fail">test name prints correctly</div>';
  52
+}
  53
+//END: createSuite
  54
+
  55
+//BEGIN: createTest
  56
+//default settings
  57
+var suite = cajaUnit.createSuite(),
  58
+    test1 = cajaUnit.createTest();
  59
+suite.addTest(test1);
  60
+if('object' === typeof test1.testSettings){
  61
+    document.getElementById('output').innerHTML += '<div class="pass">createTest() :  test default settings</div>';
  62
+}else{
  63
+    document.getElementById('output').innerHTML += '<div class="fail">createTest() :  test default settings</div>';
  64
+}
  65
+
  66
+//custom settings
  67
+var suite = cajaUnit.createSuite(),
  68
+    test1 = cajaUnit.createTest({'testName':'test 1'});
  69
+suite.addTest(test1);
  70
+if('test 1' === test1.testSettings.testName){
  71
+    document.getElementById('output').innerHTML += '<div class="pass">createTest() :  test custom settings</div>';
  72
+}else{
  73
+    document.getElementById('output').innerHTML += '<div class="fail">createTest() :  test custom settings</div>';
  74
+}
  75
+
  76
+//setUpResults
  77
+var suite = cajaUnit.createSuite({'suiteName':'test suite'}),
  78
+    test1 = cajaUnit.createTest({
  79
+	'testName':'test 1',
  80
+	'setUp':function(){
  81
+		var div = document.createElement('div');
  82
+		div.id = 'test';
  83
+		return {
  84
+	        'div':div
  85
+		};
  86
+	},
  87
+	'test':function(setUpResults){
  88
+		if('test' === setUpResults.div.id){
  89
+		    document.getElementById('output').innerHTML += '<div class="pass">createTest() :  setUpResults</div>';
  90
+		}else{
  91
+		    document.getElementById('output').innerHTML += '<div class="fail">createTest() :  setUpResults</div>';
  92
+		}
  93
+		return true;
  94
+	}
  95
+});
  96
+suite.addTest(test1);
  97
+suite.run();
  98
+
  99
+//tearDown runs after test
  100
+var suite = cajaUnit.createSuite({'suiteName':'test suite'}),
  101
+    test1 = cajaUnit.createTest({
  102
+	'testName':'test 1',
  103
+	'setUp':function(){
  104
+		var div = document.createElement('div');
  105
+		div.id = 'test';
  106
+		document.getElementById('output').appendChild(div);
  107
+		return {};
  108
+	},
  109
+	'test':function(setUpResults){
  110
+		if(document.getElementById('test')){
  111
+		    document.getElementById('output').innerHTML += '<div class="pass">- sub-test - tearDown runs after test</div>';
  112
+		}else{
  113
+		    document.getElementById('output').innerHTML += '<div class="fail">- sub-test - tearDown runs after test</div>';
  114
+		}
  115
+		return true;
  116
+	},
  117
+	'tearDown':function(setUpResults){
  118
+		document.getElementById('output').removeChild(document.getElementById('test'));
  119
+	}
  120
+});
  121
+suite.addTest(test1);
  122
+suite.run();
  123
+if(!document.getElementById('test')){
  124
+    document.getElementById('output').innerHTML += '<div class="pass">tearDown runs after test</div>';
  125
+}else{
  126
+    document.getElementById('output').innerHTML += '<div class="fail">tearDown runs after test</div>';
  127
+}
  128
+
  129
+//stand alone test
  130
+var test1 = cajaUnit.createTest({
  131
+	'outputId':'output',
  132
+	'testName':'this is my sample test',
  133
+	'test':function(){
  134
+		var foo = 1,
  135
+			bar = 1;
  136
+		return (foo === bar);
  137
+	}
  138
+});
  139
+test1.run();
  140
+</script>

0 notes on commit 61ae756

Please sign in to comment.
Something went wrong with that request. Please try again.