1 var JS
= this.JS
|| require('jstest');
3 var yf
= this.yf
|| require('yuu/yf');
5 JS
.Test
.describe('yf', function() { with (this) {
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"));
12 it('argcd', function () { with (this) {
14 function () { return 0; },
15 function (a
) { return 1; },
16 function (a
, b
, c
, d
) { return 4; }
19 assertEqual(1, f(""));
20 assertEqual(4, f("", "", "", ""));
21 assertThrows(TypeError
, f
.bind(null, "", ""));
24 it('arrayify', function () { with (this) {
25 assertEqual([1], yf
.arrayify(1));
26 assertEqual([1], yf
.arrayify([1]));
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));
35 it('compose', function () { with (this) {
36 function inc (a
) { return a
+ 1; }
37 function add (a
, b
) { return a
+ b
; }
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
);
44 assertEqual(1, plus1(0));
45 assertEqual(2, plus2(0));
46 assertEqual(3, plus3(0));
47 assertEqual(6, addplus3(1, 2));
49 function this_ () { return this; }
50 var f
= yf
.compose(this_
, this_
, this_
);
51 assertSame(this, f
.call(this));
54 it('construct', function () { with (this) {
55 assertEqual([], yf
.construct(Array
, []));
56 assertEqual([1, 2, 3], yf
.construct(Array
, [1, 2, 3]));
59 it('counter', function () { with (this) {
60 var c0
= yf
.counter(0);
61 var c5
= yf
.counter(5);
69 it('new_', function () { with (this) {
70 var a
= yf
.new_(Array
);
72 assertEqual([1, 2, 3], a(1, 2, 3));
75 it('foldl', function () { with (this) {
76 function sumString (v
, i
) { return v
+ i
.toString(); }
78 assertEqual("123", yf
.foldl(sumString
, a
));
79 assertEqual("0123", yf
.foldl(sumString
, a
, "0"));
82 it('foldr', function () { with (this) {
83 function sumString (i
, v
) { return v
+ i
.toString(); }
85 assertEqual("321", yf
.foldr(sumString
, a
));
86 assertEqual("4321", yf
.foldr(sumString
, a
, "4"));
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
));
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
));
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"));
108 it("detects Strings", function () { with (this) {
109 assert(yf
.isString(new String("")));
110 assert(yf
.isString(new String("hello world")));
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]));
120 JS
.Test
.describe('insertBefore', function () { with (this) {
121 it("inserts at start", function () { with (this) {
123 yf
.insertBefore(a
, 0, 1);
124 assertEqual([0, 1, 2], a
);
126 it("inserts at middle", function () { with (this) {
128 yf
.insertBefore(a
, 1, 2);
129 assertEqual([0, 1, 2], a
);
131 it("inserts at end if missing", function () { with (this) {
133 yf
.insertBefore(a
, 2, 99);
134 assertEqual([0, 1, 2], a
);
138 JS
.Test
.describe('contains', function () { with (this) {
140 it("finds things", function () { with (this) {
141 assert(yf
.contains(a
, 1));
142 assert(yf
.contains(a
, 3));
144 it("doesn't find things", function () { with (this) {
145 assertNot(yf
.contains(a
, -1));
146 assertNot(yf
.contains(a
, 4));
148 it("handles the empty sequence", function () { with (this) {
149 assertNot(yf
.contains([], 1));
150 assertNot(yf
.contains([], undefined));
151 assertNot(yf
.contains([], null));
153 it("uses identity, not equality", function () { with (this) {
154 assertNot(yf
.contains(a
, "1"));
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));
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));
171 JS
.Test
.describe('without', function () { with (this) {
173 it("doesn't remove wrongly", function () { with (this) {
174 assertEqual([1, 2, 3], yf
.without(a
, 4));
176 it("removes by ===", function () { with (this) {
177 assertEqual([1, 2, 3], yf
.without(a
, "1"));
179 it("removes one element", function () { with (this) {
180 assertEqual([2, 3], yf
.without(a
, 1));
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));
187 it("handles the empty sequence", function () { with (this) {
188 assertEqual([], yf
.without([]));
189 assertEqual([], yf
.without([], 1));
190 assertEqual([], yf
.without([], 1, 2));
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]));
199 assertNot(yf
.some(null, []));
200 assertNot(yf
.some(null, [0, NaN
]));
201 assertNot(yf
.some(null, [0]));
204 it('every', function () { with (this) {
205 assert(yf
.every(null, []));
206 assert(yf
.every(null, [1]));
207 assert(yf
.every(null, [1, 2]));
209 assertNot(yf
.every(null, [0]));
210 assertNot(yf
.every(null, [1, 0]));
211 assertNot(yf
.every(null, [1, 2, NaN
]));
214 it('none', function () { with (this) {
215 assert(yf
.none(null, []));
216 assert(yf
.none(null, [0]));
217 assert(yf
.none(null, [0, NaN
]));
219 assertNot(yf
.none(null, [1]));
220 assertNot(yf
.none(null, [1, 0]));
223 it('pluck', function () { with (this) {
224 assertEqual([], yf
.pluck("foo", []));
225 assertEqual([1, 2, 3], yf
.pluck("length", ["a", "ab", "abc"]));