Export 0.2.1
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / underscore.string / test / test_underscore / functions.js
1 $(document).ready(function() {
2
3   module("Functions");
4
5   test("functions: bind", function() {
6     var context = {name : 'moe'};
7     var func = function(arg) { return "name: " + (this.name || arg); };
8     var bound = _.bind(func, context);
9     equals(bound(), 'name: moe', 'can bind a function to a context');
10
11     bound = _(func).bind(context);
12     equals(bound(), 'name: moe', 'can do OO-style binding');
13
14     bound = _.bind(func, null, 'curly');
15     equals(bound(), 'name: curly', 'can bind without specifying a context');
16
17     func = function(salutation, name) { return salutation + ': ' + name; };
18     func = _.bind(func, this, 'hello');
19     equals(func('moe'), 'hello: moe', 'the function was partially applied in advance');
20
21     var func = _.bind(func, this, 'curly');
22     equals(func(), 'hello: curly', 'the function was completely applied in advance');
23
24     var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };
25     func = _.bind(func, this, 'hello', 'moe', 'curly');
26     equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');
27
28     func = function(context, message) { equals(this, context, message); };
29     _.bind(func, 0, 0, 'can bind a function to `0`')();
30     _.bind(func, '', '', 'can bind a function to an empty string')();
31     _.bind(func, false, false, 'can bind a function to `false`')();
32
33     // These tests are only meaningful when using a browser without a native bind function
34     // To test this with a modern browser, set underscore's nativeBind to undefined
35     var F = function () { return this; };
36     var Boundf = _.bind(F, {hello: "moe curly"});
37     equal(new Boundf().hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");
38     equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");
39   });
40
41   test("functions: bindAll", function() {
42     var curly = {name : 'curly'}, moe = {
43       name    : 'moe',
44       getName : function() { return 'name: ' + this.name; },
45       sayHi   : function() { return 'hi: ' + this.name; }
46     };
47     curly.getName = moe.getName;
48     _.bindAll(moe, 'getName', 'sayHi');
49     curly.sayHi = moe.sayHi;
50     equals(curly.getName(), 'name: curly', 'unbound function is bound to current object');
51     equals(curly.sayHi(), 'hi: moe', 'bound function is still bound to original object');
52
53     curly = {name : 'curly'};
54     moe = {
55       name    : 'moe',
56       getName : function() { return 'name: ' + this.name; },
57       sayHi   : function() { return 'hi: ' + this.name; }
58     };
59     _.bindAll(moe);
60     curly.sayHi = moe.sayHi;
61     equals(curly.sayHi(), 'hi: moe', 'calling bindAll with no arguments binds all functions to the object');
62   });
63
64   test("functions: memoize", function() {
65     var fib = function(n) {
66       return n < 2 ? n : fib(n - 1) + fib(n - 2);
67     };
68     var fastFib = _.memoize(fib);
69     equals(fib(10), 55, 'a memoized version of fibonacci produces identical results');
70     equals(fastFib(10), 55, 'a memoized version of fibonacci produces identical results');
71
72     var o = function(str) {
73       return str;
74     };
75     var fastO = _.memoize(o);
76     equals(o('toString'), 'toString', 'checks hasOwnProperty');
77     equals(fastO('toString'), 'toString', 'checks hasOwnProperty');
78   });
79
80   asyncTest("functions: delay", 2, function() {
81     var delayed = false;
82     _.delay(function(){ delayed = true; }, 100);
83     setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);
84     setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);
85   });
86
87   asyncTest("functions: defer", 1, function() {
88     var deferred = false;
89     _.defer(function(bool){ deferred = bool; }, true);
90     _.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
91   });
92
93   asyncTest("functions: throttle", 2, function() {
94     var counter = 0;
95     var incr = function(){ counter++; };
96     var throttledIncr = _.throttle(incr, 100);
97     throttledIncr(); throttledIncr(); throttledIncr();
98     setTimeout(throttledIncr, 70);
99     setTimeout(throttledIncr, 120);
100     setTimeout(throttledIncr, 140);
101     setTimeout(throttledIncr, 190);
102     setTimeout(throttledIncr, 220);
103     setTimeout(throttledIncr, 240);
104     _.delay(function(){ ok(counter == 1, "incr was called immediately"); }, 30);
105     _.delay(function(){ ok(counter == 4, "incr was throttled"); start(); }, 400);
106   });
107
108   asyncTest("functions: throttle arguments", 2, function() {
109     var value = 0;
110     var update = function(val){ value = val; };
111     var throttledUpdate = _.throttle(update, 100);
112     throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);
113     setTimeout(function(){ throttledUpdate(4); }, 120);
114     setTimeout(function(){ throttledUpdate(5); }, 140);
115     setTimeout(function(){ throttledUpdate(6); }, 250);
116     _.delay(function(){ equals(value, 1, "updated to latest value"); }, 40);
117     _.delay(function(){ equals(value, 6, "updated to latest value"); start(); }, 400);
118   });
119
120   asyncTest("functions: throttle once", 1, function() {
121     var counter = 0;
122     var incr = function(){ counter++; };
123     var throttledIncr = _.throttle(incr, 100);
124     throttledIncr();
125     _.delay(function(){ ok(counter == 1, "incr was called once"); start(); }, 220);
126   });
127
128   asyncTest("functions: throttle twice", 1, function() {
129     var counter = 0;
130     var incr = function(){ counter++; };
131     var throttledIncr = _.throttle(incr, 100);
132     throttledIncr(); throttledIncr();
133     _.delay(function(){ ok(counter == 2, "incr was called twice"); start(); }, 220);
134   });
135
136   asyncTest("functions: debounce", 1, function() {
137     var counter = 0;
138     var incr = function(){ counter++; };
139     var debouncedIncr = _.debounce(incr, 50);
140     debouncedIncr(); debouncedIncr(); debouncedIncr();
141     setTimeout(debouncedIncr, 30);
142     setTimeout(debouncedIncr, 60);
143     setTimeout(debouncedIncr, 90);
144     setTimeout(debouncedIncr, 120);
145     setTimeout(debouncedIncr, 150);
146     _.delay(function(){ ok(counter == 1, "incr was debounced"); start(); }, 220);
147   });
148
149   test("functions: once", function() {
150     var num = 0;
151     var increment = _.once(function(){ num++; });
152     increment();
153     increment();
154     equals(num, 1);
155   });
156
157   test("functions: wrap", function() {
158     var greet = function(name){ return "hi: " + name; };
159     var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
160     equals(backwards('moe'), 'hi: moe eom', 'wrapped the saluation function');
161
162     var inner = function(){ return "Hello "; };
163     var obj   = {name : "Moe"};
164     obj.hi    = _.wrap(inner, function(fn){ return fn() + this.name; });
165     equals(obj.hi(), "Hello Moe");
166
167     var noop    = function(){};
168     var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); });
169     var ret     = wrapped(['whats', 'your'], 'vector', 'victor');
170     same(ret, [noop, ['whats', 'your'], 'vector', 'victor']);
171   });
172
173   test("functions: compose", function() {
174     var greet = function(name){ return "hi: " + name; };
175     var exclaim = function(sentence){ return sentence + '!'; };
176     var composed = _.compose(exclaim, greet);
177     equals(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
178
179     composed = _.compose(greet, exclaim);
180     equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
181   });
182
183   test("functions: after", function() {
184     var testAfter = function(afterAmount, timesCalled) {
185       var afterCalled = 0;
186       var after = _.after(afterAmount, function() {
187         afterCalled++;
188       });
189       while (timesCalled--) after();
190       return afterCalled;
191     };
192
193     equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");
194     equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
195     equals(testAfter(0, 0), 1, "after(0) should fire immediately");
196   });
197
198 });