Skip to content

Commit

Permalink
[css-typed-om] Test that styleMap.set rejects invalid values.
Browse files Browse the repository at this point in the history
Currently, our per-property tests only test that styleMap.set accept
valid values. This patch refactors the per-property tests a bit to
also test that we reject invalid values.

Bug: 774887
Change-Id: I41b5c4c1a6dfe6e766ff37fdc1b1f890dfb81aac
Reviewed-on: https://chromium-review.googlesource.com/905523
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: nainar <nainar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536021}
  • Loading branch information
darrnshn authored and chromium-wpt-export-bot committed Feb 12, 2018
1 parent 9b23a0c commit 9d90463
Showing 1 changed file with 79 additions and 26 deletions.
@@ -1,3 +1,50 @@
const gTestSyntax = {
'0': {
description: 'unitless zero',
set: true,
examples: [
new CSSUnitValue(0, 'number'),
],
},
'<length>': {
description: 'a length',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 'px'),
new CSSUnitValue(-3.14, 'em'),
new CSSUnitValue(3.14, 'cm'),
],
},
'<percentage>': {
description: 'a percent',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 'percent'),
new CSSUnitValue(-3.14, 'percent'),
new CSSUnitValue(3.14, 'percent'),
],
},
'<time>': {
description: 'a time',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 's'),
new CSSUnitValue(-3.14, 'ms'),
new CSSUnitValue(3.14, 's'),
],
},
'<ident>': {
description: 'a CSSKeywordValue',
set: true,
get: true,
// user-specified examples
examples: null,
},
};

function testGet(propertyName, values, description) {
test(t => {
let element = createDivWithStyle(t);
Expand Down Expand Up @@ -27,36 +74,42 @@ function testSet(propertyName, values, description) {
}, `Can set '${propertyName}' to ${description}`);
}

function testGetSet(propertyName, values, description) {
testGet(propertyName, values, description);
testSet(propertyName, values, description);
function testSetInvalid(propertyName, values, description) {
test(t => {
let element = createDivWithStyle(t);
let styleMap = element.attributeStyleMap;

for (const styleValue of values) {
assert_throws(new TypeError(), () => styleMap.set(propertyName, styleValue));
}
}, `Setting '${propertyName}' to ${description} throws TypeError`);
}

function runPropertyTests(propertyName, testCases) {
let productionsTested = new Set();

for (const testCase of testCases) {
if (testCase.specified == '0') {
testSet(propertyName, [
new CSSUnitValue(0, 'number'),
], 'unitless zero');
} else if (testCase.specified === '<length>') {
testGetSet(propertyName, [
new CSSUnitValue(0, 'px'),
new CSSUnitValue(-3.14, 'em'),
new CSSUnitValue(3.14, 'cm'),
], 'a length CSSUnitValue');
} else if (testCase.specified == '<percentage>') {
testGetSet(propertyName, [
new CSSUnitValue(0, 'percent'),
new CSSUnitValue(-3.14, 'percent'),
new CSSUnitValue(3.14, 'percent'),
], 'a percent CSSUnitValue');
} else if (testCase.specified == '<ident>') {
if (!testCase.examples) {
throw new Error('<ident> tests require examples');
}

testGetSet(propertyName, testCase.examples,
'a CSSKeywordValue');
const syntax = gTestSyntax[testCase.specified];
if (!syntax)
throw new Error(`'${testCase.specified}' is not a valid production`);

const examples = testCase.examples || syntax.examples;
if (!examples)
throw new Error(`'${testCase.specified}' tests require explicit examples`);

if (syntax.get)
testGet(propertyName, examples, syntax.description);
if (syntax.set)
testSet(propertyName, examples, syntax.description);

productionsTested.add(testCase.specified);
}

// Also test that styleMap.set rejects invalid CSSStyleValues.
for (const [production, syntax] of Object.entries(gTestSyntax)) {
if (!productionsTested.has(production)) {
if (syntax.set && syntax.examples)
testSetInvalid(propertyName, syntax.examples, syntax.description);
}
}
}

0 comments on commit 9d90463

Please sign in to comment.