// ========================================================================== // Project: SproutCore Costello - Property Observing Library // Copyright: ©2006-2011 Strobe Inc. and contributors. // Portions ©2008-2011 Apple Inc. All rights reserved. // License: Licensed under MIT license (see license.js) // ========================================================================== /*global module, test, ok, isObj, equals, expects */ var enumerables; // global variables var DummyEnumerable = SC.Object.extend(SC.Enumerable, { content: [], length: function () { return this.content.length; }.property(), objectAt: function (idx) { return this.content[idx]; }, nextObject: function (idx) { return this.content[idx]; }, // add support for reduced properties. unknownProperty: function (key, value) { var ret = this.reducedProperty(key, value); if (ret === undefined) { if (value !== undefined) this[key] = value; ret = value; } return ret; }, replace: function (start, removed, added) { var ret = this.content.replace(start, removed, added), addedLength = added ? added.length : 0; this.enumerableContentDidChange(start, addedLength, addedLength - removed); return ret; }, unshiftObject: function (object) { this.replace(0, 0, [object]); return object; }, shiftObject: function () { var ret = this.replace(0, 1); return ret; }, pushObject: function (object) { this.replace(this.content.length - 1, 0, [object]); return object; }, popObject: function () { var ret = this.replace(this.content.length - 1, 1); return ret; } }); var runFunc = function (a, b) { return ['DONE', a, b]; }; var invokeWhileOK = function () { return "OK"; }; var invokeWhileNotOK = function () { return "FAIL"; }; var reduceTestFunc = function (prev, item, idx, e, pname) { return pname || 'TEST'; }; var CommonArray = [ { first: "Charles", gender: "male", californian: NO, ready: YES, visited: "Prague", doneTravelling: NO, run: runFunc, invokeWhileTest: invokeWhileOK, balance: 1 }, { first: "Jenna", gender: "female", californian: YES, ready: YES, visited: "Prague", doneTravelling: NO, run: runFunc, invokeWhileTest: invokeWhileOK, balance: 2 }, { first: "Peter", gender: "male", californian: NO, ready: YES, visited: "Prague", doneTravelling: NO, run: runFunc, invokeWhileTest: invokeWhileNotOK, balance: 3 }, { first: "Chris", gender: "male", californian: NO, ready: YES, visited: "Prague", doneTravelling: NO, run: runFunc, invokeWhileTest: invokeWhileOK, balance: 4 } ]; module("Real Array & DummyEnumerable", { setup: function () { enumerables = [SC.$A(CommonArray), DummyEnumerable.create({ content: SC.clone(CommonArray) })]; }, teardown: function () { enumerables = null; delete Array.prototype["@max(balance)"]; // remove cached value delete Array.prototype["@min(balance)"]; } }); test("should get enumerator that iterates through objects", function () { var src, ary2 = enumerables; for (var idx2=0, len2=ary2.length; idx2