prototype/test/unit/hash.html

229 lines
8.7 KiB
HTML
Raw Normal View History

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" />
</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: {
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: [] },
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 },
2007-10-13 10:55:52 +00:00
value_zero: { a:"b", c:0 }
2007-01-18 22:24:27 +00:00
};
new Test.Unit.Runner({
testSet: function() {
2007-10-13 10:55:52 +00:00
var h = $H({a: 'A'})
this.assertEqual('B', h.set('b', 'B'));
this.assertHashEqual({a: 'A', b: 'B'}, h);
2007-10-13 10:55:52 +00:00
this.assertUndefined(h.set('c'));
this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h);
},
2007-10-13 10:55:52 +00:00
testGet: function() {
2007-10-14 11:35:59 +00:00
var h = $H({a: 'A'});
this.assertEqual('A', h.get('a'));
this.assertUndefined(h.a);
this.assertUndefined($H({}).get('a'));
},
2007-10-13 10:55:52 +00:00
testUnset: function() {
2007-10-13 10:55:52 +00:00
var hash = $H(Fixtures.many);
this.assertEqual('B', hash.unset('b'));
this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash);
this.assertUndefined(hash.unset('z'));
this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash);
2007-10-14 11:35:59 +00:00
// not equivalent to Hash#remove
this.assertEqual('A', hash.unset('a', 'c'));
this.assertHashEqual({c: 'C', d:'D#'}, hash);
},
2007-10-13 10:55:52 +00:00
testToObject: function() {
2007-10-13 10:55:52 +00:00
var hash = $H(Fixtures.many), object = hash.toObject();
this.assertInstanceOf(Object, object);
this.assertHashEqual(Fixtures.many, object);
this.assertNotIdentical(Fixtures.many, object);
2007-10-13 10:55:52 +00:00
hash.set('foo', 'bar');
this.assertHashNotEqual(object, hash.toObject());
},
2007-01-18 22:24:27 +00:00
testConstruct: function() {
2007-10-13 10:55:52 +00:00
var object = Object.clone(Fixtures.one);
var h = new Hash(object), h2 = $H(object);
this.assertInstanceOf(Hash, h);
this.assertInstanceOf(Hash, h2);
2007-10-13 10:55:52 +00:00
this.assertHashEqual({}, new Hash());
this.assertHashEqual(object, h);
this.assertHashEqual(object, h2);
2007-10-13 10:55:52 +00:00
h.set('foo', 'bar');
this.assertHashNotEqual(object, h);
2007-10-13 10:55:52 +00:00
var clone = $H(h);
this.assertInstanceOf(Hash, clone);
this.assertHashEqual(h, clone);
2007-10-13 10:55:52 +00:00
h.set('foo', 'foo');
this.assertHashNotEqual(h, clone);
this.assertIdentical($H, Hash.from);
},
testKeys: function() {
this.assertEnumEqual([], $H({}).keys());
this.assertEnumEqual(['a'], $H(Fixtures.one).keys());
this.assertEnumEqual($w('a b c d'), $H(Fixtures.many).keys().sort());
this.assertEnumEqual($w('plus quad'), $H(Fixtures.functions).keys().sort());
},
testValues: function() {
this.assertEnumEqual([], $H({}).values());
this.assertEnumEqual(['A#'], $H(Fixtures.one).values());
this.assertEnumEqual($w('A B C D#'), $H(Fixtures.many).values().sort());
this.assertEnumEqual($w('function function'),
$H(Fixtures.functions).values().map(function(i){ return typeof i }));
this.assertEqual(9, $H(Fixtures.functions).get('quad')(3));
this.assertEqual(6, $H(Fixtures.functions).get('plus')(3));
},
testIndex: function() {
this.assertUndefined($H().index('foo'));
this.assert('a', $H(Fixtures.one).index('A#'));
this.assert('a', $H(Fixtures.many).index('A'));
this.assertUndefined($H(Fixtures.many).index('Z'))
var hash = $H({a:1,b:'2',c:1});
this.assert(['a','c'].include(hash.index(1)));
this.assertUndefined(hash.index('1'));
},
2007-01-18 22:24:27 +00:00
testMerge: function() {
2007-10-13 10:55:52 +00:00
var h = $H(Fixtures.many);
this.assertNotIdentical(h, h.merge());
this.assertNotIdentical(h, h.merge({}));
this.assertInstanceOf(Hash, h.merge());
this.assertInstanceOf(Hash, h.merge({}));
this.assertHashEqual(h, h.merge());
this.assertHashEqual(h, h.merge({}));
this.assertHashEqual(h, h.merge($H()));
this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.merge({aaa: 'AAA'}));
this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one));
},
testUpdate: function() {
2007-10-13 10:55:52 +00:00
var h = $H(Fixtures.many);
this.assertIdentical(h, h.update());
this.assertIdentical(h, h.update({}));
this.assertHashEqual(h, h.update());
this.assertHashEqual(h, h.update({}));
this.assertHashEqual(h, h.update($H()));
this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'}));
this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one));
},
testToQueryString: function() {
this.assertEqual('', $H({}).toQueryString());
this.assertEqual('a%23=A', $H({'a#': 'A'}).toQueryString());
this.assertEqual('a=A%23', $H(Fixtures.one).toQueryString());
this.assertEqual('a=A&b=B&c=C&d=D%23', $H(Fixtures.many).toQueryString());
this.assertEqual("a=b&c", $H(Fixtures.value_undefined).toQueryString());
this.assertEqual("a=b&c", $H("a=b&c".toQueryParams()).toQueryString());
this.assertEqual("a=b&c=", $H(Fixtures.value_null).toQueryString());
this.assertEqual("a=b&c=0", $H(Fixtures.value_zero).toQueryString());
this.assertEqual("color=r&color=g&color=b", $H(Fixtures.multiple).toQueryString());
this.assertEqual("color=r&color=&color=g&color&color=0", $H(Fixtures.multiple_nil).toQueryString());
this.assertEqual("color=&color", $H(Fixtures.multiple_all_nil).toQueryString());
this.assertEqual("", $H(Fixtures.multiple_empty).toQueryString());
this.assertEqual("", $H({foo: {}, bar: {}}).toQueryString());
this.assertEqual("stuff%5B%5D=%24&stuff%5B%5D=a&stuff%5B%5D=%3B", $H(Fixtures.multiple_special).toQueryString());
this.assertHashEqual(Fixtures.multiple_special, $H(Fixtures.multiple_special).toQueryString().toQueryParams());
this.assertIdentical(Object.toQueryString, Hash.toQueryString);
},
testInspect: function() {
this.assertEqual('#<Hash:{}>', $H({}).inspect());
this.assertEqual("#<Hash:{'a': 'A#'}>", $H(Fixtures.one).inspect());
this.assertEqual("#<Hash:{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D#'}>", $H(Fixtures.many).inspect());
},
2007-10-13 10:55:52 +00:00
testClone: function() {
2007-10-13 10:55:52 +00:00
var h = $H(Fixtures.many);
this.assertHashEqual(h, h.clone());
this.assertInstanceOf(Hash, h.clone());
this.assertNotIdentical(h, h.clone());
},
testToJSON: function() {
this.assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}',
$H({'b': [undefined, false, true, undefined], c: {a: 'hello!'}}).toJSON());
},
2007-10-14 11:35:59 +00:00
testAbilityToContainAnyKey: function() {
2007-10-14 11:35:59 +00:00
var h = $H({ _each: 'E', map: 'M', keys: 'K', pluck: 'P', unset: 'U' });
this.assertEnumEqual($w('_each keys map pluck unset'), h.keys().sort());
this.assertEqual('U', h.unset('unset'));
this.assertHashEqual({ _each: 'E', map: 'M', keys: 'K', pluck: 'P' }, h);
},
testHashToTemplateReplacements: function() {
var template = new Template("#{a} #{b}"), hash = $H({ a: "hello", b: "world" });
this.assertEqual("hello world", template.evaluate(hash.toObject()));
this.assertEqual("hello world", template.evaluate(hash));
this.assertEqual("hello", "#{a}".interpolate(hash));
},
testPreventIterationOverShadowedProperties: function() {
// redundant now that object is systematically cloned.
var FooMaker = function(value) {
this.key = value;
};
FooMaker.prototype.key = 'foo';
var foo = new FooMaker('bar');
this.assertEqual("key=bar", new Hash(foo).toQueryString());
this.assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString());
}
2007-10-14 11:35:59 +00:00
});
2007-01-18 22:24:27 +00:00
// ]]>
</script>
</body>
</html>