-
Notifications
You must be signed in to change notification settings - Fork 18
/
function.extensions.html
107 lines (90 loc) · 3.95 KB
/
function.extensions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>helpers.js test file</title>
<link rel="stylesheet" href="../lib/test.css" type="text/css" />
<script src="../lib/prototype.js" type="text/javascript"></script>
<script src="../lib/unittest.js" type="text/javascript"></script>
<script src="../function.extensions.js" type="text/javascript"></script>
</head>
<body>
<div id="testlog"></div>
<div id="dummy">foo</div>
<div id="runOnceDummy"></div>
<div id="addAdviceDummy"></div>
<script type="text/javascript">
new Test.Unit.Runner({
testFunctionPrototypeNot: function() {
var truthy = function() { return true; };
var falsy = function() { return false; };
var something = function() { return {foo: 'bar'} };
this.assert(truthy());
this.assert(!truthy.not()());
this.assert(!falsy());
this.assert(falsy.not()());
},
testFunctionPrototypeRunOnce: function() {
function updater(){
$('runOnceDummy').update('foo');
};
this.assertIdentical('', $('runOnceDummy').innerHTML); // contents should be empty
this.assertIdentical('function', typeof updater.runOnce()); // should return function
this.assertIdentical('foo', $('runOnceDummy').innerHTML); // should update content when run
},
testFunctionPrototype_New: function() {
var Person = Class.create({
initialize: function(name) { this.name = name },
speak: function(message) { return this.name + ': ' + message }
})
var jd = Person._new('John');
this.assert(jd instanceof Person);
this.assertIdentical('John', jd.name);
this.assertIdentical('John: hi there', jd.speak('hi there'));
},
testFunctionPrototypeToDelayed: function() {
var delayed = (function(){
$('dummy').addClassName('foo');
}).toDelayed(1);
delayed();
this.assert(!$('dummy').hasClassName('foo'));
this.wait(1000, (function(){
this.assert($('dummy').hasClassName('foo'));
}).bind(this))
},
testFunctionK: function() {
this.assert(Function.K(true)());
this.assert(!Function.K(false)());
this.assertEqual('foo', Function.K('foo')());
var source = { foo: 'bar', baz: 'qux' }, dest = { };
// "prop" variable should NOT be shared via closure
for (var prop in source) {
dest[prop] = Function.K(prop);
}
this.assertEqual('foo', dest.foo());
this.assertEqual('baz', dest.baz());
},
testFunctionPrototypeAddAdvice: function() {
Element.addMethods({
addClassName: Element.Methods.addClassName.addAdvice({
before: function(element, className) {
// when passed 'draggable' to addClassName, set element's position to 'absolute'
if (className == 'draggable')
$(element).absolutize();
}
})
});
// should not be "absolute" initially
this.assertNotEqual('absolute', $('addAdviceDummy').getStyle('position'));
Element.addClassName('addAdviceDummy', 'foo');
// should not be "absolute" after assigning "foo" className
this.assertNotEqual('absolute', $('addAdviceDummy').getStyle('position'));
Element.addClassName('addAdviceDummy', 'draggable');
// should be "absolute" after assigning "draggable" className
this.assertEqual('absolute', $('addAdviceDummy').getStyle('position'));
}
})
</script>
</body>
</html>