142
St
startsInvasion / files
startsInvasion / libs / node_modules / reqwest / build / valentine / tests / tests.js
7.4 KB
1if (typeof module !== 'undefined' && module.exports) { 2 var s = require('../build/sink'), 3 start = s.start, 4 sink = s.sink; 5 var v = require('../src/valentine'); 6} 7 8sink('Arrays', function(test, ok, before, after) { 9 test('each', 3, function () { 10 v.each(['a', 'b'], function (el, i, ar) { 11 ok(el == ar[i], 'each of arrays ' + i); 12 }); 13 v.each({ foo: 'bar' }, function (k, v, ar) { 14 ok(v == ar[k], 'each of objects'); 15 }); 16 }); 17 18 test('map', 2, function () { 19 var m = v.map(['a', 'b'], function (el) { 20 return el.toUpperCase(); 21 }); 22 23 ok(m[0] === 'A' && m[1] === 'B', 'map of arrays'); 24 25 var om = v.map({ 26 foo: 'bar', 27 baz: 'thunk' 28 }, function (k, v) { 29 return v; 30 }); 31 32 ok(om[0] === 'bar' && om[1] === 'thunk', 'map of objects'); 33 34 }); 35 36 test('every', 2, function () { 37 var a1 = ['a', 'a', 'a']; 38 var a2 = ['a', 'a', 'b']; 39 40 ok(v.every(a1, function (el) { 41 return el == 'a' 42 }), 'all elements in array are "a"'); 43 44 ok(!v.every(a2, function (el) { 45 return el == 'a' 46 }), 'all elements in array are not "a"'); 47 }); 48 49 test('some', 2, function () { 50 var a1 = ['a', 'a', 'a']; 51 var a2 = ['a', 'a', 'b']; 52 53 ok(!v.some(a1, function (el) { 54 return el == 'b' 55 }), 'no elements in array have "b"'); 56 57 ok(v.some(a2, function (el) { 58 return el == 'b' 59 }), 'some elements in array have "b"'); 60 }); 61 62 test('filter', 1, function () { 63 var a = v.filter(['a', 'b', 'virus', 'c'], function (el) { 64 return el !== 'virus'; 65 }); 66 var expected = ['a', 'b', 'c']; 67 ok(v.every(expected, function (el, i) { 68 return el == a[i]; 69 }), 'filters out viruses'); 70 }); 71 72 test('reject', 1, function () { 73 var a = v.reject(['a', 'b', 'virus', 'c'], function (el) { 74 return el === 'virus'; 75 }); 76 var expected = ['a', 'b', 'c']; 77 ok(v.every(expected, function (el, i) { 78 return el == a[i]; 79 }), 'filters out viruses'); 80 }); 81 82 test('indexOf', 2, function () { 83 ok(v.indexOf(['a', 'b', 'c'], 'b') == 1, 'indexOf b == 1'); 84 ok(v.indexOf(['x', 'y', 'z'], 'b') == -1, 'indexOf b == -1'); 85 }); 86 87 test('lastIndexOf', 2, function () { 88 ok(v.lastIndexOf(['a', 'b', 'c'], 'c') == 2, 'indexOf c == 2'); 89 ok(v.lastIndexOf(['x', 'y', 'z'], 'b') == -1, 'indexOf b == -1'); 90 }); 91 92}); 93 94sink('Utility', function (test, ok) { 95 test('extend', 2, function () { 96 var o = { 97 foo: 'bar' 98 }; 99 var out = v.extend(o, { baz: 'thunk' }); 100 ok(out.foo == 'bar', 'contains foo property'); 101 ok(out.baz == 'thunk', 'contains baz property'); 102 }); 103 104 test('pluck', 1, function () { 105 var o = [ 106 {a: 'foo'}, 107 {a: 'bar'}, 108 {a: 'baz'} 109 ]; 110 var expected = ['foo', 'bar', 'baz']; 111 ok(v.every(v.pluck(o, 'a'), function (el, i) { 112 return el == expected[i]; 113 }, 'plucked foo bar baz')); 114 }); 115 116 test('toArray', 1, function () { 117 var el = document.getElementsByTagName('div'); 118 ok(v.toArray(el) instanceof Array, 'element collection is now an array'); 119 }); 120 121 test('size', 1, function () { 122 ok(v.size(['a', 'b', 'c']) == 3, 'size is 3'); 123 }); 124 125 test('find', 1, function () { 126 ok(v.find(['a', 'b', 'c'], function (el) { 127 return el === 'a'; 128 }) === 'a', 'found element "a"'); 129 }); 130 131 test('compact', 1, function () { 132 ok(v.compact([,,,1,false,0,null,'']).length == 1, 'compacted [,,,1,false,0,null,""] to [1]'); 133 }); 134 135 test('flatten', 1, function () { 136 var actual = v.flatten([['a', [[['b']], ['c']], 'd']]); 137 var expected = ['a', 'b', 'c', 'd']; 138 ok(v.every(actual, function (el, i) { 139 return el == expected[i]; 140 }), 'flattened a really crappy looking array'); 141 }); 142 143 test('uniq', 1, function () { 144 var actual = v.uniq(['a', 'a', 'a', 'b', 'b', 'c']); 145 var expected = ['a', 'b', 'c']; 146 ok(v.every(actual, function (el, i) { 147 return el == expected[i]; 148 }), "turned ['a', 'a', 'a', 'b', 'b', 'c'] into ['a', 'b', 'c']"); 149 }); 150 151 test('first', 1, function () { 152 ok(v.first(['a', 'b', 'c']) == 'a', 'a is first'); 153 }); 154 155 test('last', 1, function () { 156 ok(v.last(['a', 'b', 'c']) == 'c', 'c is last'); 157 }); 158 159 test('keys', 1, function () { 160 var actual = v.keys({ 161 a: 'foo', 162 b: 'bar', 163 c: 'baz' 164 }); 165 var expected = ['a', 'b', 'c']; 166 ok(v.every(actual, function (el, i) { 167 return el == expected[i]; 168 }), "a, b, c were keys"); 169 }); 170 171 test('values', 1, function () { 172 var actual = v.values({ 173 a: 'foo', 174 b: 'bar', 175 c: 'baz' 176 }); 177 var expected = ['foo', 'bar', 'baz']; 178 ok(v.every(actual, function (el, i) { 179 return el == expected[i]; 180 }), "foo, bar, baz values were found"); 181 }); 182 183 test('trim', 1, function () { 184 ok(v.trim(' \n\r omg bbq wtf \n\n ') === 'omg bbq wtf', 'string was trimmed'); 185 }); 186 187 test('bind', 1, function () { 188 var o = { 189 foo: 'bar' 190 }; 191 function wha() { 192 ok(this.foo == 'bar', 'this.foo == "bar"'); 193 } 194 var bound = v.bind(o, wha); 195 bound(); 196 }); 197 198}); 199 200sink('Type Checking', function (test, ok) { 201 202 test('String', 3, function () { 203 ok(v.is.str('hello'), 'v.is.str("hello")'); 204 ok(v.is.str(''), 'v.is.str("")'); 205 ok(!v.is.str(null), '!v.is.str(null)'); 206 }); 207 208 test('Function', 6, function () { 209 ok(v.is.fun(function () {}), 'function () {}'); 210 ok(v.is.fun(Function), 'Function'); 211 ok(v.is.fun(new Function), 'new Function'); 212 ok(!v.is.fun({}), 'not {}'); 213 ok(!v.is.fun([]), 'not []'); 214 ok(!v.is.fun(''), 'not ""'); 215 }); 216 217 test('Array', 4, function () { 218 ok(v.is.arr([]), '[]'); 219 ok(v.is.arr(Array(1)), 'Array(1)'); 220 ok(v.is.arr(new Array), 'new Array'); 221 ok(!v.is.arr(Object), 'not Object'); 222 }); 223 224 test('Number', 3, function () { 225 ok(v.is.num(1), '1'); 226 ok(v.is.num(1.1), '1.1'); 227 ok(!v.is.num('1'), '"1"'); 228 }); 229 230 test('Boolean', 6, function () { 231 ok(v.is.bool(false), 'false'); 232 ok(v.is.bool(true), 'true'); 233 ok(v.is.bool(!0), '!0'); 234 ok(v.is.bool(!!1), '!!1'); 235 ok(!v.is.bool('true'), '"true"'); 236 ok(!v.is.bool('false'), '"false"'); 237 }); 238 239 test('Arguments', 1, function () { 240 (function () { 241 ok(v.is.args(arguments), 'arguments'); 242 })(); 243 }); 244 245 test('Empty', 6, function () { 246 ok(v.is.emp({}), '{}'); 247 ok(v.is.emp([]), '[]'); 248 ok(v.is.emp(''), '""'); 249 ok(!v.is.emp({foo:'bar'}), '{foo:bar}'); 250 ok(!v.is.emp([1]), '[1]'); 251 ok(!v.is.emp('i'), '"i"'); 252 }); 253 254 test('Date', 1, function () { 255 ok(v.is.dat(new Date), 'new Date'); 256 }); 257 258 test('RegExp', 2, function () { 259 ok(v.is.reg(/i/), '/i/'); 260 ok(v.is.reg(new RegExp("i")), 'new RegExp("i")'); 261 }); 262 263 test('Null', 3, function () { 264 ok(v.is.nil(null), 'null'); 265 ok(!v.is.nil(""), '""'); 266 ok(!v.is.nil(), 'undefined'); 267 }); 268 269 test('Undefined', 3, function () { 270 ok(v.is.und(), 'no args'); 271 ok(v.is.und(undefined), 'undefined'); 272 ok(!v.is.und(null), 'undefined'); 273 }); 274 275 test('Object', 4, function () { 276 ok(v.is.obj({}), '{}'); 277 ok(v.is.obj(new Object), 'Object'); 278 ok(!v.is.obj([]), 'not []'); 279 ok(!v.is.obj(function() {}), 'not function(){}'); 280 }); 281 282}); 283 284sink('OO Style and chaining', function (test, ok) { 285 286 test('method chains', 1, function () { 287 var expected = ['A', 'B', 'C']; 288 var actual = v(['a', 'b', [['c']], 0, false,,,null,['a', 'b', 'c']]) 289 .chain().flatten().compact().uniq().map(function (letter) { 290 return letter.toUpperCase(); 291 }).value(); 292 ok(v.every(actual, function (el, i) { 293 return el == expected[i]; 294 }), "all methods chained together"); 295 }); 296 297}); 298 299start();