michael@0: // Copyright 2012 Mozilla Corporation. All rights reserved. michael@0: // This code is governed by the BSD license found in the LICENSE file. michael@0: michael@0: /** michael@0: * @description Tests that Date.prototype.toLocaleString & Co. throws the same exceptions as Intl.DateTimeFormat. michael@0: * @author Norbert Lindenberg michael@0: */ michael@0: michael@0: var functions = { michael@0: toLocaleString: Date.prototype.toLocaleString, michael@0: toLocaleDateString: Date.prototype.toLocaleDateString, michael@0: toLocaleTimeString: Date.prototype.toLocaleTimeString michael@0: }; michael@0: var locales = [null, [NaN], ["i"], ["de_DE"]]; michael@0: var options = [ michael@0: {localeMatcher: null}, michael@0: {timeZone: "invalid"}, michael@0: {hour: "long"}, michael@0: {formatMatcher: "invalid"} michael@0: ]; michael@0: michael@0: Object.getOwnPropertyNames(functions).forEach(function (p) { michael@0: var f = functions[p]; michael@0: locales.forEach(function (locales) { michael@0: var referenceError, error; michael@0: try { michael@0: var format = new Intl.DateTimeFormat(locales); michael@0: } catch (e) { michael@0: referenceError = e; michael@0: } michael@0: if (referenceError === undefined) { michael@0: $ERROR("Internal error: Expected exception was not thrown by Intl.DateTimeFormat for locales " + locales + "."); michael@0: } michael@0: michael@0: try { michael@0: var result = f.call(new Date(), locales); michael@0: } catch (e) { michael@0: error = e; michael@0: } michael@0: if (error === undefined) { michael@0: $ERROR("Date.prototype." + p + " didn't throw exception for locales " + locales + "."); michael@0: } else if (error.name !== referenceError.name) { michael@0: $ERROR("Date.prototype." + p + " threw exception " + error.name + michael@0: " for locales " + locales + "; expected " + referenceError.name + "."); michael@0: } michael@0: }); michael@0: michael@0: options.forEach(function (options) { michael@0: var referenceError, error; michael@0: try { michael@0: var format = new Intl.DateTimeFormat([], options); michael@0: } catch (e) { michael@0: referenceError = e; michael@0: } michael@0: if (referenceError === undefined) { michael@0: $ERROR("Internal error: Expected exception was not thrown by Intl.DateTimeFormat for options " + michael@0: JSON.stringify(options) + "."); michael@0: } michael@0: michael@0: try { michael@0: var result = f.call(new Date(), [], options); michael@0: } catch (e) { michael@0: error = e; michael@0: } michael@0: if (error === undefined) { michael@0: $ERROR("Date.prototype." + p + " didn't throw exception for options " + michael@0: JSON.stringify(options) + "."); michael@0: } else if (error.name !== referenceError.name) { michael@0: $ERROR("Date.prototype." + p + " threw exception " + error.name + michael@0: " for options " + JSON.stringify(options) + "; expected " + referenceError.name + "."); michael@0: } michael@0: }); michael@0: }); michael@0: