Replace mustHaveProperty with verifyProperty

This commit is contained in:
André Bargull 2017-12-21 12:08:21 -08:00 committed by Rick Waldron
parent ce3c3d7dbd
commit 72db5f1ec1
6 changed files with 98 additions and 82 deletions

View File

@ -828,51 +828,6 @@ function testOption(Constructor, property, type, values, fallback, testOptions)
} }
/**
* Tests whether the named property of the given object has a valid value
* and the default attributes of the properties of an object literal.
* @param {Object} obj the object to be tested.
* @param {string} property the name of the property
* @param {Function|Array} valid either a function that tests value for validity and returns a boolean,
* an array of valid values.
* @exception if the property has an invalid value.
*/
function testProperty(obj, property, valid) {
var desc = Object.getOwnPropertyDescriptor(obj, property);
if (!desc.writable) {
$ERROR("Property " + property + " must be writable.");
}
if (!desc.enumerable) {
$ERROR("Property " + property + " must be enumerable.");
}
if (!desc.configurable) {
$ERROR("Property " + property + " must be configurable.");
}
var value = desc.value;
var isValid = (typeof valid === "function") ? valid(value) : (valid.indexOf(value) !== -1);
if (!isValid) {
$ERROR("Property value " + value + " is not allowed for property " + property + ".");
}
}
/**
* Tests whether the given object has the named property with a valid value
* and the default attributes of the properties of an object literal.
* @param {Object} obj the object to be tested.
* @param {string} property the name of the property
* @param {Function|Array} valid either a function that tests value for validity and returns a boolean,
* an array of valid values.
* @exception if the property is missing or has an invalid value.
*/
function mustHaveProperty(obj, property, valid) {
if (!obj.hasOwnProperty(property)) {
$ERROR("Object is missing property " + property + ".");
}
testProperty(obj, property, valid);
}
/** /**
* Properties of the RegExp constructor that may be affected by use of regular * Properties of the RegExp constructor that may be affected by use of regular
* expressions, and the default values of these properties. Properties are from * expressions, and the default values of these properties. Properties are from

View File

@ -36,20 +36,29 @@ var collations = [
]; ];
// this assumes the default values where the specification provides them // this assumes the default values where the specification provides them
mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); assert(isCanonicalizedStructurallyValidLanguageTag(actual.locale),
mustHaveProperty(actual, "usage", ["sort"]); "Invalid locale: " + actual.locale);
mustHaveProperty(actual, "sensitivity", ["variant"]); assert.sameValue(actual.usage, "sort");
mustHaveProperty(actual, "ignorePunctuation", [false]); assert.sameValue(actual.sensitivity, "variant");
mustHaveProperty(actual, "collation", collations); assert.sameValue(actual.ignorePunctuation, false);
assert.notSameValue(collations.indexOf(actual.collation), -1,
"Invalid collation: " + actual.collation);
var dataPropertyDesc = { writable: true, enumerable: true, configurable: true };
verifyProperty(actual, "locale", dataPropertyDesc);
verifyProperty(actual, "usage", dataPropertyDesc);
verifyProperty(actual, "sensitivity", dataPropertyDesc);
verifyProperty(actual, "ignorePunctuation", dataPropertyDesc);
verifyProperty(actual, "collation", dataPropertyDesc);
// "numeric" is an optional property. // "numeric" is an optional property.
if (actual.hasOwnProperty("numeric")) { if (actual.hasOwnProperty("numeric")) {
assert.notSameValue([true, false].indexOf(actual.numeric), -1); assert.notSameValue([true, false].indexOf(actual.numeric), -1);
verifyProperty(actual, "numeric", {writable: true, enumerable: true, configurable: true}); verifyProperty(actual, "numeric", dataPropertyDesc);
} }
// "caseFirst" is an optional property. // "caseFirst" is an optional property.
if (actual.hasOwnProperty("caseFirst")) { if (actual.hasOwnProperty("caseFirst")) {
assert.notSameValue(["upper", "lower", "false"].indexOf(actual.caseFirst), -1); assert.notSameValue(["upper", "lower", "false"].indexOf(actual.caseFirst), -1);
verifyProperty(actual, "caseFirst", {writable: true, enumerable: true, configurable: true}); verifyProperty(actual, "caseFirst", dataPropertyDesc);
} }

View File

@ -35,15 +35,31 @@ var calendars = [
]; ];
// this assumes the default values where the specification provides them // this assumes the default values where the specification provides them
mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); assert(isCanonicalizedStructurallyValidLanguageTag(actual.locale),
mustHaveProperty(actual, "calendar", calendars); "Invalid locale: " + actual.locale);
mustHaveProperty(actual, "numberingSystem", isValidNumberingSystem); assert.notSameValue(calendars.indexOf(actual.calendar), -1,
mustHaveProperty(actual, "timeZone", isCanonicalizedStructurallyValidTimeZoneName); "Invalid calendar: " + actual.calendar);
assert(isValidNumberingSystem(actual.numberingSystem),
"Invalid numbering system: " + actual.numberingSystem);
assert(isCanonicalizedStructurallyValidTimeZoneName(actual.timeZone),
"Invalid time zone: " + actual.timeZone);
assert.notSameValue(["2-digit", "numeric"].indexOf(actual.year), -1,
"Invalid year: " + actual.year);
assert.notSameValue(["2-digit", "numeric", "narrow", "short", "long"].indexOf(actual.month), -1,
"Invalid month: " + actual.month);
assert.notSameValue(["2-digit", "numeric"].indexOf(actual.day), -1,
"Invalid day: " + actual.day);
var dataPropertyDesc = { writable: true, enumerable: true, configurable: true };
verifyProperty(actual, "locale", dataPropertyDesc);
verifyProperty(actual, "calendar", dataPropertyDesc);
verifyProperty(actual, "numberingSystem", dataPropertyDesc);
verifyProperty(actual, "timeZone", dataPropertyDesc);
verifyProperty(actual, "weekday", undefined); verifyProperty(actual, "weekday", undefined);
verifyProperty(actual, "era", undefined); verifyProperty(actual, "era", undefined);
mustHaveProperty(actual, "year", ["2-digit", "numeric"]); verifyProperty(actual, "year", dataPropertyDesc);
mustHaveProperty(actual, "month", ["2-digit", "numeric", "narrow", "short", "long"]); verifyProperty(actual, "month", dataPropertyDesc);
mustHaveProperty(actual, "day", ["2-digit", "numeric"]); verifyProperty(actual, "day", dataPropertyDesc);
verifyProperty(actual, "hour", undefined); verifyProperty(actual, "hour", undefined);
verifyProperty(actual, "minute", undefined); verifyProperty(actual, "minute", undefined);
verifyProperty(actual, "second", undefined); verifyProperty(actual, "second", undefined);

View File

@ -9,7 +9,7 @@ description: >
info: > info: >
12.4.5 Intl.DateTimeFormat.prototype.resolvedOptions() 12.4.5 Intl.DateTimeFormat.prototype.resolvedOptions()
includes: [testIntl.js] includes: [testIntl.js, propertyHelper.js]
---*/ ---*/
/* Values passed via unicode extension key work */ /* Values passed via unicode extension key work */
@ -27,14 +27,19 @@ const hcValuePairs = [
const hour12Values = ['h11', 'h12']; const hour12Values = ['h11', 'h12'];
const hour24Values = ['h23', 'h24']; const hour24Values = ['h23', 'h24'];
const dataPropertyDesc = { writable: true, enumerable: true, configurable: true };
for (const hcValuePair of hcValuePairs) { for (const hcValuePair of hcValuePairs) {
for (const hcValue of hcValuePair) { for (const hcValue of hcValuePair) {
const resolvedOptions = new Intl.DateTimeFormat(`de-u-hc-${hcValue}`, { const resolvedOptions = new Intl.DateTimeFormat(`de-u-hc-${hcValue}`, {
hour: 'numeric' hour: 'numeric'
}).resolvedOptions(); }).resolvedOptions();
mustHaveProperty(resolvedOptions, 'hourCycle', hcValuePair); assert(hcValuePair.includes(resolvedOptions.hourCycle));
mustHaveProperty(resolvedOptions, 'hour12', [hour12Values.includes(hcValue)]); assert.sameValue(resolvedOptions.hour12, hour12Values.includes(hcValue));
verifyProperty(resolvedOptions, 'hourCycle', dataPropertyDesc);
verifyProperty(resolvedOptions, 'hour12', dataPropertyDesc);
} }
} }
@ -47,8 +52,11 @@ for (const hcValuePair of hcValuePairs) {
hourCycle: hcValue hourCycle: hcValue
}).resolvedOptions(); }).resolvedOptions();
mustHaveProperty(resolvedOptions, 'hourCycle', hcValuePair); assert(hcValuePair.includes(resolvedOptions.hourCycle));
mustHaveProperty(resolvedOptions, 'hour12', [hour12Values.includes(hcValue)]); assert.sameValue(resolvedOptions.hour12, hour12Values.includes(hcValue));
verifyProperty(resolvedOptions, 'hourCycle', dataPropertyDesc);
verifyProperty(resolvedOptions, 'hour12', dataPropertyDesc);
} }
} }
@ -59,8 +67,11 @@ let resolvedOptions = new Intl.DateTimeFormat(`en-US-u-hc-h12`, {
hourCycle: 'h23' hourCycle: 'h23'
}).resolvedOptions(); }).resolvedOptions();
mustHaveProperty(resolvedOptions, 'hourCycle', ['h23', 'h24']); assert(['h23', 'h24'].includes(resolvedOptions.hourCycle));
mustHaveProperty(resolvedOptions, 'hour12', [false]); assert.sameValue(resolvedOptions.hour12, false);
verifyProperty(resolvedOptions, 'hourCycle', dataPropertyDesc);
verifyProperty(resolvedOptions, 'hour12', dataPropertyDesc);
/* When hour12 and hourCycle are set, hour12 takes precedence */ /* When hour12 and hourCycle are set, hour12 takes precedence */
@ -70,8 +81,11 @@ resolvedOptions = new Intl.DateTimeFormat(`fr`, {
hourCycle: 'h23' hourCycle: 'h23'
}).resolvedOptions(); }).resolvedOptions();
mustHaveProperty(resolvedOptions, 'hourCycle', ['h11', 'h12']); assert(['h11', 'h12'].includes(resolvedOptions.hourCycle));
mustHaveProperty(resolvedOptions, 'hour12', [true]); assert.sameValue(resolvedOptions.hour12, true);
verifyProperty(resolvedOptions, 'hourCycle', dataPropertyDesc);
verifyProperty(resolvedOptions, 'hour12', dataPropertyDesc);
/* When hour12 and extension key are set, hour12 takes precedence */ /* When hour12 and extension key are set, hour12 takes precedence */
@ -80,5 +94,8 @@ resolvedOptions = new Intl.DateTimeFormat(`fr-u-hc-h24`, {
hour12: true, hour12: true,
}).resolvedOptions(); }).resolvedOptions();
mustHaveProperty(resolvedOptions, 'hourCycle', ['h11', 'h12']); assert(['h11', 'h12'].includes(resolvedOptions.hourCycle));
mustHaveProperty(resolvedOptions, 'hour12', [true]); assert.sameValue(resolvedOptions.hour12, true);
verifyProperty(resolvedOptions, 'hourCycle', dataPropertyDesc);
verifyProperty(resolvedOptions, 'hour12', dataPropertyDesc);

