Browse Source

Added javascript unit tests for hash.

pull/5/head
Etenil 15 years ago
parent
commit
498c92488b
  1. 19
      js/hash.js
  2. 78
      js/hash_test.html
  3. 76
      tests/js/hash_test.js
  4. 20
      tests/js/index.html
  5. 86
      tests/js/tests.js

19
js/hash.js

@ -7,6 +7,16 @@ function H(object)
return new Hash(object);
}
function isHash(object)
{
var type = "";
if(object != null) {
type = object.type;
}
return type == "Hash";
}
/**
* Allows iterating over a hash.
*/
@ -16,6 +26,7 @@ function HashIterator(data, keys)
this.keys = keys;
this.cursor = 0;
this.iterated = false;
this.type = "HashIterator";
/**
* Moves one item further.
@ -92,7 +103,8 @@ function HashIterator(data, keys)
function Hash(object)
{
this.container = null;
this.container = null;
this.type = "Hash";
/**
* Adds an element to the Hash.
@ -119,6 +131,11 @@ function Hash(object)
return value;
}
this.haskey = function(key)
{
return key in this.container;
}
/**
* Iterates through the hash.
*/

78
js/hash_test.html

@ -1,78 +0,0 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="hash.js"></script>
<script type="text/javascript">
function print(text)
{
document.getElementById("page").innerHTML += text;
}
function array_to_string(array)
{
for(i in array) {
print(array[i] + "<br />");
}
}
function runtests()
{
// Starting tests.
print("Creating the Hash.<br />");
var myhash = new Hash();
print("Adding associative elements.<br />");
myhash.set("toto", "tata");
myhash.set("prout", "tagada");
print("<br />");
print("Dumping contents.<br />");
print(myhash.to_string());
print("<br /><br />");
print("Changing the value of 'prout' to 'tigidi'.<br />");
myhash.set('prout', 'tigidi');
print(myhash.to_string());
print("<br /><br />");
print("The value of 'toto' is '" + myhash.get('toto') + "'<br />");
print("<br />");
print("Re-creating hash with constructor.<br />");
myhash = H({toto: 'tata', couac: 'prout', pouet: 'tagada'});
print(myhash.to_string());
print("<br /><br />");
print("Iterating through the array.<br />");
var iter = myhash.iterate();
while(iter.next()) {
print(iter.key() +" => '"+ iter.val() +"'<br />");
}
print("<br />");
print("And reversed!<br />");
var iter = myhash.iterate();
iter.end();
while(iter.prev()) {
print(iter.key() +" => '"+ iter.val() +"'<br />");
}
print("<br />");
print("Getting all the keys.<br />");
array_to_string(myhash.keys());
print("<br />");
print("Getting all the values.<br />");
array_to_string(myhash.values());
print("<br />");
}
</script>
</head>
<body onload="runtests()">
<div id="page">
</div>
</body>
</html>

76
tests/js/hash_test.js

@ -0,0 +1,76 @@
var myhash = null;
tests.set('hash', H({
creation: function()
{
object = {test: 'prout'};
myhash = H(object);
equals(myhash.container, object);
myhash = new Hash();
different(myhash, null);
},
ishash: function()
{
var obj = new Array();
assert(isHash(myhash));
nassert(isHash(obj));
},
get: function()
{
hash = H({toto: "tata", prout: "tagada"});
equals(hash.get("toto"), "tata");
},
set: function()
{
myhash.set("toto", "tata");
myhash.set("prout", "tagada");
equals(myhash.get("toto"), "tata");
equals(myhash.get("prout"), "tagada");
myhash.set("toto", "tigidi");
equals(myhash.get("toto"), "tigidi");
},
tostring: function()
{
var hash = H({toto: "tata", prout: "tagada"});
equals(hash.to_string(), 'toto: tata, prout: tagada');
},
change: function()
{
myhash.set('prout', 'tigidi');
equals(myhash.get('prout'), 'tigidi');
},
iterate: function()
{
var iter = myhash.iterate();
while(iter.next()) {
equals(iter.val(), myhash.get(iter.key()))
}
iter.end();
while(iter.prev()) {
equals(iter.val(), myhash.get(iter.key()))
}
},
keys: function()
{
hash = H({toto: "tata", prout: "tagada"});
keys = new Array('toto', 'prout');
vals = new Array('tata', 'tagada');
hashkeys = hash.keys();
hashvals = hash.values();
assert(keys[0] == hashkeys[0] && keys[1] == hashkeys[1]);
assert(vals[0] == hashvals[0] && vals[1] == hashvals[1]);
},
}));

20
tests/js/index.html

@ -0,0 +1,20 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- Libraries to test -->
<script type="text/javascript" src="../../js/hash.js"></script>
<script type="text/javascript" src="../../js/events.js"></script>
<!-- Test framework -->
<script type="text/javascript" src="tests.js"></script>
<!-- Tests -->
<script type="text/javascript" src="hash_test.js"></script>
<script type="text/javascript" src="events_test.js"></script>
</head>
<body onload="runtests()">
<div id="page">
</div>
</body>
</html>

86
tests/js/tests.js

@ -0,0 +1,86 @@
/* Testing framework's tools. */
var failures = 0;
var success = 0;
var tests = H();
/************* Common stuff ***************/
function print(text)
{
document.getElementById("page").innerHTML += text;
}
function printtest(result)
{
if(!result) {
failures++;
print('x');
} else {
success++;
print('.');
}
}
function print_summary()
{
var total = failures + success;
print('<h2>Test summary:</h2>');
print('<p>Total tests: ' + total + '<br />');
print('Success: ' + success + '<br />');
print('Failures: ' + failures + '<br />');
print('Success rate: ' + parseInt(success / Math.max(1, total) * 100) + '%</p>');
}
// Runs the test suites
function runtests()
{
if(isHash(tests)) {
var iter = tests.iterate();
while(iter.next()) {
if(isHash(iter.val())) { // This is a test suite.
print("<h2>Running test " + iter.key() + "</h2>");
var suite_iter = iter.val().iterate();
while(suite_iter.next()) {
print('<b>' + suite_iter.key() + '</b> ');
var test = suite_iter.val();
try {
test();
}
catch(err)
{
printtest(false);
}
print('<br />');
}
}
}
}
print_summary();
}
/*********** Testing funtions *************/
// Asserts that thing returns true.
function assert(thing)
{
printtest(thing);
}
// Ensures that thing returns false.
function nassert(thing)
{
printtest(!thing);
}
// Ensures that thing is equal to exp_thing
function equals(thing, exp_thing)
{
printtest(thing == exp_thing);
}
// Ensures that thing is not equal to exp_thing
function different(thing, exp_thing)
{
printtest(thing != exp_thing);
}
Loading…
Cancel
Save