Fix code duplication in tests

This commit is contained in:
RMidhunSuresh 2022-03-24 16:52:26 +05:30
parent cbff912476
commit b59d6970fc
3 changed files with 35 additions and 22 deletions

View File

@ -0,0 +1,30 @@
/*
Copyright 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const postcss = require("postcss");
module.exports.createTestRunner = function (plugin) {
return async function run(input, output, opts = {}, assert) {
let result = await postcss([plugin(opts)]).process(input, { from: undefined, });
assert.strictEqual(
result.css.replaceAll(/\s/g, ""),
output.replaceAll(/\s/g, "")
);
assert.strictEqual(result.warnings().length, 0);
};
}

View File

@ -18,15 +18,7 @@ const offColor = require("off-color").offColor;
const postcss = require("postcss"); const postcss = require("postcss");
const plugin = require("../css-compile-variables"); const plugin = require("../css-compile-variables");
const derive = require("../color").derive; const derive = require("../color").derive;
const run = require("./common").createTestRunner(plugin);
async function run(input, output, opts = {}, assert) {
let result = await postcss([plugin({ ...opts, derive })]).process(input, { from: undefined, });
assert.strictEqual(
result.css.replaceAll(/\s/g, ""),
output.replaceAll(/\s/g, "")
);
assert.strictEqual(result.warnings().length, 0);
}
module.exports.tests = function tests() { module.exports.tests = function tests() {
return { return {
@ -46,7 +38,7 @@ module.exports.tests = function tests() {
--foo-color--lighter-50: ${transformedColor.hex()}; --foo-color--lighter-50: ${transformedColor.hex()};
} }
`; `;
await run( inputCSS, outputCSS, {}, assert); await run( inputCSS, outputCSS, {derive}, assert);
}, },
"derived variables work with alias": async (assert) => { "derived variables work with alias": async (assert) => {
@ -66,7 +58,7 @@ module.exports.tests = function tests() {
--my-alias--lighter-15: ${aliasLighter}; --my-alias--lighter-15: ${aliasLighter};
} }
`; `;
await run(inputCSS, outputCSS, { }, assert); await run(inputCSS, outputCSS, {derive}, assert);
}, },
"derived variable throws if base not present in config": async (assert) => { "derived variable throws if base not present in config": async (assert) => {
@ -94,7 +86,7 @@ module.exports.tests = function tests() {
--foo-color--darker-20: ${transformedColor2.hex()}; --foo-color--darker-20: ${transformedColor2.hex()};
} }
`; `;
await run( inputCSS, outputCSS, { }, assert); await run( inputCSS, outputCSS, {derive}, assert);
}, },
"multiple aliased-derived variable in single declaration is parsed correctly": async (assert) => { "multiple aliased-derived variable in single declaration is parsed correctly": async (assert) => {

View File

@ -14,17 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const postcss = require("postcss");
const plugin = require("../css-url-to-variables"); const plugin = require("../css-url-to-variables");
const run = require("./common").createTestRunner(plugin);
async function run(input, output, opts = {}, assert) {
let result = await postcss([plugin(opts)]).process(input, { from: undefined, });
assert.strictEqual(
result.css.replaceAll(/\s/g, ""),
output.replaceAll(/\s/g, "")
);
assert.strictEqual(result.warnings().length, 0);
}
module.exports.tests = function tests() { module.exports.tests = function tests() {
return { return {