michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = "346582"; michael@0: var summary = "Basic support for iterable objects and for-each"; michael@0: var actual, expect; michael@0: michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: var failed = false; michael@0: michael@0: var iterable = { persistedProp: 17 }; michael@0: michael@0: function Array_equals(a, b) michael@0: { michael@0: if (!(a instanceof Array) || !(b instanceof Array)) michael@0: throw new Error("Arguments not both of type Array"); michael@0: if (a.length != b.length) michael@0: return false; michael@0: for (var i = 0, sz = a.length; i < sz; i++) michael@0: if (a[i] !== b[i]) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: try michael@0: { michael@0: // nothing unusual so far -- verify basic properties michael@0: for (var i in iterable) michael@0: { michael@0: if (i != "persistedProp") michael@0: throw "no persistedProp!"; michael@0: if (iterable[i] != 17) michael@0: throw "iterable[\"persistedProp\"] == 17"; michael@0: } michael@0: michael@0: var keys = ["foo", "bar", "baz"]; michael@0: var vals = [6, 5, 14]; michael@0: michael@0: iterable.__iterator__ = michael@0: function(keysOnly) michael@0: { michael@0: var gen = michael@0: function() michael@0: { michael@0: for (var i = 0; i < keys.length; i++) michael@0: { michael@0: if (keysOnly) michael@0: yield keys[i]; michael@0: else michael@0: yield [keys[i], vals[i]]; michael@0: } michael@0: }; michael@0: return gen(); michael@0: }; michael@0: michael@0: // for each sets keysOnly==false michael@0: var index = 0; michael@0: for each (var v in iterable) michael@0: { michael@0: if (!Array_equals(v, [keys[index], vals[index]])) michael@0: throw "for-each iteration failed on index=" + index + "!"; michael@0: index++; michael@0: } michael@0: if (index != keys.length) michael@0: throw "not everything iterated! index=" + index + michael@0: ", keys.length=" + keys.length; michael@0: michael@0: if (iterable.persistedProp != 17) michael@0: throw "iterable.persistedProp not persisted!"; michael@0: } michael@0: catch (e) michael@0: { michael@0: failed = e; michael@0: } michael@0: michael@0: michael@0: michael@0: expect = false; michael@0: actual = failed; michael@0: michael@0: reportCompare(expect, actual, summary);