Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | //----------------------------------------------------------------------------- |
michael@0 | 7 | var BUGNUMBER = 290592; |
michael@0 | 8 | var summary = 'Array extras: forEach, indexOf, filter, map'; |
michael@0 | 9 | var actual = ''; |
michael@0 | 10 | var expect = ''; |
michael@0 | 11 | |
michael@0 | 12 | printBugNumber(BUGNUMBER); |
michael@0 | 13 | printStatus (summary); |
michael@0 | 14 | |
michael@0 | 15 | // Utility functions |
michael@0 | 16 | |
michael@0 | 17 | function identity(v, index, array) |
michael@0 | 18 | { |
michael@0 | 19 | reportCompare(v, array[index], 'identity: check callback argument consistency'); |
michael@0 | 20 | return v; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function mutate(v, index, array) |
michael@0 | 24 | { |
michael@0 | 25 | reportCompare(v, array[index], 'mutate: check callback argument consistency'); |
michael@0 | 26 | if (index == 0) |
michael@0 | 27 | { |
michael@0 | 28 | array[1] = 'mutated'; |
michael@0 | 29 | delete array[2]; |
michael@0 | 30 | array.push('not visited'); |
michael@0 | 31 | } |
michael@0 | 32 | return v; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | function mutateForEach(v, index, array) |
michael@0 | 36 | { |
michael@0 | 37 | reportCompare(v, array[index], 'mutateForEach: check callback argument consistency'); |
michael@0 | 38 | if (index == 0) |
michael@0 | 39 | { |
michael@0 | 40 | array[1] = 'mutated'; |
michael@0 | 41 | delete array[2]; |
michael@0 | 42 | array.push('not visited'); |
michael@0 | 43 | } |
michael@0 | 44 | actual += v + ','; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function makeUpperCase(v, index, array) |
michael@0 | 48 | { |
michael@0 | 49 | reportCompare(v, array[index], 'makeUpperCase: check callback argument consistency'); |
michael@0 | 50 | try |
michael@0 | 51 | { |
michael@0 | 52 | return v.toUpperCase(); |
michael@0 | 53 | } |
michael@0 | 54 | catch(e) |
michael@0 | 55 | { |
michael@0 | 56 | } |
michael@0 | 57 | return v; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | function concat(v, index, array) |
michael@0 | 62 | { |
michael@0 | 63 | reportCompare(v, array[index], 'concat: check callback argument consistency'); |
michael@0 | 64 | actual += v + ','; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | function isUpperCase(v, index, array) |
michael@0 | 69 | { |
michael@0 | 70 | reportCompare(v, array[index], 'isUpperCase: check callback argument consistency'); |
michael@0 | 71 | try |
michael@0 | 72 | { |
michael@0 | 73 | return v == v.toUpperCase(); |
michael@0 | 74 | } |
michael@0 | 75 | catch(e) |
michael@0 | 76 | { |
michael@0 | 77 | } |
michael@0 | 78 | return false; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | function isString(v, index, array) |
michael@0 | 82 | { |
michael@0 | 83 | reportCompare(v, array[index], 'isString: check callback argument consistency'); |
michael@0 | 84 | return typeof v == 'string'; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | // callback object. |
michael@0 | 89 | function ArrayCallback(state) |
michael@0 | 90 | { |
michael@0 | 91 | this.state = state; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | ArrayCallback.prototype.makeUpperCase = function (v, index, array) |
michael@0 | 95 | { |
michael@0 | 96 | reportCompare(v, array[index], 'ArrayCallback.prototype.makeUpperCase: check callback argument consistency'); |
michael@0 | 97 | try |
michael@0 | 98 | { |
michael@0 | 99 | return this.state ? v.toUpperCase() : v.toLowerCase(); |
michael@0 | 100 | } |
michael@0 | 101 | catch(e) |
michael@0 | 102 | { |
michael@0 | 103 | } |
michael@0 | 104 | return v; |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | ArrayCallback.prototype.concat = function(v, index, array) |
michael@0 | 108 | { |
michael@0 | 109 | reportCompare(v, array[index], 'ArrayCallback.prototype.concat: check callback argument consistency'); |
michael@0 | 110 | actual += v + ','; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | ArrayCallback.prototype.isUpperCase = function(v, index, array) |
michael@0 | 114 | { |
michael@0 | 115 | reportCompare(v, array[index], 'ArrayCallback.prototype.isUpperCase: check callback argument consistency'); |
michael@0 | 116 | try |
michael@0 | 117 | { |
michael@0 | 118 | return this.state ? true : (v == v.toUpperCase()); |
michael@0 | 119 | } |
michael@0 | 120 | catch(e) |
michael@0 | 121 | { |
michael@0 | 122 | } |
michael@0 | 123 | return false; |
michael@0 | 124 | }; |
michael@0 | 125 | |
michael@0 | 126 | ArrayCallback.prototype.isString = function(v, index, array) |
michael@0 | 127 | { |
michael@0 | 128 | reportCompare(v, array[index], 'ArrayCallback.prototype.isString: check callback argument consistency'); |
michael@0 | 129 | return this.state ? true : (typeof v == 'string'); |
michael@0 | 130 | }; |
michael@0 | 131 | |
michael@0 | 132 | function dumpError(e) |
michael@0 | 133 | { |
michael@0 | 134 | var s = e.name + ': ' + e.message + |
michael@0 | 135 | ' File: ' + e.fileName + |
michael@0 | 136 | ', Line: ' + e.lineNumber + |
michael@0 | 137 | ', Stack: ' + e.stack; |
michael@0 | 138 | return s; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | var obj; |
michael@0 | 142 | var strings = ['hello', 'Array', 'WORLD']; |
michael@0 | 143 | var mixed = [0, '0', 0]; |
michael@0 | 144 | var sparsestrings = new Array(); |
michael@0 | 145 | sparsestrings[2] = 'sparse'; |
michael@0 | 146 | |
michael@0 | 147 | if ('map' in Array.prototype) |
michael@0 | 148 | { |
michael@0 | 149 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map |
michael@0 | 150 | |
michael@0 | 151 | // test Array.map |
michael@0 | 152 | |
michael@0 | 153 | // map has 1 required argument |
michael@0 | 154 | expect = 1; |
michael@0 | 155 | actual = Array.prototype.map.length; |
michael@0 | 156 | reportCompare(expect, actual, 'Array.prototype.map.length == 1'); |
michael@0 | 157 | |
michael@0 | 158 | // throw TypeError if no callback function specified |
michael@0 | 159 | expect = 'TypeError'; |
michael@0 | 160 | try |
michael@0 | 161 | { |
michael@0 | 162 | strings.map(); |
michael@0 | 163 | actual = 'no error'; |
michael@0 | 164 | } |
michael@0 | 165 | catch(e) |
michael@0 | 166 | { |
michael@0 | 167 | actual = e.name; |
michael@0 | 168 | } |
michael@0 | 169 | reportCompare(expect, actual, 'Array.map(undefined) throws TypeError'); |
michael@0 | 170 | |
michael@0 | 171 | try |
michael@0 | 172 | { |
michael@0 | 173 | // identity map |
michael@0 | 174 | expect = 'hello,Array,WORLD'; |
michael@0 | 175 | actual = strings.map(identity).toString(); |
michael@0 | 176 | } |
michael@0 | 177 | catch(e) |
michael@0 | 178 | { |
michael@0 | 179 | actual = dumpError(e); |
michael@0 | 180 | } |
michael@0 | 181 | reportCompare(expect, actual, 'Array.map: identity'); |
michael@0 | 182 | |
michael@0 | 183 | |
michael@0 | 184 | try |
michael@0 | 185 | { |
michael@0 | 186 | expect = 'hello,mutated,'; |
michael@0 | 187 | actual = strings.map(mutate).toString(); |
michael@0 | 188 | } |
michael@0 | 189 | catch(e) |
michael@0 | 190 | { |
michael@0 | 191 | actual = dumpError(e); |
michael@0 | 192 | } |
michael@0 | 193 | reportCompare(expect, actual, 'Array.map: mutate'); |
michael@0 | 194 | |
michael@0 | 195 | strings = ['hello', 'Array', 'WORLD']; |
michael@0 | 196 | |
michael@0 | 197 | try |
michael@0 | 198 | { |
michael@0 | 199 | // general map |
michael@0 | 200 | expect = 'HELLO,ARRAY,WORLD'; |
michael@0 | 201 | actual = strings.map(makeUpperCase).toString(); |
michael@0 | 202 | } |
michael@0 | 203 | catch(e) |
michael@0 | 204 | { |
michael@0 | 205 | actual = dumpError(e); |
michael@0 | 206 | } |
michael@0 | 207 | reportCompare(expect, actual, 'Array.map: uppercase'); |
michael@0 | 208 | |
michael@0 | 209 | try |
michael@0 | 210 | { |
michael@0 | 211 | // pass object method as map callback |
michael@0 | 212 | expect = 'HELLO,ARRAY,WORLD'; |
michael@0 | 213 | var obj = new ArrayCallback(true); |
michael@0 | 214 | actual = strings.map(obj.makeUpperCase, obj).toString(); |
michael@0 | 215 | } |
michael@0 | 216 | catch(e) |
michael@0 | 217 | { |
michael@0 | 218 | actual = dumpError(e); |
michael@0 | 219 | } |
michael@0 | 220 | reportCompare(expect, actual, 'Array.map: uppercase with object callback'); |
michael@0 | 221 | |
michael@0 | 222 | try |
michael@0 | 223 | { |
michael@0 | 224 | expect = 'hello,array,world'; |
michael@0 | 225 | obj = new ArrayCallback(false); |
michael@0 | 226 | actual = strings.map(obj.makeUpperCase, obj).toString(); |
michael@0 | 227 | } |
michael@0 | 228 | catch(e) |
michael@0 | 229 | { |
michael@0 | 230 | actual = dumpError(e); |
michael@0 | 231 | } |
michael@0 | 232 | reportCompare(expect, actual, 'Array.map: lowercase with object callback'); |
michael@0 | 233 | |
michael@0 | 234 | try |
michael@0 | 235 | { |
michael@0 | 236 | // map on sparse arrays |
michael@0 | 237 | expect = ',,SPARSE'; |
michael@0 | 238 | actual = sparsestrings.map(makeUpperCase).toString(); |
michael@0 | 239 | } |
michael@0 | 240 | catch(e) |
michael@0 | 241 | { |
michael@0 | 242 | actual = dumpError(e); |
michael@0 | 243 | } |
michael@0 | 244 | reportCompare(expect, actual, 'Array.map: uppercase on sparse array'); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | if ('forEach' in Array.prototype) |
michael@0 | 248 | { |
michael@0 | 249 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach |
michael@0 | 250 | |
michael@0 | 251 | // test Array.forEach |
michael@0 | 252 | |
michael@0 | 253 | // forEach has 1 required argument |
michael@0 | 254 | expect = 1; |
michael@0 | 255 | actual = Array.prototype.forEach.length; |
michael@0 | 256 | reportCompare(expect, actual, 'Array.prototype.forEach.length == 1'); |
michael@0 | 257 | |
michael@0 | 258 | // throw TypeError if no callback function specified |
michael@0 | 259 | expect = 'TypeError'; |
michael@0 | 260 | try |
michael@0 | 261 | { |
michael@0 | 262 | strings.forEach(); |
michael@0 | 263 | actual = 'no error'; |
michael@0 | 264 | } |
michael@0 | 265 | catch(e) |
michael@0 | 266 | { |
michael@0 | 267 | actual = e.name; |
michael@0 | 268 | } |
michael@0 | 269 | reportCompare(expect, actual, 'Array.forEach(undefined) throws TypeError'); |
michael@0 | 270 | |
michael@0 | 271 | try |
michael@0 | 272 | { |
michael@0 | 273 | // general forEach |
michael@0 | 274 | expect = 'hello,Array,WORLD,'; |
michael@0 | 275 | actual = ''; |
michael@0 | 276 | strings.forEach(concat); |
michael@0 | 277 | } |
michael@0 | 278 | catch(e) |
michael@0 | 279 | { |
michael@0 | 280 | actual = dumpError(e); |
michael@0 | 281 | } |
michael@0 | 282 | reportCompare(expect, actual, 'Array.forEach'); |
michael@0 | 283 | |
michael@0 | 284 | try |
michael@0 | 285 | { |
michael@0 | 286 | expect = 'hello,mutated,'; |
michael@0 | 287 | actual = ''; |
michael@0 | 288 | strings.forEach(mutateForEach); |
michael@0 | 289 | } |
michael@0 | 290 | catch(e) |
michael@0 | 291 | { |
michael@0 | 292 | actual = dumpError(e); |
michael@0 | 293 | } |
michael@0 | 294 | reportCompare(expect, actual, 'Array.forEach: mutate'); |
michael@0 | 295 | |
michael@0 | 296 | strings = ['hello', 'Array', 'WORLD']; |
michael@0 | 297 | |
michael@0 | 298 | |
michael@0 | 299 | |
michael@0 | 300 | try |
michael@0 | 301 | { |
michael@0 | 302 | // pass object method as forEach callback |
michael@0 | 303 | expect = 'hello,Array,WORLD,'; |
michael@0 | 304 | actual = ''; |
michael@0 | 305 | obj = new ArrayCallback(true); |
michael@0 | 306 | strings.forEach(obj.concat, obj); |
michael@0 | 307 | } |
michael@0 | 308 | catch(e) |
michael@0 | 309 | { |
michael@0 | 310 | actual = dumpError(e); |
michael@0 | 311 | } |
michael@0 | 312 | reportCompare(expect, actual, 'Array.forEach with object callback 1'); |
michael@0 | 313 | |
michael@0 | 314 | try |
michael@0 | 315 | { |
michael@0 | 316 | expect = 'hello,Array,WORLD,'; |
michael@0 | 317 | actual = ''; |
michael@0 | 318 | obj = new ArrayCallback(false); |
michael@0 | 319 | strings.forEach(obj.concat, obj); |
michael@0 | 320 | } |
michael@0 | 321 | catch(e) |
michael@0 | 322 | { |
michael@0 | 323 | actual = dumpError(e); |
michael@0 | 324 | } |
michael@0 | 325 | reportCompare(expect, actual, 'Array.forEach with object callback 2'); |
michael@0 | 326 | |
michael@0 | 327 | try |
michael@0 | 328 | { |
michael@0 | 329 | // test forEach on sparse arrays |
michael@0 | 330 | // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 |
michael@0 | 331 | expect = 'sparse,'; |
michael@0 | 332 | actual = ''; |
michael@0 | 333 | sparsestrings.forEach(concat); |
michael@0 | 334 | } |
michael@0 | 335 | catch(e) |
michael@0 | 336 | { |
michael@0 | 337 | actual = dumpError(e); |
michael@0 | 338 | } |
michael@0 | 339 | reportCompare(expect, actual, 'Array.forEach on sparse array'); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | if ('filter' in Array.prototype) |
michael@0 | 343 | { |
michael@0 | 344 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter |
michael@0 | 345 | |
michael@0 | 346 | // test Array.filter |
michael@0 | 347 | |
michael@0 | 348 | // filter has 1 required argument |
michael@0 | 349 | expect = 1; |
michael@0 | 350 | actual = Array.prototype.filter.length; |
michael@0 | 351 | reportCompare(expect, actual, 'Array.prototype.filter.length == 1'); |
michael@0 | 352 | |
michael@0 | 353 | // throw TypeError if no callback function specified |
michael@0 | 354 | expect = 'TypeError'; |
michael@0 | 355 | try |
michael@0 | 356 | { |
michael@0 | 357 | strings.filter(); |
michael@0 | 358 | actual = 'no error'; |
michael@0 | 359 | } |
michael@0 | 360 | catch(e) |
michael@0 | 361 | { |
michael@0 | 362 | actual = e.name; |
michael@0 | 363 | } |
michael@0 | 364 | reportCompare(expect, actual, 'Array.filter(undefined) throws TypeError'); |
michael@0 | 365 | |
michael@0 | 366 | try |
michael@0 | 367 | { |
michael@0 | 368 | // test general filter |
michael@0 | 369 | expect = 'WORLD'; |
michael@0 | 370 | actual = strings.filter(isUpperCase).toString(); |
michael@0 | 371 | } |
michael@0 | 372 | catch(e) |
michael@0 | 373 | { |
michael@0 | 374 | actual = dumpError(e); |
michael@0 | 375 | } |
michael@0 | 376 | reportCompare(expect, actual, 'Array.filter'); |
michael@0 | 377 | |
michael@0 | 378 | try |
michael@0 | 379 | { |
michael@0 | 380 | expect = 'WORLD'; |
michael@0 | 381 | obj = new ArrayCallback(false); |
michael@0 | 382 | actual = strings.filter(obj.isUpperCase, obj).toString(); |
michael@0 | 383 | } |
michael@0 | 384 | catch(e) |
michael@0 | 385 | { |
michael@0 | 386 | actual = dumpError(e); |
michael@0 | 387 | } |
michael@0 | 388 | reportCompare(expect, actual, 'Array.filter object callback 1'); |
michael@0 | 389 | |
michael@0 | 390 | try |
michael@0 | 391 | { |
michael@0 | 392 | expect = 'hello,Array,WORLD'; |
michael@0 | 393 | obj = new ArrayCallback(true); |
michael@0 | 394 | actual = strings.filter(obj.isUpperCase, obj).toString(); |
michael@0 | 395 | } |
michael@0 | 396 | catch(e) |
michael@0 | 397 | { |
michael@0 | 398 | actual = dumpError(e); |
michael@0 | 399 | } |
michael@0 | 400 | reportCompare(expect, actual, 'Array.filter object callback 2'); |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | if ('every' in Array.prototype) |
michael@0 | 404 | { |
michael@0 | 405 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every |
michael@0 | 406 | |
michael@0 | 407 | // test Array.every |
michael@0 | 408 | |
michael@0 | 409 | // every has 1 required argument |
michael@0 | 410 | |
michael@0 | 411 | expect = 1; |
michael@0 | 412 | actual = Array.prototype.every.length; |
michael@0 | 413 | reportCompare(expect, actual, 'Array.prototype.every.length == 1'); |
michael@0 | 414 | |
michael@0 | 415 | // throw TypeError if no every callback function specified |
michael@0 | 416 | expect = 'TypeError'; |
michael@0 | 417 | try |
michael@0 | 418 | { |
michael@0 | 419 | strings.every(); |
michael@0 | 420 | actual = 'no error'; |
michael@0 | 421 | } |
michael@0 | 422 | catch(e) |
michael@0 | 423 | { |
michael@0 | 424 | actual = e.name; |
michael@0 | 425 | } |
michael@0 | 426 | reportCompare(expect, actual, 'Array.every(undefined) throws TypeError'); |
michael@0 | 427 | |
michael@0 | 428 | // test general every |
michael@0 | 429 | |
michael@0 | 430 | try |
michael@0 | 431 | { |
michael@0 | 432 | expect = true; |
michael@0 | 433 | actual = strings.every(isString); |
michael@0 | 434 | } |
michael@0 | 435 | catch(e) |
michael@0 | 436 | { |
michael@0 | 437 | actual = dumpError(e); |
michael@0 | 438 | } |
michael@0 | 439 | reportCompare(expect, actual, 'strings: every element is a string'); |
michael@0 | 440 | |
michael@0 | 441 | try |
michael@0 | 442 | { |
michael@0 | 443 | expect = false; |
michael@0 | 444 | actual = mixed.every(isString); |
michael@0 | 445 | } |
michael@0 | 446 | catch(e) |
michael@0 | 447 | { |
michael@0 | 448 | actual = dumpError(e); |
michael@0 | 449 | } |
michael@0 | 450 | reportCompare(expect, actual, 'mixed: every element is a string'); |
michael@0 | 451 | |
michael@0 | 452 | try |
michael@0 | 453 | { |
michael@0 | 454 | // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 |
michael@0 | 455 | expect = true; |
michael@0 | 456 | actual = sparsestrings.every(isString); |
michael@0 | 457 | } |
michael@0 | 458 | catch(e) |
michael@0 | 459 | { |
michael@0 | 460 | actual = dumpError(e); |
michael@0 | 461 | } |
michael@0 | 462 | reportCompare(expect, actual, 'sparsestrings: every element is a string'); |
michael@0 | 463 | |
michael@0 | 464 | // pass object method as map callback |
michael@0 | 465 | |
michael@0 | 466 | obj = new ArrayCallback(false); |
michael@0 | 467 | |
michael@0 | 468 | try |
michael@0 | 469 | { |
michael@0 | 470 | expect = true; |
michael@0 | 471 | actual = strings.every(obj.isString, obj); |
michael@0 | 472 | } |
michael@0 | 473 | catch(e) |
michael@0 | 474 | { |
michael@0 | 475 | actual = dumpError(e); |
michael@0 | 476 | } |
michael@0 | 477 | reportCompare(expect, actual, 'strings: every element is a string, via object callback'); |
michael@0 | 478 | |
michael@0 | 479 | try |
michael@0 | 480 | { |
michael@0 | 481 | expect = false; |
michael@0 | 482 | actual = mixed.every(obj.isString, obj); |
michael@0 | 483 | } |
michael@0 | 484 | catch(e) |
michael@0 | 485 | { |
michael@0 | 486 | actual = dumpError(e) ; |
michael@0 | 487 | } |
michael@0 | 488 | reportCompare(expect, actual, 'mixed: every element is a string, via object callback'); |
michael@0 | 489 | |
michael@0 | 490 | try |
michael@0 | 491 | { |
michael@0 | 492 | // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 |
michael@0 | 493 | expect = true; |
michael@0 | 494 | actual = sparsestrings.every(obj.isString, obj); |
michael@0 | 495 | } |
michael@0 | 496 | catch(e) |
michael@0 | 497 | { |
michael@0 | 498 | actual = dumpError(e); |
michael@0 | 499 | } |
michael@0 | 500 | reportCompare(expect, actual, 'sparsestrings: every element is a string, via object callback'); |
michael@0 | 501 | |
michael@0 | 502 | } |
michael@0 | 503 | |
michael@0 | 504 | if ('some' in Array.prototype) |
michael@0 | 505 | { |
michael@0 | 506 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some |
michael@0 | 507 | |
michael@0 | 508 | // test Array.some |
michael@0 | 509 | |
michael@0 | 510 | // some has 1 required argument |
michael@0 | 511 | |
michael@0 | 512 | expect = 1; |
michael@0 | 513 | actual = Array.prototype.some.length; |
michael@0 | 514 | reportCompare(expect, actual, 'Array.prototype.some.length == 1'); |
michael@0 | 515 | |
michael@0 | 516 | // throw TypeError if no some callback function specified |
michael@0 | 517 | expect = 'TypeError'; |
michael@0 | 518 | try |
michael@0 | 519 | { |
michael@0 | 520 | strings.some(); |
michael@0 | 521 | actual = 'no error'; |
michael@0 | 522 | } |
michael@0 | 523 | catch(e) |
michael@0 | 524 | { |
michael@0 | 525 | actual = e.name; |
michael@0 | 526 | } |
michael@0 | 527 | reportCompare(expect, actual, 'Array.some(undefined) throws TypeError'); |
michael@0 | 528 | |
michael@0 | 529 | // test general some |
michael@0 | 530 | |
michael@0 | 531 | try |
michael@0 | 532 | { |
michael@0 | 533 | expect = true; |
michael@0 | 534 | actual = strings.some(isString); |
michael@0 | 535 | } |
michael@0 | 536 | catch(e) |
michael@0 | 537 | { |
michael@0 | 538 | actual = dumpError(e); |
michael@0 | 539 | } |
michael@0 | 540 | reportCompare(expect, actual, 'strings: some element is a string'); |
michael@0 | 541 | |
michael@0 | 542 | try |
michael@0 | 543 | { |
michael@0 | 544 | expect = true; |
michael@0 | 545 | actual = mixed.some(isString); |
michael@0 | 546 | } |
michael@0 | 547 | catch(e) |
michael@0 | 548 | { |
michael@0 | 549 | actual = dumpError(e); |
michael@0 | 550 | } |
michael@0 | 551 | reportCompare(expect, actual, 'mixed: some element is a string'); |
michael@0 | 552 | |
michael@0 | 553 | try |
michael@0 | 554 | { |
michael@0 | 555 | expect = true; |
michael@0 | 556 | actual = sparsestrings.some(isString); |
michael@0 | 557 | } |
michael@0 | 558 | catch(e) |
michael@0 | 559 | { |
michael@0 | 560 | actual = dumpError(e); |
michael@0 | 561 | } |
michael@0 | 562 | reportCompare(expect, actual, 'sparsestrings: some element is a string'); |
michael@0 | 563 | |
michael@0 | 564 | // pass object method as map callback |
michael@0 | 565 | |
michael@0 | 566 | obj = new ArrayCallback(false); |
michael@0 | 567 | |
michael@0 | 568 | try |
michael@0 | 569 | { |
michael@0 | 570 | expect = true; |
michael@0 | 571 | actual = strings.some(obj.isString, obj); |
michael@0 | 572 | } |
michael@0 | 573 | catch(e) |
michael@0 | 574 | { |
michael@0 | 575 | actual = dumpError(e); |
michael@0 | 576 | } |
michael@0 | 577 | reportCompare(expect, actual, 'strings: some element is a string, via object callback'); |
michael@0 | 578 | |
michael@0 | 579 | try |
michael@0 | 580 | { |
michael@0 | 581 | expect = true; |
michael@0 | 582 | actual = mixed.some(obj.isString, obj); |
michael@0 | 583 | } |
michael@0 | 584 | catch(e) |
michael@0 | 585 | { |
michael@0 | 586 | actual = dumpError(e); |
michael@0 | 587 | } |
michael@0 | 588 | reportCompare(expect, actual, 'mixed: some element is a string, via object callback'); |
michael@0 | 589 | |
michael@0 | 590 | try |
michael@0 | 591 | { |
michael@0 | 592 | expect = true; |
michael@0 | 593 | actual = sparsestrings.some(obj.isString, obj); |
michael@0 | 594 | } |
michael@0 | 595 | catch(e) |
michael@0 | 596 | { |
michael@0 | 597 | actual = dumpError(e); |
michael@0 | 598 | } |
michael@0 | 599 | reportCompare(expect, actual, 'sparsestrings: some element is a string, via object callback'); |
michael@0 | 600 | |
michael@0 | 601 | } |
michael@0 | 602 | |
michael@0 | 603 | if ('indexOf' in Array.prototype) |
michael@0 | 604 | { |
michael@0 | 605 | // see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf |
michael@0 | 606 | |
michael@0 | 607 | // test Array.indexOf |
michael@0 | 608 | |
michael@0 | 609 | // indexOf has 1 required argument |
michael@0 | 610 | |
michael@0 | 611 | expect = 1; |
michael@0 | 612 | actual = Array.prototype.indexOf.length; |
michael@0 | 613 | reportCompare(expect, actual, 'Array.prototype.indexOf.length == 1'); |
michael@0 | 614 | |
michael@0 | 615 | // test general indexOf |
michael@0 | 616 | |
michael@0 | 617 | try |
michael@0 | 618 | { |
michael@0 | 619 | expect = -1; |
michael@0 | 620 | actual = mixed.indexOf('not found'); |
michael@0 | 621 | } |
michael@0 | 622 | catch(e) |
michael@0 | 623 | { |
michael@0 | 624 | actual = dumpError(e); |
michael@0 | 625 | } |
michael@0 | 626 | reportCompare(expect, actual, 'indexOf returns -1 if value not found'); |
michael@0 | 627 | |
michael@0 | 628 | try |
michael@0 | 629 | { |
michael@0 | 630 | expect = 0; |
michael@0 | 631 | actual = mixed.indexOf(0); |
michael@0 | 632 | } |
michael@0 | 633 | catch(e) |
michael@0 | 634 | { |
michael@0 | 635 | actual = dumpError(e); |
michael@0 | 636 | } |
michael@0 | 637 | reportCompare(expect, actual, 'indexOf matches using strict equality'); |
michael@0 | 638 | |
michael@0 | 639 | try |
michael@0 | 640 | { |
michael@0 | 641 | expect = 1; |
michael@0 | 642 | actual = mixed.indexOf('0'); |
michael@0 | 643 | } |
michael@0 | 644 | catch(e) |
michael@0 | 645 | { |
michael@0 | 646 | actual = dumpError(e); |
michael@0 | 647 | } |
michael@0 | 648 | reportCompare(expect, actual, 'indexOf matches using strict equality'); |
michael@0 | 649 | |
michael@0 | 650 | try |
michael@0 | 651 | { |
michael@0 | 652 | expect = 2; |
michael@0 | 653 | actual = mixed.indexOf(0, 1); |
michael@0 | 654 | } |
michael@0 | 655 | catch(e) |
michael@0 | 656 | { |
michael@0 | 657 | actual = dumpError(e); |
michael@0 | 658 | } |
michael@0 | 659 | reportCompare(expect, actual, 'indexOf begins searching at fromIndex'); |
michael@0 | 660 | } |
michael@0 | 661 |