Initial import.
[pwl6.git] / test / spec / yuu / yf.js
1 var JS = this.JS || require('jstest');
2 require('yuu/pre');
3 var yf = this.yf || require('yuu/yf');
4
5 JS.Test.describe('yf', function() { with (this) {
6
7 it("allKeys", function () { with (this) {
8 assert(yf.contains(yf.allKeys(Object(1)), "hasOwnProperty"));
9 assertNot(yf.contains(yf.allKeys(Object(1)), "not a property"));
10 }});
11
12 it('argcd', function () { with (this) {
13 var f = yf.argcd(
14 function () { return 0; },
15 function (a) { return 1; },
16 function (a, b, c, d) { return 4; }
17 );
18 assertEqual(0, f());
19 assertEqual(1, f(""));
20 assertEqual(4, f("", "", "", ""));
21 assertThrows(TypeError, f.bind(null, "", ""));
22 }});
23
24 it('arrayify', function () { with (this) {
25 assertEqual([1], yf.arrayify(1));
26 assertEqual([1], yf.arrayify([1]));
27 }});
28
29 it('clamp', function () { with (this) {
30 assertEqual(0.5, yf.clamp(0.5, 0, 1));
31 assertEqual(1, yf.clamp(10, 0, 1));
32 assertEqual(0, yf.clamp(-1, 0, 1));
33 }})
34
35 it('compose', function () { with (this) {
36 function inc (a) { return a + 1; }
37 function add (a, b) { return a + b; }
38
39 var plus1 = yf.compose(inc);
40 var plus2 = yf.compose(inc, inc);
41 var plus3 = yf.compose(inc, inc, inc);
42 var addplus3 = yf.compose(plus3, add);
43
44 assertEqual(1, plus1(0));
45 assertEqual(2, plus2(0));
46 assertEqual(3, plus3(0));
47 assertEqual(6, addplus3(1, 2));
48
49 function this_ () { return this; }
50 var f = yf.compose(this_, this_, this_);
51 assertSame(this, f.call(this));
52 }});
53
54 it('construct', function () { with (this) {
55 assertEqual([], yf.construct(Array, []));
56 assertEqual([1, 2, 3], yf.construct(Array, [1, 2, 3]));
57 }});
58
59 it('counter', function () { with (this) {
60 var c0 = yf.counter(0);
61 var c5 = yf.counter(5);
62 assertEqual(0, c0());
63 assertEqual(1, c0());
64 assertEqual(2, c0());
65 assertEqual(5, c5());
66 assertEqual(6, c5());
67 }});
68
69 it('new_', function () { with (this) {
70 var a = yf.new_(Array);
71 assertEqual([], a());
72 assertEqual([1, 2, 3], a(1, 2, 3));
73 }});
74
75 it('foldl', function () { with (this) {
76 function sumString (v, i) { return v + i.toString(); }
77 var a = [1, 2, 3];
78 assertEqual("123", yf.foldl(sumString, a));
79 assertEqual("0123", yf.foldl(sumString, a, "0"));
80 }});
81
82 it('foldr', function () { with (this) {
83 function sumString (i, v) { return v + i.toString(); }
84 var a = [1, 2, 3];
85 assertEqual("321", yf.foldr(sumString, a));
86 assertEqual("4321", yf.foldr(sumString, a, "4"));
87 }});
88
89 JS.Test.describe('isFunction', function() { with (this) {
90 it("detects functions", function () { with (this) {
91 assert(yf.isFunction(Object));
92 assert(yf.isFunction(assert));
93 assert(yf.isFunction(yf.isFunction));
94 }});
95 it("detects non-functions", function () { with (this) {
96 assertNot(yf.isFunction(undefined));
97 assertNot(yf.isFunction(null));
98 assertNot(yf.isFunction(23));
99 assertNot(yf.isFunction(yf));
100 }});
101 }});
102
103 JS.Test.describe('isString', function () { with (this) {
104 it("detects strings", function () { with (this) {
105 assert(yf.isString(""));
106 assert(yf.isString("hello world"));
107 }});
108 it("detects Strings", function () { with (this) {
109 assert(yf.isString(new String("")));
110 assert(yf.isString(new String("hello world")));
111 }});
112 it("detects non-strings", function () { with (this) {
113 assertNot(yf.isString(undefined));
114 assertNot(yf.isString(null));
115 assertNot(yf.isString(23));
116 assertNot(yf.isString([1, 2, 3]));
117 }});
118 }});
119
120 JS.Test.describe('insertBefore', function () { with (this) {
121 it("inserts at start", function () { with (this) {
122 var a = [1, 2];
123 yf.insertBefore(a, 0, 1);
124 assertEqual([0, 1, 2], a);
125 }});
126 it("inserts at middle", function () { with (this) {
127 var a = [0, 2];
128 yf.insertBefore(a, 1, 2);
129 assertEqual([0, 1, 2], a);
130 }});
131 it("inserts at end if missing", function () { with (this) {
132 var a = [0, 1];
133 yf.insertBefore(a, 2, 99);
134 assertEqual([0, 1, 2], a);
135 }});
136 }});
137
138 JS.Test.describe('contains', function () { with (this) {
139 var a = [1, 2, 3];
140 it("finds things", function () { with (this) {
141 assert(yf.contains(a, 1));
142 assert(yf.contains(a, 3));
143 }});
144 it("doesn't find things", function () { with (this) {
145 assertNot(yf.contains(a, -1));
146 assertNot(yf.contains(a, 4));
147 }});
148 it("handles the empty sequence", function () { with (this) {
149 assertNot(yf.contains([], 1));
150 assertNot(yf.contains([], undefined));
151 assertNot(yf.contains([], null));
152 }});
153 it("uses identity, not equality", function () { with (this) {
154 assertNot(yf.contains(a, "1"));
155 }});
156 }});
157
158 JS.Test.describe('repeat', function () { with (this) {
159 it("repeats nothing", function () { with (this) {
160 assertEqual([], yf.repeat("hello", 0));
161 assertEqual([], yf.repeat(1, 0));
162 assertEqual([], yf.repeat(null, 0));
163 }});
164 it("repeats something", function () { with (this) {
165 assertEqual([1], yf.repeat(1, 1));
166 assertEqual([1, 1], yf.repeat(1, 2));
167 assertEqual([1, 1, 1, 1], yf.repeat(1, 4));
168 }});
169 }})
170
171 JS.Test.describe('without', function () { with (this) {
172 var a = [1, 2, 3];
173 it("doesn't remove wrongly", function () { with (this) {
174 assertEqual([1, 2, 3], yf.without(a, 4));
175 }});
176 it("removes by ===", function () { with (this) {
177 assertEqual([1, 2, 3], yf.without(a, "1"));
178 }});
179 it("removes one element", function () { with (this) {
180 assertEqual([2, 3], yf.without(a, 1));
181 }});
182 it("removes multiple elements", function () { with (this) {
183 assertEqual([3], yf.without(a, 1, 2));
184 assertEqual([2], yf.without(a, 1, 3));
185 assertEqual([], yf.without(a, 1, 2, 3));
186 }});
187 it("handles the empty sequence", function () { with (this) {
188 assertEqual([], yf.without([]));
189 assertEqual([], yf.without([], 1));
190 assertEqual([], yf.without([], 1, 2));
191 }});
192 }});
193
194 it("some", function () { with (this) {
195 assert(yf.some(null, [1]));
196 assert(yf.some(null, [1, 2]));
197 assert(yf.some(null, [1, 0]));
198
199 assertNot(yf.some(null, []));
200 assertNot(yf.some(null, [0, NaN]));
201 assertNot(yf.some(null, [0]));
202 }});
203
204 it('every', function () { with (this) {
205 assert(yf.every(null, []));
206 assert(yf.every(null, [1]));
207 assert(yf.every(null, [1, 2]));
208
209 assertNot(yf.every(null, [0]));
210 assertNot(yf.every(null, [1, 0]));
211 assertNot(yf.every(null, [1, 2, NaN]));
212 }});
213
214 it('none', function () { with (this) {
215 assert(yf.none(null, []));
216 assert(yf.none(null, [0]));
217 assert(yf.none(null, [0, NaN]));
218
219 assertNot(yf.none(null, [1]));
220 assertNot(yf.none(null, [1, 0]));
221 }});
222
223 it('pluck', function () { with (this) {
224 assertEqual([], yf.pluck("foo", []));
225 assertEqual([1, 2, 3], yf.pluck("length", ["a", "ab", "abc"]));
226 }});
227 }});