View File

@ -17,14 +17,25 @@ var actual2 = new Intl.NumberFormat().resolvedOptions();
assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice."); assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice.");
// this assumes the default values where the specification provides them // this assumes the default values where the specification provides them
mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); assert(isCanonicalizedStructurallyValidLanguageTag(actual.locale),
mustHaveProperty(actual, "numberingSystem", isValidNumberingSystem); "Invalid locale: " + actual.locale);
mustHaveProperty(actual, "style", ["decimal"]); assert(isValidNumberingSystem(actual.numberingSystem),
"Invalid numbering system: " + actual.numberingSystem);
assert.sameValue(actual.style, "decimal");
assert.sameValue(actual.minimumIntegerDigits, 1);
assert.sameValue(actual.minimumFractionDigits, 0);
assert.sameValue(actual.maximumFractionDigits, 3);
assert.sameValue(actual.useGrouping, true);
var dataPropertyDesc = { writable: true, enumerable: true, configurable: true };
verifyProperty(actual, "locale", dataPropertyDesc);
verifyProperty(actual, "numberingSystem", dataPropertyDesc);
verifyProperty(actual, "style", dataPropertyDesc);
verifyProperty(actual, "currency", undefined); verifyProperty(actual, "currency", undefined);
verifyProperty(actual, "currencyDisplay", undefined); verifyProperty(actual, "currencyDisplay", undefined);
mustHaveProperty(actual, "minimumIntegerDigits", [1]); verifyProperty(actual, "minimumIntegerDigits", dataPropertyDesc);
mustHaveProperty(actual, "minimumFractionDigits", [0]); verifyProperty(actual, "minimumFractionDigits", dataPropertyDesc);
mustHaveProperty(actual, "maximumFractionDigits", [3]); verifyProperty(actual, "maximumFractionDigits", dataPropertyDesc);
verifyProperty(actual, "minimumSignificantDigits", undefined); verifyProperty(actual, "minimumSignificantDigits", undefined);
verifyProperty(actual, "maximumSignificantDigits", undefined); verifyProperty(actual, "maximumSignificantDigits", undefined);
mustHaveProperty(actual, "useGrouping", [true]); verifyProperty(actual, "useGrouping", dataPropertyDesc);

