2007-01-18 22:24:27 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Prototype Unit test file</title>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
|
|
<script src="../../dist/prototype.js" type="text/javascript"></script>
|
|
|
|
<script src="../lib/unittest.js" type="text/javascript"></script>
|
|
|
|
<link rel="stylesheet" href="../test.css" type="text/css" />
|
|
|
|
<style type="text/css" media="screen">
|
|
|
|
/* <![CDATA[ */
|
|
|
|
#testcss1 { font-size:11px; color: #f00; }
|
|
|
|
#testcss2 { font-size:12px; color: #0f0; display: none; }
|
|
|
|
/* ]]> */
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Prototype Unit test file</h1>
|
|
|
|
<p>
|
|
|
|
Test of the Hash.prototype extensions
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<!-- Log output -->
|
|
|
|
<div id="testlog"> </div>
|
|
|
|
|
|
|
|
<!-- Tests follow -->
|
|
|
|
<script type="text/javascript" language="javascript" charset="utf-8">
|
|
|
|
// <![CDATA[
|
|
|
|
|
|
|
|
var Fixtures = {
|
|
|
|
one: { a: 'A#' },
|
|
|
|
|
|
|
|
many: {
|
|
|
|
a: 'A',
|
|
|
|
b: 'B',
|
|
|
|
c: 'C',
|
|
|
|
d: 'D#'
|
|
|
|
},
|
|
|
|
|
|
|
|
functions: {
|
2007-03-27 20:55:56 +00:00
|
|
|
quad: function(n) { return n*n },
|
|
|
|
plus: function(n) { return n+n }
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
multiple: { color: $w('r g b') },
|
|
|
|
multiple_nil: { color: ['r', null, 'g', undefined, 0] },
|
|
|
|
multiple_all_nil: { color: [null, undefined] },
|
|
|
|
multiple_empty: { color: [] },
|
2007-03-09 03:15:03 +00:00
|
|
|
multiple_special: { 'stuff[]': $w('$ a ;') },
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
value_undefined: { a:"b", c:undefined },
|
|
|
|
value_null: { a:"b", c:null },
|
|
|
|
value_zero: { a:"b", c:0 },
|
|
|
|
|
|
|
|
dangerous: {
|
|
|
|
_each: 'E',
|
|
|
|
map: 'M',
|
|
|
|
keys: 'K',
|
|
|
|
values: 'V',
|
|
|
|
collect: 'C',
|
|
|
|
inject: 'I'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
new Test.Unit.Runner({
|
|
|
|
|
2007-03-09 03:15:03 +00:00
|
|
|
testConstruct: function(){ with(this) {
|
2007-03-27 20:55:56 +00:00
|
|
|
var h = $H(Fixtures.one);
|
|
|
|
assertNotIdentical(Fixtures.one, h);
|
|
|
|
assertIdentical(h, $H(h));
|
2007-03-09 03:15:03 +00:00
|
|
|
|
2007-03-27 20:55:56 +00:00
|
|
|
var h2 = new Hash(h);
|
|
|
|
assertNotIdentical(h, h2);
|
|
|
|
assertHashEqual(h, h2);
|
2007-03-09 03:15:03 +00:00
|
|
|
}},
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
testKeys: function(){ with(this) {
|
2007-03-04 21:57:29 +00:00
|
|
|
assertEnumEqual([], $H({}).keys());
|
|
|
|
assertEnumEqual(['a'], $H(Fixtures.one).keys());
|
2007-03-27 20:55:56 +00:00
|
|
|
assertEnumEqual($w('a b c d'), $H(Fixtures.many).keys().sort());
|
|
|
|
assertEnumEqual($w('plus quad'), $H(Fixtures.functions).keys().sort());
|
2007-01-18 22:24:27 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testValues: function(){ with(this) {
|
2007-03-04 21:57:29 +00:00
|
|
|
assertEnumEqual([], $H({}).values());
|
|
|
|
assertEnumEqual(['A#'], $H(Fixtures.one).values());
|
2007-03-27 20:55:56 +00:00
|
|
|
assertEnumEqual($w('A B C D#'), $H(Fixtures.many).values().sort());
|
|
|
|
assertEnumEqual($w('function function'),
|
|
|
|
$H(Fixtures.functions).values().map(function(i){ return typeof i }));
|
|
|
|
assertEqual(9, $H(Fixtures.functions).quad(3));
|
|
|
|
assertEqual(6, $H(Fixtures.functions).plus(3));
|
2007-01-18 22:24:27 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testMerge: function(){ with(this) {
|
2007-03-27 20:55:56 +00:00
|
|
|
assertHashEqual(Fixtures.many, $H(Fixtures.many).merge());
|
|
|
|
assertHashEqual(Fixtures.many, $H(Fixtures.many).merge({}));
|
|
|
|
assertHashEqual(Fixtures.many, $H(Fixtures.many).merge($H()));
|
|
|
|
assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, $H(Fixtures.many).merge({aaa: 'AAA'}));
|
|
|
|
assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, $H(Fixtures.many).merge(Fixtures.one));
|
2007-01-18 22:24:27 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testRemove: function(){ with(this) {
|
2007-03-04 21:57:29 +00:00
|
|
|
var hash = $H(Fixtures.many);
|
|
|
|
var values = hash.remove('b', 'c');
|
2007-03-27 20:55:56 +00:00
|
|
|
assertHashEqual({a:'A', d:'D#'}, hash);
|
2007-03-04 21:57:29 +00:00
|
|
|
assertEnumEqual($w('B C'), values);
|
2007-01-18 22:24:27 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testToQueryString: function(){ with(this) {
|
2007-03-27 20:55:56 +00:00
|
|
|
assertEqual('', $H({}).toQueryString());
|
|
|
|
assertEqual('a=A%23', $H(Fixtures.one).toQueryString());
|
|
|
|
assertEqual('a=A&b=B&c=C&d=D%23', $H(Fixtures.many).toQueryString());
|
|
|
|
assertEqual("a=b&c", $H(Fixtures.value_undefined).toQueryString());
|
|
|
|
assertEqual("a=b&c", $H("a=b&c".toQueryParams()).toQueryString());
|
|
|
|
assertEqual("a=b&c=", $H(Fixtures.value_null).toQueryString());
|
|
|
|
assertEqual("a=b&c=0", $H(Fixtures.value_zero).toQueryString());
|
2007-03-04 21:57:29 +00:00
|
|
|
assertEqual("color=r&color=g&color=b", $H(Fixtures.multiple).toQueryString());
|
2007-03-27 20:55:56 +00:00
|
|
|
assertEqual("color=r&color=&color=g&color&color=0", $H(Fixtures.multiple_nil).toQueryString());
|
|
|
|
assertEqual("color=&color", $H(Fixtures.multiple_all_nil).toQueryString());
|
|
|
|
assertEqual("", $H(Fixtures.multiple_empty).toQueryString());
|
2007-03-09 03:15:03 +00:00
|
|
|
assertEqual("stuff%5B%5D=%24&stuff%5B%5D=a&stuff%5B%5D=%3B", $H(Fixtures.multiple_special).toQueryString());
|
2007-03-27 20:55:56 +00:00
|
|
|
assertHashEqual(Fixtures.multiple_special, $H(Fixtures.multiple_special).toQueryString().toQueryParams());
|
|
|
|
|
|
|
|
var danger = $w("_each=E collect=C inject=I keys=K map=M values=V");
|
|
|
|
assertEnumEqual(danger, Hash.toQueryString(Fixtures.dangerous).split('&').sort());
|
|
|
|
assertEnumEqual(danger, $H(Fixtures.dangerous).toQueryString().split('&').sort());
|
2007-01-18 22:24:27 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testInspect: function(){ with(this) {
|
|
|
|
assertEqual('#<Hash:{}>', $H({}).inspect());
|
|
|
|
assertEqual("#<Hash:{'a': 'A#'}>", $H(Fixtures.one).inspect());
|
|
|
|
assertEqual("#<Hash:{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D#'}>", $H(Fixtures.many).inspect());
|
2007-03-09 03:23:24 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
testToJSON: function(){ with(this) {
|
2007-04-24 02:50:52 +00:00
|
|
|
assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}',
|
2007-03-09 03:23:24 +00:00
|
|
|
$H({'b': [undefined, false, true, undefined], c: {a: 'hello!'}}).toJSON());
|
|
|
|
|
|
|
|
assertEqual('E', eval('(' + $H(Fixtures.dangerous).toJSON() + ')')._each);
|
2007-01-18 22:24:27 +00:00
|
|
|
}}
|
|
|
|
}, 'testlog');
|
|
|
|
// ]]>
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|