View File

@ -17,12 +17,20 @@ var actual2 = new Intl.PluralRules().resolvedOptions();
assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice."); assert.notSameValue(actual2, actual, "resolvedOptions returned the same object twice.");
// this assumes the default values where the specification provides them // this assumes the default values where the specification provides them
mustHaveProperty(actual, "locale", isCanonicalizedStructurallyValidLanguageTag); assert(isCanonicalizedStructurallyValidLanguageTag(actual.locale),
mustHaveProperty(actual, "type", ["cardinal"]); "Invalid locale: " + actual.locale);
assert.sameValue(actual.type, "cardinal");
assert.sameValue(actual.minimumIntegerDigits, 1);
assert.sameValue(actual.minimumFractionDigits, 0);
assert.sameValue(actual.maximumFractionDigits, 3);
var dataPropertyDesc = { writable: true, enumerable: true, configurable: true };
verifyProperty(actual, "locale", dataPropertyDesc);
verifyProperty(actual, "type", dataPropertyDesc);
verifyProperty(actual, "currency", undefined); verifyProperty(actual, "currency", undefined);
verifyProperty(actual, "currencyDisplay", undefined); verifyProperty(actual, "currencyDisplay", undefined);
mustHaveProperty(actual, "minimumIntegerDigits", [1]); verifyProperty(actual, "minimumIntegerDigits", dataPropertyDesc);
mustHaveProperty(actual, "minimumFractionDigits", [0]); verifyProperty(actual, "minimumFractionDigits", dataPropertyDesc);
mustHaveProperty(actual, "maximumFractionDigits", [3]); verifyProperty(actual, "maximumFractionDigits", dataPropertyDesc);
verifyProperty(actual, "minimumSignificantDigits", undefined); verifyProperty(actual, "minimumSignificantDigits", undefined);
verifyProperty(actual, "maximumSignificantDigits", undefined); verifyProperty(actual, "maximumSignificantDigits", undefined);